Repository navigation
fix(anthropic): map Responses API cached input tokens to cache_read_input_tokens on /v1/messages - #36097
Conversation
…nput_tokens The Anthropic /v1/messages bridge for Responses API models never populated cache_read_input_tokens, so clients reading the Anthropic shape saw an uncached prompt on every call even when upstream served ~100% of it from cache. Streaming read the cache counts from Anthropic-named attributes that a Responses usage object does not have, and the non-streaming path dropped them entirely. Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
|
|
Greptile SummaryThis PR centralizes conversion of Responses API usage into Anthropic usage and applies it to both streaming and non-streaming
Confidence Score: 4/5The PR appears safe to merge after addressing the non-blocking coarse type annotations in the new streaming test helper. The shared usage conversion matches established Anthropic cache-token semantics and is covered across streaming and non-streaming paths; the only accepted concern is reduced static checking in a test helper. Files Needing Attention: tests/test_litellm/llms/anthropic/experimental_pass_through/responses_adapters/test_responses_adapters_streaming_iterator.py
|
| Filename | Overview |
|---|---|
| litellm/llms/anthropic/experimental_pass_through/responses_adapters/usage.py | Adds a defensive shared mapper that converts Responses API token and cache usage into Anthropic semantics. |
| litellm/llms/anthropic/experimental_pass_through/responses_adapters/streaming_iterator.py | Replaces ineffective direct cache-field reads with the shared usage mapper for terminal stream events. |
| litellm/llms/anthropic/experimental_pass_through/responses_adapters/transformation.py | Uses the shared mapper for non-streaming Anthropic response translation. |
| tests/test_litellm/llms/anthropic/experimental_pass_through/responses_adapters/test_responses_adapters_streaming_iterator.py | Adds focused streaming usage tests, but the new helper violates the repository’s precise-typing convention. |
| tests/test_litellm/llms/anthropic/experimental_pass_through/responses_adapters/test_responses_adapters_transformation.py | Adds non-streaming regression coverage for cached-token mapping, subtraction, and omitted zero-value fields. |
Reviews (1): Last reviewed commit: "fix(anthropic): map Responses API cached..." | Re-trigger Greptile
| ``input_tokens`` to exclude it.""" | ||
|
|
||
| @staticmethod | ||
| def _message_delta_usage(usage: dict) -> dict: |
There was a problem hiding this comment.
Specify the usage dictionary types
The bare dict annotations discard the expected usage shape from static checking, allowing malformed fixtures and accesses to pass without diagnostics. Parameterize the input and return dictionaries to preserve type checking in these new tests.
| def _message_delta_usage(usage: dict) -> dict: | |
| def _message_delta_usage(usage: dict[str, object]) -> dict[str, object]: |
Context Used: CLAUDE.md (source)
Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!
|
Closing as superseded: #34957 merged Aug 5 already maps Responses API cache tokens into Anthropic usage on both paths and shipped in v1.97.0-rc.1. |
TLDR
Problem this solves:
/v1/messageson Responses-API models never returnedcache_read_input_tokensHow it solves it:
input_tokens_details.cached_tokenstocache_read_input_tokensinput_tokens, as Anthropic clients expectRelevant issues
Fixes #36091
Linear ticket
Pre-Submission checklist
@greptileaito re-request a review after pushing changes)Delays in PR merge?
If you're seeing a delay in your PR being merged, ping the LiteLLM Team on Slack (#pr-review).
Screenshots / Proof of Fix
Live proxy on localhost:4000, one model routed to a Responses-API upstream that reports a cache hit on the second identical prompt (
input_tokens: 25616,input_tokens_details.cached_tokens: 25613,output_tokens: 5). The upstream here is a local stand-in rather than api.openai.com, which this environment cannot reach, so a reviewer should re-run the same two calls against a realopenai/gpt-5.xdeployment: the numbers below come straight from whatever the upstream reports, so any cached prompt reproduces itBefore, at 24dbd2b
The same prompt through
/v1/chat/completionsshowed the upstream cache working the whole time (prompt_tokens=25616 cached_tokens=25613), so only the Anthropic shape lost itAfter, at e030038
Call 1 is the cold prompt and call 2 is the cache hit, so
input_tokensdrops to the 3 uncached tokens and the 25613 cached ones show up where an Anthropic client reads them./v1/chat/completionsand/v1/responsesare unaffected by this change: it only touches the Anthropic bridge's Responses adaptersType
🐛 Bug Fix
Changes
responses_adapters/transformation.pybuiltAnthropicUsagefrominput_tokensandoutput_tokensonly, andresponses_adapters/streaming_iterator.pyfetched the two details objects into the cache variables and then overwrote both withcache_creation_input_tokens/cache_read_input_tokensoff the usage object, names that only exist on Anthropic usage and never on a Responses one, so both counts were pinned to 0 on every callBoth now go through
responses_adapters/usage.py::anthropic_usage_from_responses_usage, which readsinput_tokens_details.cached_tokens(object or dict shaped) and returnsomitting the cache field entirely when there is no hit, which matches how the chat-completions bridge already reports it. The subtraction is the part that is easy to miss: the Responses API counts cached tokens inside
input_tokens, while Anthropic reports them alongside it, so passing the total through unchanged would double-count the prompt for anything summing the fields. The cached count is clamped toinput_tokensso an upstream reporting more cached than total can't yield a negativeRegression tests cover both paths in the existing adapter test files: the mapped fields and the subtraction, the no-cache-hit case still omitting the fields, and a usage object missing the counts falling back to zeroes instead of raising mid-stream
Final Attestation