[BUG] Streaming output tokens are incomplete in session.jsonl compared to /cost

Resolved 💬 7 comments Opened Jan 30, 2026 by ainurabek Closed Mar 24, 2026

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

  1. Send request via Claude Code CLI and enable streaming responses.
  1. Run a prompt that produces a long, multi-chunk streamed response (so that multiple messages_delta events are emitted).
  1. After the assistant finishes, run the /cost command in Claude Code and note the reported output_tokens.
  1. Open the file:

``
.Claude/project/session.jsonl
``

  1. Locate the entry for the same assistant message.
  1. Compare the output_tokens value 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_

View original on GitHub ↗

This issue has 7 comments on GitHub. Read the full discussion on GitHub ↗