Skip to content

Commit 235e380

Browse files
committed
fix(agency): UsageTotals carries + accumulates cache-token fields
Final unswept accumulator surface. UsageTotals (the agency() API's internal helper type) matched the same pattern as the pre-fix generateObject / streamText / strategies / UsageLedger / ITokenUsage types — promptTokens + completionTokens + totalTokens + costUSD but no cacheReadTokens / cacheCreationTokens. normalizeUsage dropped cache fields from the source usage; addUsageTotals never summed them even when callers routed cache-token-reporting usage objects through. With this fix, every AgentOS accumulator I could find in src/ forwards Anthropic prompt-cache counters end-to-end. Consumers using the public agency() API now see cache-aware totals with the same undefined-vs-zero convention as the rest of the surface. No behavior change for non-cache paths. Typecheck clean.
1 parent b986b94 commit 235e380

1 file changed

Lines changed: 14 additions & 0 deletions

File tree

src/api/agency.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -977,6 +977,10 @@ type UsageTotals = {
977977
completionTokens: number;
978978
totalTokens: number;
979979
costUSD?: number;
980+
/** Anthropic `cache_read_input_tokens`. Undefined when no source reported. */
981+
cacheReadTokens?: number;
982+
/** Anthropic `cache_creation_input_tokens`. Same undefined convention. */
983+
cacheCreationTokens?: number;
980984
};
981985

982986
function emptyUsageTotals(): UsageTotals {
@@ -990,6 +994,10 @@ function normalizeUsage(raw: unknown): UsageTotals {
990994
completionTokens: usage.completionTokens ?? 0,
991995
totalTokens: usage.totalTokens ?? 0,
992996
costUSD: usage.costUSD,
997+
// Preserve cache-token fields from the source. Undefined stays
998+
// undefined so consumers distinguish "not reported" from "zero".
999+
cacheReadTokens: usage.cacheReadTokens,
1000+
cacheCreationTokens: usage.cacheCreationTokens,
9931001
};
9941002
}
9951003

@@ -1000,6 +1008,12 @@ function addUsageTotals(target: UsageTotals, usage: UsageTotals): void {
10001008
if (typeof usage.costUSD === 'number') {
10011009
target.costUSD = (target.costUSD ?? 0) + usage.costUSD;
10021010
}
1011+
if (typeof usage.cacheReadTokens === 'number') {
1012+
target.cacheReadTokens = (target.cacheReadTokens ?? 0) + usage.cacheReadTokens;
1013+
}
1014+
if (typeof usage.cacheCreationTokens === 'number') {
1015+
target.cacheCreationTokens = (target.cacheCreationTokens ?? 0) + usage.cacheCreationTokens;
1016+
}
10031017
}
10041018

10051019
function getSessionUsage(

0 commit comments

Comments
 (0)