fix(provider): count Anthropic cache_creation tokens in input usage - #9881
Open
xiaoyuyu6420 wants to merge 1 commit into
Open
fix(provider): count Anthropic cache_creation tokens in input usage#9881xiaoyuyu6420 wants to merge 1 commit into
xiaoyuyu6420 wants to merge 1 commit into
Conversation
Anthropic's input_tokens only covers tokens after the last cache breakpoint; cache_creation_input_tokens (tokens written into a new cache entry) were dropped entirely from TokenUsage, so usage.input/total undercounted requests that create a fresh cache entry. Fold cache_creation_input_tokens into input_other in both _extract_usage and the streaming _update_usage, matching the OpenAI provider's accounting where input_other holds all non-cache-read input (including cache writes). Official formula: total_input = cache_read + cache_creation + input_tokens.
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.
Problem
Anthropic's
Usage.input_tokensonly counts tokens after the last cache breakpoint — it excludes both cache reads and cache writes.ProviderAnthropic._extract_usagemapsinput_tokensintoTokenUsage.input_otherandcache_read_input_tokensintoinput_cached, which is correct for reads — butcache_creation_input_tokens(tokens written into a freshly created cache entry) are dropped entirely.Per Anthropic's official docs (linked in the code comment):
So on any request that creates a new cache entry (e.g. first turn after a cache
ephemeral_1hbreakpoint on a long system prompt),usage.inputandusage.totalundercount the real input — which feeds context-occupancy stats (current_context_tokensin the tool-loop runner).The OpenAI provider handles this category correctly:
input_other = prompt_tokens - cached, soinput_otherincludes cache-write tokens. Anthropic was the only provider dropping them.Changes
astrbot/core/provider/sources/anthropic_source.py: foldcache_creation_input_tokensintoinput_otherin both_extract_usage(non-streaming / message_start) and_update_usage(streaming message_delta), mirroring OpenAI's accounting whereinput_otherholds all non-cache-read input including cache writes.tests/test_anthropic_source.py(new): 5 unit tests covering extract/update with and without cache fields, and omitted-field preservation in streaming deltas.Verification
Summary by Sourcery
Ensure Anthropic cache writes are included in input token accounting so context and usage statistics remain accurate.
Bug Fixes:
Tests: