Repository navigation
fix(usage): include cached input in prompt_tokens on the non-stream path - #15
Merged
Merged
Conversation
claude --print leans on prompt caching, so almost all of a real prompt is
reported under cache_creation_input_tokens / cache_read_input_tokens while bare
input_tokens stays tiny. Measured 2026-10-11 for a 13.5k-char prompt:
cache_creation_input_tokens: 37795
cache_read_input_tokens: 10468
input_tokens: 2
-> 48265 total, or 2 reading input_tokens alone
The tool/streaming path already summed all three. The plain non-stream reply
read only input_tokens, so it reported prompt_tokens=2 for every request
regardless of size.
To be clear about what this is NOT: history reaches the model fine. A
181,816-byte, 183-message conversation correctly recalled a codeword planted in
message 1, so nothing was being truncated. The payload was always delivered;
only the accounting was wrong.
It still matters, because clients size their context off usage. Hermes decides
when to compress from token counts, and a number four orders of magnitude too
small makes that decision on fiction.
Both paths now call one prompt_tokens_of() helper so they cannot drift apart
again. Suite: 51 passed, 23 subtests. The new test fails against the old code.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What is wrong
claude --printleans on prompt caching, so almost all of a real prompt lands in the cache fields while bareinput_tokensstays tiny. Measured on 2026-10-11 for a 13.5k-char prompt:The tool/streaming path (
server.py:1028) already summed all three. The plain non-stream reply (server.py:1149) read onlyinput_tokens, so it reportedprompt_tokens: 2for every request regardless of size.What this is NOT
I first suspected history was being dropped before reaching
claude --print. It is not. A 181,816-byte, 183-message conversation correctly recalled a codeword planted in message 1, and a smaller 5-message case did too. Nothing is truncated; the payload is delivered in full. Only the accounting was wrong.Why it still matters
Clients size their context window off
usage. Hermes decides when to compress from token counts, so it was making that call on a number ~24,000x too small. That is a plausible contributor to its compression behaving oddly (observed the same day:messages=55->55,rough_tokens ~52,708 -> ~69,140, then "cannot compress further").Fix
One
prompt_tokens_of()helper, called from both paths so they cannot drift apart again.Verification
test_prompt_tokens.py: 4 tests, including the exact production usage shape. Fails against the old code, passes with the fix.🤖 Generated with Claude Code
https://claude.ai/code/session_015WUuTHQAtRufsDNYzLAMJk