Skip to content

SWF(bug): exclude cached tokens from Anthropic input_tokens - #74

Merged
maplepaladin73 merged 1 commit into
mainfrom
devin/1795397382-anthropic-uncached-input-tokens
Nov 23, 2026
Merged

maplepaladin73 merged 1 commit into
mainfrom
devin/1795397382-anthropic-uncached-input-tokens

Conversation

@maplepaladin73

@maplepaladin73 maplepaladin73 commented Nov 23, 2026 •

Copy link
Copy Markdown
Owner

Summary

Anthropic /v1/messages usage now reports input_tokens without prefix-cache hits, in both the blocking response and the streaming message_delta. A 10-token prompt with 3 cached tokens now reports input_tokens: 7, cache_read_input_tokens: 3. Before, it reported 10 and 3, so clients that add the two fields counted cached tokens twice.

Root cause: Anthropic's usage fields don't overlap: the total prompt is input_tokens + cache_creation_input_tokens + cache_read_input_tokens. Both Anthropic encoders copied the IR's prompt_tokens (which includes cache hits) straight into input_tokens and also reported cached_prompt_tokens as cache_read_input_tokens.

  • Blocking: anthropic/translate.rs, input_tokens: ir.usage.prompt_tokens.
  • Streaming: anthropic/translator.rs saved usage.prompt_tokens on Finish and emitted it in final_usage().

The OpenAI encoders are correct as they are: OpenAI's prompt_tokens includes cached_tokens.

Fix: new ir::Usage::uncached_prompt_tokens() (prompt_tokens.saturating_sub(cached_prompt_tokens)) is the single place this is computed, and both Anthropic paths use it. The stream translator's field is renamed prompt_tokens → input_tokens to match what it now holds.

Test plan

Regression tests: the existing blocking assertion in ir_carry.rs (which encoded the bug as 10) now expects 7, and a new streaming test final_usage_excludes_cache_reads_from_input_tokens covers the stream. On main (fix stashed, tests kept), both fail:

test anthropic::tests::ir_carry::response_maps_reasoning_text_tools_and_usage ... FAILED
test anthropic::tests::translator_stream::final_usage_excludes_cache_reads_from_input_tokens ... FAILED
  panicked at crates/spark-server/src/anthropic/tests/ir_carry.rs:373:5:
  left: Number(10)
 right: 7
  panicked at crates/spark-server/src/anthropic/tests/translator_stream.rs:92:5:
  left: Number(10)
 right: 7
test result: FAILED. 43 passed; 2 failed; 0 ignored; 0 measured; 3222 filtered out

With the fix:

test anthropic::tests::ir_carry::response_maps_reasoning_text_tools_and_usage ... ok
test anthropic::tests::translator_stream::final_usage_excludes_cache_reads_from_input_tokens ... ok
anthropic::tests     test result: ok. 45 passed; 0 failed
full --bin spark     test result: ok. 3253 passed; 0 failed; 14 ignored

Try it:

ATLAS_SKIP_BUILD=1 CUDARC_CUDA_VERSION=13000 cargo test -p spark-server --bin spark anthropic::tests
  • cargo fmt --all -- --check
  • ATLAS_SKIP_BUILD=1 CUDARC_CUDA_VERSION=13000 cargo clippy --workspace --tests (clean)
  • bash scripts/check-license-headers.sh (0 invalid) and typos (clean)
  • Tested against a real model / hardware: not done. This was CPU only, so there was no live HTTP round trip with a real prefix-cache hit.
  • Added/updated tests

Notes for reviewers

  • message_start.usage.input_tokens is unchanged: it is still 0, because usage is unknown until the terminal delta. The corrected value arrives in message_delta, which clients merge over it.
  • Atlas never reports cache_creation_input_tokens. Prefix-cache writes stay inside input_tokens, which matches Anthropic when there is no explicit cache-control write.
  • /v1/messages/count_tokens is unaffected because it counts the full prompt and has no cache concept.
  • Untested: live serving with a real model and a real cache hit.

Software Factory opened this PR for Robert
Implemented in session Exclude cached tokens from Anthropic input_tokens
Review with Devin · Open in Devin Desktop


Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
@devin-ai-integration

Copy link
Copy Markdown

I'll fix CI failures and address comments from users with write access. I'll skip comments containing "(aside)".

  • Disable automatic comment, CI, and merge conflict monitoring

@devin-ai-integration devin-ai-integration Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ Devin Review: No Issues Found

Devin Review analyzed this PR and found no bugs or issues to report.

Devin Review

@maplepaladin73
maplepaladin73 merged commit 1cf00b5 into main Nov 23, 2026
1 check passed
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