[BUG] Streaming output tokens are incomplete in session.jsonl compared to /cost
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?
I noticed a discrepancy between the token usage shown by the /cost command and the data written to .Claude/project/session.jsonl when using streaming.
When I inspect the session.jsonl file, the recorded output_tokens are consistently lower than the actual usage reported by /cost for the same session.
It appears that the final chunk of data from messages_delta is never written to the JSONL file.
From reviewing the Anthropic npm SDK source, it looks like the session data is written only once—right after the assistant message is first created—and is not updated afterward when subsequent messages_delta streaming events arrive.
As a result, the file captures only a partial output token count, not the final aggregated total.
This causes:
Incorrect output token accounting in session.jsonl
Mismatch with /cost output
Underreported streaming usage
What Should Happen?
Expected behavior:
The session file should be updated after the final streaming delta so that output_tokens reflects the full assistant response.
Actual behavior:
Only the initial partial token count is persisted; later deltas are ignored.
Error Messages/Logs
Steps to Reproduce
- Send request via Claude Code CLI and enable streaming responses.
- Run a prompt that produces a long, multi-chunk streamed response (so that multiple
messages_deltaevents are emitted).
- After the assistant finishes, run the
/costcommand in Claude Code and note the reportedoutput_tokens.
- Open the file:
````
.Claude/project/session.jsonl
- Locate the entry for the same assistant message.
- Compare the
output_tokensvalue in the JSONL file with the value shown by/cost.
---
Observed Result
The output_tokens in session.jsonl is lower than the value reported by /cost.
Expected Result
Both values should match, and session.jsonl should contain the final aggregated token count after all messages_delta streaming events are processed.
Claude Model
Sonnet (default)
Is this a regression?
I don't know
Last Working Version
_No response_
Claude Code Version
2.1.25
Platform
Anthropic API
Operating System
Ubuntu/Debian Linux
Terminal/Shell
Other
Additional Information
_No response_
This issue has 7 comments on GitHub. Read the full discussion on GitHub ↗