Skip to content

Commit ee215bc

Browse files
committed
fix(web): address Insights token usage review findings
1 parent b69271d commit ee215bc

5 files changed

Lines changed: 123 additions & 27 deletions

File tree

‎apps/server/src/usage/UsageService.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,9 @@ export class UsageService extends Context.Service<
151151
{
152152
readonly readSummary: (input: UsageSummaryInput) => Effect.Effect<UsageSummary, UsageReadError>;
153153
readonly refreshRates: Effect.Effect<UsagePricing>;
154-
readonly priceUsage: (input: UsagePriceInput) => Effect.Effect<UsagePriceResult>;
154+
readonly priceUsage: (
155+
input: UsagePriceInput,
156+
) => Effect.Effect<UsagePriceResult, UsageReadError>;
155157
}
156158
>()("t3/usage/UsageService") {}
157159

‎apps/web/src/components/InsightsPanel.tsx‎

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -97,10 +97,10 @@ function priceItemsForRows(
9797
rows: readonly ThreadTurnTokenUsageRow[],
9898
): readonly UsagePriceItemInput[] {
9999
return rows.flatMap((row) => {
100-
if (row.model === null) return [];
100+
if (row.priceModel === null) return [];
101101
const totals = threadTurnTokenUsageTotals(row.usage);
102102
if (totals === null) return [];
103-
return [{ id: row.runId, model: row.model, totals }];
103+
return [{ id: row.runId, model: row.priceModel, totals }];
104104
});
105105
}
106106

@@ -255,12 +255,22 @@ function FlameChartPanel({
255255
startMs: 0,
256256
durationMs: Math.max(MIN_VIEW_MS, axis.displayTotalMs),
257257
};
258-
const traceKey = `${trace.originMs}:${trace.totalMs}:${trace.turns.length}:${axis.displayTotalMs}`;
258+
// Keep zoom/pan across live totalMs updates; only reset when the turn set changes.
259+
const traceKey = `${trace.originMs}:${trace.turns.map((turn) => turn.runId).join("\0")}`;
259260
const [viewByKey, setViewByKey] = useState<{ key: string; view: ViewWindow }>(() => ({
260261
key: traceKey,
261262
view: fitView,
262263
}));
263-
const view = viewByKey.key === traceKey ? viewByKey.view : fitView;
264+
const view =
265+
viewByKey.key === traceKey
266+
? clampTimingViewWindow(
267+
viewByKey.view.startMs,
268+
viewByKey.view.durationMs,
269+
0,
270+
Math.max(1, axis.displayTotalMs),
271+
MIN_VIEW_MS,
272+
)
273+
: fitView;
264274

265275
const [selection, setSelection] = useState<Selection | null>(null);
266276
const selectedTurn =
@@ -430,6 +440,8 @@ function Summary({
430440
tokenUsage.usageStatus !== "unavailable" &&
431441
((tokenUsage.totalTokens ?? 0) > 0 ||
432442
(tokenUsage.uncachedInputTokens ?? 0) > 0 ||
443+
(tokenUsage.cachedInputTokens ?? 0) > 0 ||
444+
(tokenUsage.cacheCreationTokens ?? 0) > 0 ||
433445
(tokenUsage.outputTokens ?? 0) > 0);
434446
return (
435447
<div className={cn("grid shrink-0 gap-4", showTokens ? "lg:grid-cols-2" : "grid-cols-1")}>
@@ -565,7 +577,12 @@ function TokenUsageSummary({
565577
Some turns are still running or missing counts; totals may grow.
566578
</p>
567579
) : null}
568-
{cost === null && ((usage.totalTokens ?? 0) > 0 || (usage.outputTokens ?? 0) > 0) ? (
580+
{cost === null &&
581+
((usage.totalTokens ?? 0) > 0 ||
582+
(usage.uncachedInputTokens ?? 0) > 0 ||
583+
(usage.cachedInputTokens ?? 0) > 0 ||
584+
(usage.cacheCreationTokens ?? 0) > 0 ||
585+
(usage.outputTokens ?? 0) > 0) ? (
569586
<p className="text-2xs text-muted-foreground">
570587
Estimated API cost appears when Usage has rates for the models in this thread.
571588
</p>
@@ -599,7 +616,7 @@ function TurnTokenBreakdown({
599616
<h3 className="text-sm font-medium text-foreground">By turn</h3>
600617
<span className="text-2xs text-muted-foreground">Estimated API cost</span>
601618
</div>
602-
<ul className="divide-y divide-border/40 overflow-hidden rounded-md border border-border/50">
619+
<ul className="max-h-40 divide-y divide-border/40 overflow-x-hidden overflow-y-auto rounded-md border border-border/50">
603620
{visible.map((row) => {
604621
const cost = formatCostEstimate(costs.get(row.runId) ?? null);
605622
const tokens =
@@ -854,6 +871,7 @@ function FlameViewport({
854871
zIndex: selected ? 30 : 10 + node.depth,
855872
}}
856873
onPointerDown={(event) => {
874+
suppressClickRef.current = false;
857875
event.stopPropagation();
858876
}}
859877
onClick={(event) => {

‎packages/client-runtime/src/state/threadTurnTokenUsage.test.ts‎

Lines changed: 56 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,20 @@ describe("summarizeThreadTurnTokenUsage", () => {
113113
expect(summary.measuredTurnCount).toBe(2);
114114
});
115115

116+
it("marks partial when only cache counters are present", () => {
117+
const summary = summarizeThreadTurnTokenUsage([
118+
turn({
119+
usageScope: "main_agent",
120+
usageStatus: "partial",
121+
cachedInputTokens: 12,
122+
hasSubagents: false,
123+
}),
124+
]);
125+
expect(summary.usageStatus).toBe("partial");
126+
expect(summary.cachedInputTokens).toBe(12);
127+
expect(summary.measuredTurnCount).toBe(1);
128+
});
129+
116130
it("sums native otherUsage when present", () => {
117131
const summary = summarizeThreadTurnTokenUsage([
118132
turn(
@@ -164,7 +178,7 @@ describe("threadTurnTokenUsageTotals", () => {
164178
});
165179

166180
describe("listThreadTurnTokenUsageByRun", () => {
167-
it("groups provider turns by non-superseded attempts and prefers reported models", () => {
181+
it("includes superseded attempts and prices only a single reported model", () => {
168182
const rows = listThreadTurnTokenUsageByRun({
169183
runs: [
170184
{
@@ -209,12 +223,52 @@ describe("listThreadTurnTokenUsageByRun", () => {
209223
expect(rows[0]).toMatchObject({
210224
runId: "run-1",
211225
model: "claude-opus-4",
212-
usage: { inputTokens: 40, outputTokens: 5, totalTokens: 45, measuredTurnCount: 1 },
226+
priceModel: "claude-opus-4",
227+
usage: { inputTokens: 1039, outputTokens: 14, totalTokens: 1053, measuredTurnCount: 2 },
213228
});
214229
expect(rows[1]).toMatchObject({
215230
runId: "run-2",
216231
model: "fallback-model",
232+
priceModel: "fallback-model",
217233
usage: { inputTokens: 10, outputTokens: 2, totalTokens: 12 },
218234
});
219235
});
236+
237+
it("leaves mixed-model runs unpriced", () => {
238+
const rows = listThreadTurnTokenUsageByRun({
239+
runs: [
240+
{
241+
id: "run-1" as never,
242+
ordinal: 1,
243+
activeAttemptId: "attempt-1" as never,
244+
modelSelection: { instanceId: "claude" as never, model: "fallback-model" },
245+
},
246+
],
247+
attempts: [{ id: "attempt-1" as never, runId: "run-1" as never, status: "completed" }],
248+
providerTurns: [
249+
{
250+
runAttemptId: "attempt-1" as never,
251+
turnTokenUsage: complete({
252+
inputTokens: 10,
253+
outputTokens: 1,
254+
models: ["claude-opus-4"],
255+
}),
256+
},
257+
{
258+
runAttemptId: "attempt-1" as never,
259+
turnTokenUsage: complete({
260+
inputTokens: 20,
261+
outputTokens: 2,
262+
models: ["claude-sonnet-4"],
263+
}),
264+
},
265+
],
266+
});
267+
268+
expect(rows[0]).toMatchObject({
269+
model: "Mixed models",
270+
priceModel: null,
271+
usage: { totalTokens: 33 },
272+
});
273+
});
220274
});

‎packages/client-runtime/src/state/threadTurnTokenUsage.ts‎

Lines changed: 39 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,13 @@ export type ThreadTurnTokenUsageSummary = {
2929
export type ThreadTurnTokenUsageRow = {
3030
readonly runId: RunId;
3131
readonly ordinal: number;
32-
/** Native model id when a turn reported one; otherwise the run's selected model. */
32+
/** Label for the By turn row (a single model, Mixed models, or Unknown model). */
3333
readonly model: string | null;
34+
/**
35+
* Model id to price this run's totals. Null when models are mixed or unknown so we do not
36+
* apply one rate to another model's tokens.
37+
*/
38+
readonly priceModel: string | null;
3439
readonly usage: ThreadTurnTokenUsageSummary;
3540
};
3641

@@ -69,18 +74,24 @@ function sumOther(
6974
return known === 0 ? null : sum;
7075
}
7176

77+
function usageHasCounts(usage: TurnTokenUsage | undefined): boolean {
78+
return (
79+
usage?.inputTokens !== undefined ||
80+
usage?.outputTokens !== undefined ||
81+
usage?.cachedInputTokens !== undefined ||
82+
usage?.cacheCreationTokens !== undefined ||
83+
usage?.reasoningTokens !== undefined
84+
);
85+
}
86+
7287
/** complete only when every turn reports complete usage; partial when any turn has counts. */
7388
function combinedStatus(
7489
usages: ReadonlyArray<TurnTokenUsage | undefined>,
7590
): ThreadTurnTokenUsageStatus {
7691
if (usages.length > 0 && usages.every((usage) => usage?.usageStatus === "complete")) {
7792
return "complete";
7893
}
79-
return usages.some(
80-
(usage) => usage?.inputTokens !== undefined || usage?.outputTokens !== undefined,
81-
)
82-
? "partial"
83-
: "unavailable";
94+
return usages.some(usageHasCounts) ? "partial" : "unavailable";
8495
}
8596

8697
function emptySummary(turnCount: number): ThreadTurnTokenUsageSummary {
@@ -169,19 +180,30 @@ export function threadTurnTokenUsageTotals(
169180
};
170181
}
171182

172-
function reportedModels(
183+
function resolveRunModels(
173184
turns: ReadonlyArray<Pick<OrchestrationV2ProviderTurn, "turnTokenUsage">>,
174-
): string | null {
185+
fallbackModel: string,
186+
): { readonly label: string | null; readonly priceModel: string | null } {
187+
const reported = new Set<string>();
175188
for (const turn of turns) {
176-
const model = turn.turnTokenUsage?.models?.find((entry) => entry.trim().length > 0);
177-
if (model !== undefined) return model.trim();
189+
for (const entry of turn.turnTokenUsage?.models ?? []) {
190+
const model = entry.trim();
191+
if (model.length > 0) reported.add(model);
192+
}
193+
}
194+
if (reported.size > 1) return { label: "Mixed models", priceModel: null };
195+
if (reported.size === 1) {
196+
const model = [...reported][0]!;
197+
return { label: model, priceModel: model };
178198
}
179-
return null;
199+
const fallback = fallbackModel.trim();
200+
if (fallback.length > 0) return { label: fallback, priceModel: fallback };
201+
return { label: null, priceModel: null };
180202
}
181203

182204
/**
183-
* One row per user run, summing provider turns on non-superseded attempts.
184-
* Retries that actually ran still count; superseded attempts do not.
205+
* One row per user run, summing provider turns across every attempt that ran.
206+
* Superseded attempts still count when they burned tokens before a restart.
185207
*/
186208
export function listThreadTurnTokenUsageByRun(input: {
187209
readonly runs: ReadonlyArray<
@@ -212,14 +234,14 @@ export function listThreadTurnTokenUsageByRun(input: {
212234

213235
const rows: ThreadTurnTokenUsageRow[] = [];
214236
for (const run of [...input.runs].sort((a, b) => a.ordinal - b.ordinal)) {
215-
const attempts = (attemptsByRun.get(run.id) ?? []).filter(
216-
(attempt) => attempt.status !== "superseded",
217-
);
237+
const attempts = attemptsByRun.get(run.id) ?? [];
218238
const turns = attempts.flatMap((attempt) => turnsByAttempt.get(attempt.id) ?? []);
239+
const models = resolveRunModels(turns, run.modelSelection.model);
219240
rows.push({
220241
runId: run.id,
221242
ordinal: run.ordinal,
222-
model: reportedModels(turns) ?? (run.modelSelection.model.trim() || null),
243+
model: models.label,
244+
priceModel: models.priceModel,
223245
usage: summarizeThreadTurnTokenUsage(turns),
224246
});
225247
}

‎packages/contracts/src/rpc.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -981,7 +981,7 @@ const WsServerRefreshUsageRatesRpc = Rpc.make(WS_METHODS.serverRefreshUsageRates
981981
const WsServerPriceUsageRpc = Rpc.make(WS_METHODS.serverPriceUsage, {
982982
payload: UsagePriceInput,
983983
success: UsagePriceResult,
984-
error: EnvironmentAuthorizationError,
984+
error: Schema.Union([EnvironmentAuthorizationError, UsageReadError]),
985985
});
986986

987987
const WsServerSignalProcessRpc = Rpc.make(WS_METHODS.serverSignalProcess, {

0 commit comments

Comments
 (0)