ClaudeBar icon indicating copy to clipboard operation
ClaudeBar copied to clipboard

Daily Usage cost & token cards overcount ~4x: streaming/duplicate JSONL entries not deduplicated by message.id + requestId

Open joelmlevin opened this issue 5 months ago • 2 comments

App version: 0.4.63 (Homebrew cask) · macOS · Claude Max

Summary

The Daily Usage cards (Cost Usage / Token Usage) sum every usage-bearing line in ~/.claude/projects/**/*.jsonl. Claude Code writes multiple JSONL entries per API response — one per content block while streaming (thinking / text / tool_use), plus duplicate copies when a session is resumed or branched — and each entry repeats the full message.usage. Because the DailyUsage scan doesn't deduplicate, the displayed cost and token totals are inflated several-fold (~4× on my machine).

Evidence (one machine, one day)

Parsing the exact files the app reads:

  • 231 distinct assistant responses produced 926 usage-bearing lines → 4.01× duplication.
  • Every line collapses cleanly by (message.id, requestId) — all duplicates of a given message share one requestId.
  • Example: a single response was logged 14× with byte-identical usage — 175,202 tokens (billed once) summed as 2,452,828.
  • The same response also recurs across copied/resumed session files (identical entries observed in two different session .jsonl files).

Cross-checked against the app's own behavior (from ~/Library/Logs/ClaudeBar/ClaudeBar.log):

  • The DailyUsage probe logs found 877 token records for the day — i.e. it counts ~raw lines (877), not the 231 real responses.
  • The Cost card showed ~$179 for the day; the deduplicated total is ~4× lower (~$48).
  • ~98% of those tokens are cache reads, so the duplication multiplies an already-large cache-read figure — the inflation is most visible on cache-heavy days.

Root cause

DailyUsage (added in v0.4.43, scanning ~/.claude/projects/) sums token counts per JSONL line. The CHANGELOG shows token math was tuned in v0.4.60 (cache tokens now included), but no deduplication step was ever added.

This is a known bug class

  • ccusage dedupes by message.id + requestId for exactly this reason — ryoppippi/ccusage#389.
  • anthropics/claude-code #6805: duplicate stream-json usage → "3–8× more than actual usage."
  • matt1398/claude-devtools #74: "sums every entry without deduplication."
  • Anthropic's Agent SDK cost-tracking docs: "multiple messages share the same id with identical usage data. Track which IDs you've already counted and skip duplicates to avoid inflated totals."

Suggested fix

Before summing, group usage records by (message.id, requestId) and keep a single entry per group — the final / most-complete one (streaming entries accumulate output_tokens, so last-wins avoids both over- and under-counting). This matches ccusage's approach and should bring the Cost/Token cards in line with Claude Code's own /cost.

joelmlevin avatar Jun 08 '26 19:06 joelmlevin

@joelmlevin pls take latest version and check if works for you

hanrw avatar Jun 10 '26 07:06 hanrw

Since v0.4.68 (3a5e737), Daily Usage records are deduplicated by (message.id, requestId), keeping the most complete entry, as you suggested; the log now prints raw vs. deduplicated counts. Please reopen if totals still disagree with /cost on a current release.

hanrw avatar Sep 24 '26 01:09 hanrw