fix(models-report): stop double-counting cache reads (#447)

cacheReadInputTokens (Anthropic) and cachedInputTokens (OpenAI) name the
same tokens. ~11 providers populate both with the same value, so summing
them doubled the cache-read token figure in the models report. Take the
max instead. Cost is unaffected (calculateCost never used the sum).
This commit is contained in:
Resham Joshi 2026-06-06 03:14:51 +02:00 • committed by GitHub
parent cef1a6b669
commit 77340e52b8
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 16 additions and 1 deletions

View file

@ -89,7 +89,11 @@ export async function aggregateModels(projects: ProjectSummary[], opts: Aggregat
bucket.inputTokens += call.usage.inputTokens
bucket.outputTokens += call.usage.outputTokens + call.usage.reasoningTokens
bucket.cacheWriteTokens += call.usage.cacheCreationInputTokens
bucket.cacheReadTokens += call.usage.cacheReadInputTokens + call.usage.cachedInputTokens
// cacheReadInputTokens (Anthropic vocab) and cachedInputTokens (OpenAI vocab)
// are two names for the same thing. Providers populate one or set both to the
// same value, so take the max — summing double-counts cache reads for every
// provider that fills both fields.
bucket.cacheReadTokens += Math.max(call.usage.cacheReadInputTokens, call.usage.cachedInputTokens)
bucket.costUSD += call.costUSD
bucket.calls += 1

View file

@ -125,6 +125,17 @@ describe('aggregateModels', () => {
expect(claudeRow.totalTokens).toBe(1800 + 300 + 800 + 13000)
})
it('does not double-count cache reads when a provider sets both cache fields', async () => {
// Providers like codex/mux/codebuff populate cacheReadInputTokens AND
// cachedInputTokens with the same value (Anthropic vs OpenAI vocabulary for
// the same tokens). The report must count them once, not sum them.
const call = makeCall({ provider: 'mux', model: 'claude-opus-4-8', input: 100, output: 50, cacheRead: 4000, costUSD: 2.0 })
call.usage.cachedInputTokens = 4000 // mirrors cacheReadInputTokens, as those providers do
const rows = await aggregateModels([makeProject([makeTurn('feature', [call])])])
expect(rows[0]!.cacheReadTokens).toBe(4000) // not 8000
})
it('reports the dominant task type with its cost share in default mode', async () => {
const project = makeProject([
makeTurn('feature', [makeCall({ provider: 'claude', model: 'claude-sonnet-4-6', costUSD: 6.0, input: 100, output: 20 })]),