more information in cost usage console
This commit is contained in:
parent
409935d998
commit
a0de6da682
5 changed files with 308 additions and 11 deletions
|
|
@ -117,11 +117,11 @@ export default function Details({ interaction }: DetailsProps) {
|
|||
format={(v: number) => v.toFixed(1)}
|
||||
></Cell>
|
||||
</div>
|
||||
{/**summary.costBreakdown && (
|
||||
{summary.costBreakdown && (
|
||||
<pre className="whitespace-pre-wrap px-2 text-sm">
|
||||
{summary.costBreakdown.breakdown}
|
||||
</pre>
|
||||
)*/}
|
||||
)}
|
||||
</div>
|
||||
<div
|
||||
ref={scrollTop}
|
||||
|
|
|
|||
|
|
@ -2,11 +2,13 @@ import { useState } from "react";
|
|||
import useLLMLog from "../../hooks/useLLMLog";
|
||||
import Details from "./Details";
|
||||
import List from "./List";
|
||||
import TotalUsage from "./TotalUsage";
|
||||
|
||||
export default function Layout() {
|
||||
const llmLog = useLLMLog();
|
||||
|
||||
const [selectedId, setSelectedId] = useState<string | undefined>(undefined);
|
||||
const [showTotalUsage, setShowTotalUsage] = useState(false);
|
||||
|
||||
const interaction = selectedId
|
||||
? llmLog.interactions.get(selectedId)
|
||||
|
|
@ -16,14 +18,46 @@ export default function Layout() {
|
|||
<div>Loading...</div>
|
||||
) : (
|
||||
<div className="flex h-full w-full">
|
||||
<List
|
||||
llmLog={llmLog}
|
||||
onClickInteraction={(interactionId) => {
|
||||
setSelectedId(interactionId);
|
||||
}}
|
||||
></List>
|
||||
{interaction && (
|
||||
<Details key="{selectedId}" interaction={interaction}></Details>
|
||||
<div className="flex h-full w-full flex-col">
|
||||
<div className="flex-shrink-0 border-b border-[color:var(--vscode-panel-border)] p-3">
|
||||
<button
|
||||
onClick={() => setShowTotalUsage(true)}
|
||||
className="float-right cursor-pointer border-none bg-transparent text-sm text-[color:var(--vscode-textLink-foreground)] underline hover:text-[color:var(--vscode-textLink-activeForeground)]"
|
||||
>
|
||||
View Total Usage Summary
|
||||
</button>
|
||||
</div>
|
||||
<div className="flex min-h-0 flex-1 overflow-hidden">
|
||||
<List
|
||||
llmLog={llmLog}
|
||||
onClickInteraction={(interactionId) => {
|
||||
setSelectedId(interactionId);
|
||||
}}
|
||||
></List>
|
||||
{interaction && (
|
||||
<Details key="{selectedId}" interaction={interaction}></Details>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* Full-screen popover for TotalUsage */}
|
||||
{showTotalUsage && (
|
||||
<div className="fixed inset-0 z-50 flex items-center justify-center bg-black bg-opacity-50">
|
||||
<div className="relative h-full w-full max-w-6xl overflow-hidden bg-[color:var(--vscode-panel-background)] shadow-2xl">
|
||||
{/* Close button */}
|
||||
<button
|
||||
onClick={() => setShowTotalUsage(false)}
|
||||
className="absolute right-4 top-4 z-10 flex h-8 w-8 items-center justify-center rounded-full bg-[color:var(--vscode-button-background)] text-[color:var(--vscode-button-foreground)] transition-colors hover:bg-[color:var(--vscode-button-hoverBackground)]"
|
||||
>
|
||||
×
|
||||
</button>
|
||||
|
||||
{/* Content container with scroll */}
|
||||
<div className="h-full overflow-y-auto p-6">
|
||||
<TotalUsage llmLog={llmLog} />
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
|
|
|
|||
161
gui/src/components/console/TotalUsage.tsx
Normal file
161
gui/src/components/console/TotalUsage.tsx
Normal file
|
|
@ -0,0 +1,161 @@
|
|||
import React, { useState } from "react";
|
||||
import { LLMLog } from "../../hooks/useLLMLog";
|
||||
import useTotalUsage from "../../hooks/useTotalUsage";
|
||||
|
||||
interface TotalUsageProps {
|
||||
llmLog: LLMLog;
|
||||
}
|
||||
|
||||
const StatCard: React.FC<{ label: string; value: number; color?: string }> = ({
|
||||
label,
|
||||
value,
|
||||
color = "var(--vscode-foreground)",
|
||||
}) => (
|
||||
<div className="flex flex-col items-center rounded-lg border border-[color:var(--vscode-panel-border)] bg-[color:var(--vscode-editor-background)] p-3">
|
||||
<div className="text-sm text-[color:var(--vscode-descriptionForeground)]">
|
||||
{label}
|
||||
</div>
|
||||
<div className="text-lg font-semibold" style={{ color }}>
|
||||
{value.toLocaleString()}
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
|
||||
const TotalUsage: React.FC<TotalUsageProps> = ({ llmLog }) => {
|
||||
const totalUsage = useTotalUsage(llmLog);
|
||||
const [showCostBreakdown, setShowCostBreakdown] = useState(false);
|
||||
|
||||
return (
|
||||
<div className="border-b border-[color:var(--vscode-panel-border)] p-4">
|
||||
<div className="mb-4 flex items-center justify-between">
|
||||
<h2 className="text-lg font-semibold text-[color:var(--vscode-foreground)]">
|
||||
Total Usage Summary
|
||||
</h2>
|
||||
<div className="flex items-center gap-4">
|
||||
{totalUsage.totalCost > 0 && (
|
||||
<button
|
||||
onClick={() => setShowCostBreakdown(!showCostBreakdown)}
|
||||
className="cursor-pointer text-lg font-semibold text-[color:var(--vscode-charts-green)] hover:text-[color:var(--vscode-charts-blue)]"
|
||||
>
|
||||
${totalUsage.totalCost.toFixed(6)}
|
||||
</button>
|
||||
)}
|
||||
<div className="text-sm text-[color:var(--vscode-descriptionForeground)]">
|
||||
{totalUsage.totalInteractions} interactions
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{showCostBreakdown && totalUsage.costBreakdowns.length > 0 && (
|
||||
<div className="mb-4 max-h-32 overflow-y-auto rounded border border-[color:var(--vscode-panel-border)] bg-[color:var(--vscode-editor-background)] p-3">
|
||||
<h3 className="mb-2 text-sm font-semibold text-[color:var(--vscode-foreground)]">
|
||||
Cost Breakdown
|
||||
</h3>
|
||||
<div className="space-y-2 text-xs">
|
||||
{totalUsage.costBreakdowns.map((breakdown, index) => (
|
||||
<div
|
||||
key={index}
|
||||
className="border-b border-[color:var(--vscode-panel-border)] pb-2 last:border-b-0"
|
||||
>
|
||||
<pre className="whitespace-pre-wrap text-[color:var(--vscode-descriptionForeground)]">
|
||||
{breakdown.breakdown}
|
||||
</pre>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
|
||||
<div className="mb-4 grid grid-cols-2 gap-3 md:grid-cols-4">
|
||||
<StatCard
|
||||
label="Input Tokens"
|
||||
value={totalUsage.totalPromptTokens}
|
||||
color="var(--vscode-charts-blue)"
|
||||
/>
|
||||
<StatCard
|
||||
label="Output Tokens"
|
||||
value={totalUsage.totalGeneratedTokens}
|
||||
color="var(--vscode-charts-green)"
|
||||
/>
|
||||
<StatCard
|
||||
label="Thinking Tokens"
|
||||
value={totalUsage.totalThinkingTokens}
|
||||
color="var(--vscode-charts-purple)"
|
||||
/>
|
||||
<StatCard
|
||||
label="Cache Read"
|
||||
value={totalUsage.totalCachedTokens}
|
||||
color="var(--vscode-charts-orange)"
|
||||
/>
|
||||
</div>
|
||||
|
||||
{totalUsage.totalCost > 0 && (
|
||||
<div className="mb-4">
|
||||
<div className="flex flex-col items-center rounded-lg border border-[color:var(--vscode-panel-border)] bg-[color:var(--vscode-editor-background)] p-3">
|
||||
<div className="text-sm text-[color:var(--vscode-descriptionForeground)]">
|
||||
Total Cost
|
||||
</div>
|
||||
<div className="text-2xl font-bold text-[color:var(--vscode-charts-green)]">
|
||||
${totalUsage.totalCost.toFixed(6)}
|
||||
</div>
|
||||
<div className="text-xs text-[color:var(--vscode-descriptionForeground)]">
|
||||
{totalUsage.costBreakdowns.length} cost calculations
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{totalUsage.totalCacheWriteTokens > 0 && (
|
||||
<div className="mb-4 grid grid-cols-1 gap-3 md:grid-cols-2">
|
||||
<StatCard
|
||||
label="Cache Write"
|
||||
value={totalUsage.totalCacheWriteTokens}
|
||||
color="var(--vscode-charts-red)"
|
||||
/>
|
||||
<div className="flex items-center justify-center rounded-lg border border-[color:var(--vscode-panel-border)] bg-[color:var(--vscode-editor-background)] p-3">
|
||||
<div className="text-sm text-[color:var(--vscode-descriptionForeground)]">
|
||||
Cache Hit Rate:{" "}
|
||||
{totalUsage.totalPromptTokens > 0
|
||||
? (
|
||||
(totalUsage.totalCachedTokens /
|
||||
totalUsage.totalPromptTokens) *
|
||||
100
|
||||
).toFixed(1)
|
||||
: "0"}
|
||||
%
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
|
||||
<div className="grid grid-cols-3 gap-3 text-sm">
|
||||
<div className="rounded border border-[color:var(--vscode-panel-border)] bg-[color:var(--vscode-editor-background)] p-2 text-center">
|
||||
<div className="text-[color:var(--vscode-charts-green)]">
|
||||
{totalUsage.totalSuccessfulInteractions}
|
||||
</div>
|
||||
<div className="text-[color:var(--vscode-descriptionForeground)]">
|
||||
Success
|
||||
</div>
|
||||
</div>
|
||||
<div className="rounded border border-[color:var(--vscode-panel-border)] bg-[color:var(--vscode-editor-background)] p-2 text-center">
|
||||
<div className="text-[color:var(--vscode-charts-red)]">
|
||||
{totalUsage.totalErrorInteractions}
|
||||
</div>
|
||||
<div className="text-[color:var(--vscode-descriptionForeground)]">
|
||||
Error
|
||||
</div>
|
||||
</div>
|
||||
<div className="rounded border border-[color:var(--vscode-panel-border)] bg-[color:var(--vscode-editor-background)] p-2 text-center">
|
||||
<div className="text-[color:var(--vscode-charts-yellow)]">
|
||||
{totalUsage.totalCancelledInteractions}
|
||||
</div>
|
||||
<div className="text-[color:var(--vscode-descriptionForeground)]">
|
||||
Cancelled
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
||||
export default TotalUsage;
|
||||
|
|
@ -3,7 +3,7 @@ import ReactDOM from "react-dom/client";
|
|||
import Layout from "./components/console/Layout";
|
||||
import "./indexConsole.css";
|
||||
|
||||
(async () => {
|
||||
void (async () => {
|
||||
ReactDOM.createRoot(document.getElementById("root") as HTMLElement).render(
|
||||
<React.StrictMode>
|
||||
<Layout></Layout>
|
||||
|
|
|
|||
102
gui/src/hooks/useTotalUsage.ts
Normal file
102
gui/src/hooks/useTotalUsage.ts
Normal file
|
|
@ -0,0 +1,102 @@
|
|||
import {
|
||||
calculateRequestCost,
|
||||
CostBreakdown,
|
||||
} from "core/llm/utils/calculateRequestCost";
|
||||
import { useMemo } from "react";
|
||||
import { LLMLog } from "./useLLMLog";
|
||||
|
||||
export interface TotalUsage {
|
||||
totalPromptTokens: number;
|
||||
totalGeneratedTokens: number;
|
||||
totalThinkingTokens: number;
|
||||
totalCachedTokens: number;
|
||||
totalCacheWriteTokens: number;
|
||||
totalInteractions: number;
|
||||
totalSuccessfulInteractions: number;
|
||||
totalErrorInteractions: number;
|
||||
totalCancelledInteractions: number;
|
||||
totalCost: number;
|
||||
costBreakdowns: CostBreakdown[];
|
||||
}
|
||||
|
||||
/**
|
||||
* Hook to calculate total usage across all interactions in the LLM log
|
||||
*/
|
||||
export default function useTotalUsage(llmLog: LLMLog): TotalUsage {
|
||||
return useMemo(() => {
|
||||
let totalPromptTokens = 0;
|
||||
let totalGeneratedTokens = 0;
|
||||
let totalThinkingTokens = 0;
|
||||
let totalCachedTokens = 0;
|
||||
let totalCacheWriteTokens = 0;
|
||||
let totalInteractions = 0;
|
||||
let totalSuccessfulInteractions = 0;
|
||||
let totalErrorInteractions = 0;
|
||||
let totalCancelledInteractions = 0;
|
||||
let totalCost = 0;
|
||||
const costBreakdowns: CostBreakdown[] = [];
|
||||
|
||||
// Iterate through all interactions
|
||||
for (const interaction of llmLog.interactions.values()) {
|
||||
if (interaction.end) {
|
||||
totalInteractions++;
|
||||
|
||||
// Add basic token counts
|
||||
totalPromptTokens += interaction.end.promptTokens || 0;
|
||||
totalGeneratedTokens += interaction.end.generatedTokens || 0;
|
||||
totalThinkingTokens += interaction.end.thinkingTokens || 0;
|
||||
|
||||
// Add cache-related tokens from usage if available
|
||||
if (interaction.end.usage) {
|
||||
const usage = interaction.end.usage;
|
||||
totalCachedTokens += usage.promptTokensDetails?.cachedTokens || 0;
|
||||
totalCacheWriteTokens +=
|
||||
usage.promptTokensDetails?.cacheWriteTokens || 0;
|
||||
|
||||
// Calculate cost if we have the necessary information
|
||||
if (
|
||||
interaction.start?.provider &&
|
||||
interaction.start?.options?.model
|
||||
) {
|
||||
const costBreakdown = calculateRequestCost(
|
||||
interaction.start.provider,
|
||||
interaction.start.options.model,
|
||||
usage,
|
||||
);
|
||||
if (costBreakdown) {
|
||||
totalCost += costBreakdown.cost;
|
||||
costBreakdowns.push(costBreakdown);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Count interaction outcomes
|
||||
switch (interaction.end.kind) {
|
||||
case "success":
|
||||
totalSuccessfulInteractions++;
|
||||
break;
|
||||
case "error":
|
||||
totalErrorInteractions++;
|
||||
break;
|
||||
case "cancel":
|
||||
totalCancelledInteractions++;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
totalPromptTokens,
|
||||
totalGeneratedTokens,
|
||||
totalThinkingTokens,
|
||||
totalCachedTokens,
|
||||
totalCacheWriteTokens,
|
||||
totalInteractions,
|
||||
totalSuccessfulInteractions,
|
||||
totalErrorInteractions,
|
||||
totalCancelledInteractions,
|
||||
totalCost,
|
||||
costBreakdowns,
|
||||
};
|
||||
}, [llmLog]);
|
||||
}
|
||||
Loading…
Add table
Reference in a new issue