Skip to content

Commit c969d0d

Browse files
authored
feat(usage): show the cached subset beside every token total (lidge-jun#4421)
A cached request's total is mostly cache. A 58,000-token prompt that is 57,000 cache read and 1,000 fresh was printed as a bare 58,000 on every surface except the logs table row, so it read as a different, smaller request than the row directly beside it. formatTokensWithCache renders the total with its cached companion — 5.8만 c5.7만, 58K c57K — reusing the marker the logs.tokens.cacheRead label already documents as "cache read (c)", so no new i18n key is needed. A provider that reports no cache is untouched, and a turn served entirely from cache still shows the marker, since that is the row worth seeing. No backend change: /api/logs forwards the whole usage object and /api/usage already emits cache on summary, models and providers. The loss was purely client-side, in the row types and the aggregators, so the fix widens UsageModel, UsageProvider, UsageSummary30d, the CLI CostRow, and summarizeFilteredLogs to keep the fields that were already arriving. The log detail panel is deliberately left alone: it already has separate cache read and cache write cells, and stacking the companion onto its total would duplicate them.
1 parent fedcb61 commit c969d0d

11 files changed

Lines changed: 152 additions & 10 deletions

File tree

18.5 KB
Loading

‎gui/src/format-tokens.ts‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,24 @@ export function formatTokens(n: number, locale: string): string {
3232
if (n < 1_000_000_000_000) return `${trim((n / 1_000_000_000).toFixed(1))}B`;
3333
return `${trim((n / 1_000_000_000_000).toFixed(1))}T`;
3434
}
35+
36+
/**
37+
* A token total with its cached subset beside it: `5.8만 c5.7만`, `58K c57K`.
38+
*
39+
* A cached request's total is mostly cache. Printing the total alone makes a
40+
* 58,000-token prompt that is 57,000 cache read and 1,000 fresh look like an
41+
* ordinary 58,000-token prompt, and it reads as a different, smaller request
42+
* than the log row directly below it, which already shows the companion. The
43+
* `c` marker matches the `logs.tokens.cacheRead` label, which reads
44+
* "cache read (c)".
45+
*
46+
* The companion is omitted only when there is no cache to report. It is NOT
47+
* omitted when the cached subset equals the total: a turn served entirely from
48+
* cache is the most interesting row on the page, and hiding its marker there
49+
* would blank exactly the case this exists for.
50+
*/
51+
export function formatTokensWithCache(total: number, cached: number | undefined, locale: string): string {
52+
const base = formatTokens(total, locale);
53+
if (cached === undefined || !Number.isFinite(cached) || cached <= 0) return base;
54+
return `${base} c${formatTokens(cached, locale)}`;
55+
}

‎gui/src/pages/Logs.tsx‎

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { useVirtualizer } from "@tanstack/react-virtual";
33
import { useI18n, LOCALES, type TFn } from "../i18n/shared";
44
import { formatProviderDisplayName } from "../provider-icons";
55
import { formatTokens } from "../format-tokens";
6+
import { formatTokensWithCache } from "../format-tokens";
67
import { hashLogConversationQuery } from "../log-conversation-id";
78
import { statusCodeInfo } from "../status-codes";
89
import { IconX } from "../icons";
@@ -363,19 +364,27 @@ function formatLogDateTime(ts: number, localeTag?: string, timeZone?: string): s
363364
function summarizeFilteredLogs(entries: LogEntry[]): {
364365
requests: number;
365366
totalTokens: number;
367+
cachedInputTokens: number;
366368
estimatedCostUsd: number;
367369
priorityLowerBound: boolean;
368370
unpricedRequests: number;
369371
unmeteredRequests: number;
370372
} {
371373
let totalTokens = 0;
374+
let cachedInputTokens = 0;
372375
for (const entry of entries) {
373376
const tokens = displayTokenTotal(entry);
374377
if (tokens !== undefined) totalTokens += tokens;
378+
// The banner sits directly above rows that already print `c <read>`, so a
379+
// total with no companion read as a different, smaller figure than the rows
380+
// it summarizes.
381+
const read = cacheSplit(entry).read;
382+
if (read !== undefined && read > 0) cachedInputTokens += read;
375383
}
376384
return {
377385
requests: entries.length,
378386
totalTokens,
387+
cachedInputTokens,
379388
...summarizeEstimatedCosts(entries),
380389
};
381390
}
@@ -687,7 +696,11 @@ export default function Logs({ apiBase }: { apiBase: string }) {
687696
<Notice tone="ok">
688697
{t("logs.conversation.totals", {
689698
requests: conversationTotals.requests,
690-
tokens: formatTokens(conversationTotals.totalTokens, localeTag ?? locale),
699+
tokens: formatTokensWithCache(
700+
conversationTotals.totalTokens,
701+
conversationTotals.cachedInputTokens,
702+
localeTag ?? locale,
703+
),
691704
cost: formatEstimatedUsdValue(
692705
conversationTotals.estimatedCostUsd,
693706
t,

‎gui/src/pages/Usage.tsx‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { useCallback, useEffect, useMemo, useRef, useState, type ReactNode } from "react";
22
import { useI18n, type TFn, type Locale } from "../i18n/shared";
33
import { formatProviderDisplayName } from "../provider-icons";
4-
import { formatTokens } from "../format-tokens";
4+
import { formatTokens, formatTokensWithCache } from "../format-tokens";
55
import { formatEstimatedUsdValue as formatUsdEstimate } from "../intl-formatters";
66
import { readSessionListCache, writeSessionListCache } from "../session-list-cache";
77
import { EmptyState, Notice } from "../ui";
@@ -64,6 +64,10 @@ interface UsageModel {
6464
totalTokens: number;
6565
inputTokens: number;
6666
outputTokens: number;
67+
// /api/usage has carried these all along; dropping them from the row type is
68+
// what left the token column without its cached companion.
69+
cachedInputTokens?: number;
70+
cacheReadInputTokens?: number;
6771
shareRatio: number;
6872
}
6973

@@ -74,6 +78,8 @@ interface UsageProvider {
7478
reportedRequests: number;
7579
estimatedRequests: number;
7680
totalTokens: number;
81+
cachedInputTokens?: number;
82+
cacheReadInputTokens?: number;
7783
shareRatio: number;
7884
}
7985

@@ -561,7 +567,7 @@ function UsageModelsTable({
561567
<td className="muted">{formatProviderDisplayName(model.provider, t)}</td>
562568
<td className="num">{model.requests}</td>
563569
<td className="num">{model.measuredRequests}</td>
564-
<td className="num mono">{formatTokens(model.totalTokens, locale)}</td>
570+
<td className="num mono">{formatTokensWithCache(model.totalTokens, model.cacheReadInputTokens ?? model.cachedInputTokens, locale)}</td>
565571
<td><div className="usage-bar"><div className="usage-bar-fill" style={{ width: `${Math.round(model.shareRatio * 100)}%` }} /></div></td>
566572
</tr>
567573
))}
@@ -621,7 +627,7 @@ function UsageProvidersTable({
621627
<td className="mono">{formatProviderDisplayName(provider.provider, t)}</td>
622628
<td className="num">{provider.requests}</td>
623629
<td className="num">{provider.measuredRequests}</td>
624-
<td className="num mono">{formatTokens(provider.totalTokens, locale)}</td>
630+
<td className="num mono">{formatTokensWithCache(provider.totalTokens, provider.cacheReadInputTokens ?? provider.cachedInputTokens, locale)}</td>
625631
<td><div className="usage-bar"><div className="usage-bar-fill" style={{ width: `${Math.round(provider.shareRatio * 100)}%` }} /></div></td>
626632
</tr>
627633
))}

‎gui/src/pages/dashboard-overview-head.tsx‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { IconAlert, IconInfo } from "../icons";
22
import { type TKey, useT } from "../i18n/shared";
3-
import { formatTokens } from "../format-tokens";
3+
import { formatTokensWithCache } from "../format-tokens";
44
import { formatUptime } from "../formatUptime";
55
import { navigateHash } from "../hash-routing";
66
import type { useDashboardData } from "./use-dashboard-data";
@@ -81,7 +81,13 @@ export function DashboardOverviewHead({
8181
<div className="stat" aria-busy={healthLoading || undefined}><div className="label">{t("dash.providers")}</div><div className="value">{providers.length}</div></div>
8282
<div className="stat" aria-busy={usageLoading || undefined}>
8383
<div className="label">{t("dash.tokens30d")}</div>
84-
<div className="value mono">{usage30d && usage30d.summary.requests > 0 ? formatTokens(usage30d.summary.totalTokens, locale) : "—"}</div>
84+
<div className="value mono">{usage30d && usage30d.summary.requests > 0
85+
? formatTokensWithCache(
86+
usage30d.summary.totalTokens,
87+
usage30d.summary.cacheReadInputTokens ?? usage30d.summary.cachedInputTokens,
88+
locale,
89+
)
90+
: "—"}</div>
8591
<div className="muted text-label dash-stat-coverage">
8692
{usage30d && usage30d.summary.requests > 0
8793
? t("dash.coverage").replace("{pct}", `${Math.round(usage30d.summary.coverageRatio * 100)}%`)

‎gui/src/pages/dashboard-shared.ts‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,17 @@ export interface SidecarPatch {
123123
};
124124
}
125125
export interface ShadowCallData { enabled: boolean; model: string; sourceModels?: string[] }
126-
export interface UsageSummary30d { summary: { requests: number; totalTokens: number; coverageRatio: number } }
126+
export interface UsageSummary30d {
127+
summary: {
128+
requests: number;
129+
totalTokens: number;
130+
coverageRatio: number;
131+
// Already on /api/usage; the tile showed a bare total only because this
132+
// type dropped them.
133+
cachedInputTokens?: number;
134+
cacheReadInputTokens?: number;
135+
};
136+
}
127137
export type UpdateChannel = "latest" | "preview";
128138
export type Installer = "npm" | "bun" | "source";
129139
export type UpdateJobStatus = "running" | "restarting" | "succeeded" | "failed";

‎scripts/test-layout/layout.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -718,6 +718,7 @@
718718
"grok-sync.test.ts": "providers/xai",
719719
"grok-writer-boundary.test.ts": "providers/xai",
720720
"gui-api-error.test.ts": "gui",
721+
"gui-format-tokens-cache.test.ts": "gui",
721722
"gui-management-session.test.ts": "gui",
722723
"gui-pair-capability.test.ts": "gui",
723724
"gui-pair-client.test.ts": "gui",

‎src/cli/usage-report.ts‎

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,13 @@ interface CostRow {
1717
model?: string;
1818
requests: number;
1919
totalTokens: number;
20+
/**
21+
* Cache-read subset of the row's tokens. The API sends it; the CLI dropped it,
22+
* so a mostly-cached provider's TOKENS column read as an ordinary total while
23+
* the summary line two rows above already said `cached N`.
24+
*/
25+
cachedInputTokens?: number;
26+
cacheReadInputTokens?: number;
2027
estimatedCostUsd?: number;
2128
}
2229

@@ -53,6 +60,8 @@ interface UsageReportInput {
5360
ambiguous?: boolean;
5461
requests: number;
5562
totalTokens: number;
63+
cachedInputTokens?: number;
64+
cacheReadInputTokens?: number;
5665
estimatedCostUsd?: number;
5766
}[];
5867
}
@@ -76,6 +85,16 @@ function count(value: number | undefined): string {
7685
return typeof value === "number" && Number.isFinite(value) ? value.toLocaleString("en-US") : "—";
7786
}
7887

88+
/**
89+
* A row's token total with its cache-read subset, matching the summary line's
90+
* `cached N` wording rather than inventing a second vocabulary for the tables.
91+
*/
92+
function countWithCache(total: number | undefined, cached: number | undefined): string {
93+
const base = count(total);
94+
if (typeof cached !== "number" || !Number.isFinite(cached) || cached <= 0) return base;
95+
return `${base} (cached ${count(cached)})`;
96+
}
97+
7998
/**
8099
* Matches the dashboard's `~$` with four fraction digits. Estimates below a
81100
* hundredth of a cent still read as a number rather than collapsing to $0.00,
@@ -141,7 +160,12 @@ export function formatUsageReport(data: UsageReportInput): string[] {
141160
lines.push("");
142161
lines.push(...table(
143162
["PROVIDER", "REQUESTS", "TOKENS", "EST. COST"],
144-
providers.map(row => [row.provider, count(row.requests), count(row.totalTokens), usd(row.estimatedCostUsd)]),
163+
providers.map(row => [
164+
row.provider,
165+
count(row.requests),
166+
countWithCache(row.totalTokens, row.cacheReadInputTokens ?? row.cachedInputTokens),
167+
usd(row.estimatedCostUsd),
168+
]),
145169
));
146170
}
147171

@@ -163,7 +187,7 @@ export function formatUsageReport(data: UsageReportInput): string[] {
163187
// wrong conclusion. Mark it rather than presenting it as a single identity.
164188
row.ambiguous ? `${terminalText(row.accountLogLabel)} (ambiguous)` : terminalText(row.accountLogLabel),
165189
count(row.requests),
166-
count(row.totalTokens),
190+
countWithCache(row.totalTokens, row.cacheReadInputTokens ?? row.cachedInputTokens),
167191
usd(row.estimatedCostUsd),
168192
]),
169193
));
@@ -175,7 +199,13 @@ export function formatUsageReport(data: UsageReportInput): string[] {
175199
const shown = models.slice(0, MAX_MODEL_ROWS);
176200
lines.push(...table(
177201
["MODEL", "PROVIDER", "REQUESTS", "TOKENS", "EST. COST"],
178-
shown.map(row => [row.model ?? "-", row.provider, count(row.requests), count(row.totalTokens), usd(row.estimatedCostUsd)]),
202+
shown.map(row => [
203+
row.model ?? "-",
204+
row.provider,
205+
count(row.requests),
206+
countWithCache(row.totalTokens, row.cacheReadInputTokens ?? row.cachedInputTokens),
207+
usd(row.estimatedCostUsd),
208+
]),
179209
));
180210
if (models.length > shown.length) {
181211
lines.push(`... ${models.length - shown.length} more (use --json)`);

‎tests/cli/cli-usage-report.test.ts‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -404,3 +404,28 @@ describe("ocx logs --follow output contract", () => {
404404
}
405405
});
406406
});
407+
408+
describe("cached tokens in the per-row tables", () => {
409+
test("a provider and model row name their cache-read subset", () => {
410+
// The summary line has always said "cached N". The tables below it printed a
411+
// bare total, so a mostly-cached provider looked like an ordinary one.
412+
const lines = formatUsageReport({
413+
range: "today",
414+
summary: { requests: 1, totalTokens: 58_000, cachedInputTokens: 57_000 },
415+
providers: [{ provider: "devin-cli", requests: 1, totalTokens: 58_000, cacheReadInputTokens: 57_000 }],
416+
models: [{ provider: "devin-cli", model: "swe-2", requests: 1, totalTokens: 58_000, cachedInputTokens: 57_000 }],
417+
}).join("\n");
418+
expect(lines).toContain("58,000 (cached 57,000)");
419+
expect(lines.match(/cached 57,000/g)?.length).toBeGreaterThanOrEqual(2);
420+
});
421+
422+
test("a provider that reports no cache keeps a bare total", () => {
423+
const lines = formatUsageReport({
424+
range: "today",
425+
summary: { requests: 1, totalTokens: 58_000 },
426+
providers: [{ provider: "xai", requests: 1, totalTokens: 58_000 }],
427+
}).join("\n");
428+
expect(lines).toContain("58,000");
429+
expect(lines).not.toContain("cached");
430+
});
431+
});

‎tests/fixtures/test-layout-expected.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -549,6 +549,7 @@
549549
"grok-sync.test.ts": "providers/xai",
550550
"grok-writer-boundary.test.ts": "providers/xai",
551551
"gui-api-error.test.ts": "gui",
552+
"gui-format-tokens-cache.test.ts": "gui",
552553
"gui-management-session.test.ts": "gui",
553554
"gui-pair-capability.test.ts": "gui",
554555
"gui-pair-client.test.ts": "gui",

0 commit comments

Comments
 (0)