fix(stats): include Anthropic cache tokens in usage rankings token_used - #5384
fix(stats): include Anthropic cache tokens in usage rankings token_used#5384broven wants to merge 1 commit into
Conversation
The rankings / quota_data export records token_used as prompt_tokens + completion_tokens. For OpenAI-style usage this is the full volume because prompt_tokens already includes cache-read tokens. For Anthropic-style usage, cache-read and cache-creation tokens are reported separately and are NOT part of prompt_tokens, so they were dropped from token_used entirely. Cache-heavy Claude traffic is then massively undercounted in the leaderboard (observed: a model whose real 24h volume was ~145M tokens, 98%+ cache, showed under 2M in the rankings) while its quota stayed correct. Compute token_used in a provider-aware helper: add cache-read and cache-creation tokens back only for anthropic usage semantics, where they are excluded from prompt_tokens. OpenAI-style usage is unchanged. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> Co-authored-by: multica-agent <github@multica.ai>
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (2)
WalkthroughThe PR refactors token accounting for quota logging to separately compute token totals via a new ChangesToken Usage Accounting for Cache-Aware Logging
Estimated code review effort🎯 2 (Simple) | ⏱️ ~12 minutes Possibly related PRs
Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
|
Independent confirmation of this from another deployment, plus one correction worth folding in before merge. Same root cause, same magnitude. On new-api
The dashboard and rankings showed 0.52% of real volume — close to the 1.4% reported here. OpenAI-semantic models on the same instance (deepseek et al.) matched exactly, isolating it to the Anthropic separate-cache accounting. Billing/ Correction —
{
name: "anthropic cache_write_tokens alias must not double count",
params: RecordConsumeLogParams{
PromptTokens: 2, CompletionTokens: 535,
Other: map[string]interface{}{
"usage_semantic": "anthropic",
"cache_tokens": 28097,
"cache_creation_tokens": 281100,
"cache_write_tokens": 281100, // alias of cache_creation_tokens
"cache_creation_tokens_5m": 281100, // ditto
},
},
want: 2 + 535 + 28097 + 281100,
},Also worth noting Rebase note. This no longer applies cleanly: Adapted this onto rc.24 and it has been running in production since 2026-08-28 — new |
Problem
The usage rankings /
quota_dataexport recordstoken_usedasprompt_tokens + completion_tokens(model/log.go, inRecordConsumeLog).prompt_tokensalready includes cache-read tokens, soprompt_tokens + completion_tokensis the full token volume. ✅cache_tokens) and cache-creation (cache_creation_tokens) are reported separately and are not part ofprompt_tokens. They were therefore dropped fromtoken_usedentirely. ❌For cache-heavy Claude traffic, cache tokens dominate, so the model is massively undercounted in the leaderboard even though its
quota(billing) is computed correctly from the cache tokens + ratios.Observed (production, 24h, one Claude Opus model)
token_used(prompt+completion)The leaderboard showed ~1.4% of the real token volume. OpenAI models on the same instance matched their downstream stats exactly, which isolated the issue to the Anthropic-only separate cache accounting.
Fix
Compute
token_usedin a small provider-aware helperstatsTokenUsage:anthropicusage semantics (where they are excluded fromprompt_tokens);The discriminator is
other["usage_semantic"] == "anthropic", which is set in exactly the same place the cache tokens are excluded fromprompt_tokens, so the two stay in sync. The cache counts are read from the existingotherpayload, so no call-site or struct-signature changes are needed.This only affects the statistics token count;
quota/ billing is untouched. Historicalquota_datarows are not rewritten by this change.Tests
Added
model/log_stats_test.gocovering OpenAI (no double-count), Anthropic with/without cache tokens (including the real opus-4-8 shape),float64payloads, and nilother.🤖 Generated with Claude Code
Summary by CodeRabbit
Release Notes
Bug Fixes
Tests