Skip to content

fix(usage): include cached input in prompt_tokens on the non-stream path - #15

Merged
chefboyrdave21 merged 1 commit into
mainfrom
fix/prompt-tokens-include-cache
Oct 11, 2026
Merged

chefboyrdave21 merged 1 commit into
mainfrom
fix/prompt-tokens-include-cache

Conversation

@chefboyrdave21

Copy link
Copy Markdown
Owner

What is wrong

claude --print leans on prompt caching, so almost all of a real prompt lands in the cache fields while bare input_tokens stays tiny. Measured on 2026-10-11 for a 13.5k-char prompt:

cache_creation_input_tokens: 37,795
cache_read_input_tokens:     10,468
input_tokens:                     2
                             -------
summed:                      48,265

The tool/streaming path (server.py:1028) already summed all three. The plain non-stream reply (server.py:1149) read only input_tokens, so it reported prompt_tokens: 2 for 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

  • New test_prompt_tokens.py: 4 tests, including the exact production usage shape. Fails against the old code, passes with the fix.
  • Existing suite: 51 passed, 23 subtests passed.

🤖 Generated with Claude Code

https://claude.ai/code/session_015WUuTHQAtRufsDNYzLAMJk

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.
@chefboyrdave21
chefboyrdave21 merged commit a8dab3f into main Oct 11, 2026
8 checks passed
@chefboyrdave21
chefboyrdave21 deleted the fix/prompt-tokens-include-cache branch October 11, 2026 21:15
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.

1 participant