Repository navigation
SWF(bug): exclude cached tokens from Anthropic input_tokens - #74
Merged
maplepaladin73 merged 1 commit intoNov 23, 2026
Merged
Conversation
Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
|
I'll fix CI failures and address comments from users with write access. I'll skip comments containing "(aside)".
|
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.
Summary
Anthropic
/v1/messagesusage now reportsinput_tokenswithout prefix-cache hits, in both the blocking response and the streamingmessage_delta. A 10-token prompt with 3 cached tokens now reportsinput_tokens: 7, cache_read_input_tokens: 3. Before, it reported10and3, 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'sprompt_tokens(which includes cache hits) straight intoinput_tokensand also reportedcached_prompt_tokensascache_read_input_tokens.anthropic/translate.rs,input_tokens: ir.usage.prompt_tokens.anthropic/translator.rssavedusage.prompt_tokensonFinishand emitted it infinal_usage().The OpenAI encoders are correct as they are: OpenAI's
prompt_tokensincludescached_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 renamedprompt_tokens→input_tokensto match what it now holds.Test plan
Regression tests: the existing blocking assertion in
ir_carry.rs(which encoded the bug as10) now expects7, and a new streaming testfinal_usage_excludes_cache_reads_from_input_tokenscovers the stream. Onmain(fix stashed, tests kept), both fail:With the fix:
Try it:
ATLAS_SKIP_BUILD=1 CUDARC_CUDA_VERSION=13000 cargo test -p spark-server --bin spark anthropic::testscargo fmt --all -- --checkATLAS_SKIP_BUILD=1 CUDARC_CUDA_VERSION=13000 cargo clippy --workspace --tests(clean)bash scripts/check-license-headers.sh(0 invalid) andtypos(clean)Notes for reviewers
message_start.usage.input_tokensis unchanged: it is still0, because usage is unknown until the terminal delta. The corrected value arrives inmessage_delta, which clients merge over it.cache_creation_input_tokens. Prefix-cache writes stay insideinput_tokens, which matches Anthropic when there is no explicit cache-control write./v1/messages/count_tokensis unaffected because it counts the full prompt and has no cache concept.Implemented in session Exclude cached tokens from Anthropic input_tokens
Review with Devin · Open in Devin Desktop