[BUG] Incorrect context token count in Claude Code when first chunk contains empty usage info
Preflight Checklist
- [x] I have searched existing issues and this hasn't been reported yet
- [x] This is a single bug report (please file separate reports for different bugs)
- [x] I am using the latest version of Claude Code
What's Wrong?
The /context command consistently reports 0 tokens (or incorrect counts) if the initial data chunks of a streaming response contain empty or zeroed-out usage statistics. It appears the system captures the metadata from the start of the stream but fails to update it when the final, corrected statistics arrive at the end of the stream.
What Should Happen?
The internal counter should be reactive or cumulative. Specifically:
It should update the session's context state whenever a chunk containing a higher or non-zero token count is received.
The final metadata chunk in a stream should always take precedence over initial placeholder values.
Error Messages/Logs
Steps to Reproduce
The Stream Start: The server begins sending response chunks. The very first chunk includes a metadata/usage object where the input/prompt count is initialized to 0.
The Stream End: The final chunk arrives, containing the correct and cumulative token counts (e.g., input_tokens: 1024, output_tokens: 50).
The Result:
During and after the chat, the UI or the /context command displays the metadata from the initial chunk (0 tokens).
The correct metadata from the final chunk is seemingly ignored or overwritten by the earlier zeroed data.
Claude Model
None
Is this a regression?
I don't know
Last Working Version
_No response_
Claude Code Version
2.1.29
Platform
Anthropic API
Operating System
Other Linux
Terminal/Shell
VS Code integrated terminal
Additional Information
The client-side logic likely initializes the context state once upon receiving the first metadata object and does not implement an "update or overwrite" strategy for subsequent metadata frames in the same stream.
This issue has 4 comments on GitHub. Read the full discussion on GitHub ↗