fix(ccusage): Fix underreporting output tokens by jstasiak · Pull Request #835 · ccusage/ccusage · GitHub
Skip to content

fix(ccusage): Fix underreporting output tokens#835

Closed
jstasiak wants to merge 1 commit into
ccusage:mainfrom
jstasiak:fix-output-tokens
Closed

fix(ccusage): Fix underreporting output tokens#835
jstasiak wants to merge 1 commit into
ccusage:mainfrom
jstasiak:fix-output-tokens

Conversation

@jstasiak

@jstasiak jstasiak commented Feb 1, 2026

Copy link
Copy Markdown

Claude Code writes multiple JSONL entries per API response during streaming. Each entry shares the same messageId:requestId hash, but output_tokens accumulates incrementally, starting near 0 and reaching the final count when the response completes.

The old dedup logic kept the first entry per hash, resulting in using low output_tokens values for some responses. The impact of that varied.

In my Claude Code sessions sometimes there was no difference at all (presumably because of no incremental response streaming), sometimes it made ~15% difference (in cost terms) in a given 5-hour block.


Review note: I'm not sure the change to the should deduplicate entries across sessions test makes sense and matches the goals of the project. I figure we'll have a problem regardless of the way we assign tokens (session 1 vs session 2)? Is there something more sophisticated we can do here?

Summary by CodeRabbit

  • Bug Fixes
    • Improved accuracy of streaming output token counts in usage data by ensuring the most complete entry state is retained during processing.

✏️ Tip: You can customize this high-level summary in your review settings.

Claude Code writes multiple JSONL entries per API response during
streaming. Each entry shares the same messageId:requestId hash, but
output_tokens accumulates incrementally, starting near 0 and reaching
the final count when the response completes.

The old dedup logic kept the first entry per hash, resulting in
using low output_tokens values for some responses. The impact of that
varied.

In my Claude Code sessions sometimes there was no difference at all
(presumably because of no incremental response streaming), sometimes it
made ~15% difference (in cost terms) in a given 5-hour block.
@coderabbitai

coderabbitai Bot commented Feb 1, 2026

Copy link
Copy Markdown

@jstasiak

jstasiak commented Feb 4, 2026

Copy link
Copy Markdown
Author

It's more than likely that this fix, while being an improvement, is incomplete: #705 (comment)

@ryoppippi

Copy link
Copy Markdown
Member

@ryoppippi ryoppippi closed this May 17, 2026
@jstasiak jstasiak deleted the fix-output-tokens branch May 17, 2026 11:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants