Repository navigation
fix(proxy): propagate Invoke cache-token counts to usage - #34498
Draft
yryzhan-vitech wants to merge 1 commit into
Draft
yryzhan-vitech wants to merge 1 commit into
yryzhan-vitech wants to merge 1 commit into
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
yryzhan-vitech
marked this pull request as ready for review
July 24, 2026 12:52
Contributor
Greptile SummaryPropagates Bedrock Invoke prompt-cache token counts through streaming usage and fallback cost accounting
Confidence Score: 5/5The PR appears safe to merge, with no concrete correctness or security failures identified in the changed paths The new mappings preserve optional and zero-valued provider metrics, and the fallback forwards final cache-creation usage through the existing Anthropic usage calculation without introducing additive double-counting
|
| Filename | Overview |
|---|---|
| litellm/llms/bedrock/messages/invoke_transformations/anthropic_claude3_transformation.py | Maps independently reported Bedrock Invoke cache metrics into the established Anthropic usage fields without changing absent-field behavior |
| litellm/proxy/pass_through_endpoints/llm_provider_handlers/anthropic_passthrough_logging_handler.py | Extends usage-only stream recovery to retain cache-creation counts reported in final message deltas |
| tests/test_litellm/llms/bedrock/messages/invoke_transformations/test_anthropic_claude3_transformation.py | Adds regression coverage for combined, absent, independent, and zero-valued cache metrics |
| tests/test_litellm/proxy/pass_through_endpoints/llm_provider_handlers/test_anthropic_passthrough_logging_handler.py | Verifies fallback usage reconstruction includes delta-reported cache-creation tokens in prompt totals |
Reviews (1): Last reviewed commit: "fix(bedrock): propagate Invoke cache-tok..." | Re-trigger Greptile
Contributor
yryzhan-vitech
marked this pull request as draft
July 24, 2026 13:33
AmazonAnthropicClaudeMessagesStreamDecoder._chunk_parser mapped only inputTokenCount/outputTokenCount from Bedrock Invoke's amazon-bedrock-invocationMetrics block, dropping cacheReadInputTokenCount and cacheWriteInputTokenCount. Cache-heavy Anthropic Claude traffic over the Invoke streaming path then had no cache_read_input_tokens / cache_creation_input_tokens in usage, so the cost calculator billed every request as fresh input. Map the two cache fields onto the synthesized Anthropic-style usage when present. Also fix the usage-only recovery fallback (_build_usage_only_response_from_chunks): its message_delta branch read cache_read_input_tokens but not cache_creation_input_tokens (only message_start supplied the latter). Since the Invoke decoder lands cache usage on the final delta, cache-write tokens were silently dropped and billed at zero on large/agentic streams that stream_chunk_builder cannot reassemble. Read cache_creation_input_tokens from message_delta too. Add unit tests: the decoder mapping (present/absent/independent branches) and the fallback recovering cache_creation from message_delta.
yryzhan-vitech
force-pushed
the
bugfix/bedrock-invoke-cache-token-usage
branch
from
August 13, 2026 09:01
4be947c to
4467abc
Compare
7 tasks done
devin-ai-integration
Bot
changed the base branch from
litellm_internal_staging
to
main
September 23, 2026 15:32
8 of 10 tasks
This branch has not been deployed
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.
TLDR
Problem this solves:
cacheRead/cacheWritetoken counts from usage → cache-heavy Claude traffic billed as fresh input (4–7× over-report)How it solves it:
cache_creationfrommessage_deltain the fallback — only when reported, no double-countRelevant issues
Fixes #34497
Pre-Submission checklist
Screenshots / Proof of Fix
Reproduction: route an Anthropic Claude model over the Bedrock Invoke streaming path (
bedrock/invoke/…) with a large cache-eligible prompt +cache_control. Pre-fix the returnedusagehasinput_tokens/output_tokensbut nocache_read_input_tokens/cache_creation_input_tokens, even though Bedrock reportedcacheReadInputTokenCount/cacheWriteInputTokenCountinamazon-bedrock-invocationMetrics— so the cost calculator bills every request as fresh input.The fallback test is a negative control: it asserts
cache_creation_input_tokens == 120; reverting only the handler change fails it atassert 0 == 120, confirming the fix is load-bearing.Live proof on a running proxy to be added.
Type
🐛 Bug Fix
Changes
For Anthropic Claude models on the Bedrock Invoke streaming path, the final chunk reports token usage in
amazon-bedrock-invocationMetrics. The decoder mapped onlyinputTokenCount/outputTokenCountand droppedcacheReadInputTokenCount/cacheWriteInputTokenCount, so prompt-cache usage never reachedcache_read_input_tokens/cache_creation_input_tokensand cache-heavy traffic was billed as fresh input.AmazonAnthropicClaudeMessagesStreamDecoder._chunk_parser(litellm/llms/bedrock/messages/invoke_transformations/anthropic_claude3_transformation.py) now mapscacheReadInputTokenCount → cache_read_input_tokensandcacheWriteInputTokenCount → cache_creation_input_tokenswhen present._build_usage_only_response_from_chunks(litellm/proxy/pass_through_endpoints/llm_provider_handlers/anthropic_passthrough_logging_handler.py) readcache_read_input_tokensfrommessage_deltabut readcache_creation_input_tokensonly frommessage_start; themessage_deltabranch now readscache_creation_input_tokenstoo.Notes for review:
_promote_message_stop_usage: that runs one stage later on fields already in snake_case onmessage_start/message_stop. On the Invoke path the cache counts exist only in the camelCaseinvocationMetricsblock, so they were dropped before that step could see them — the two are complementary, not redundant.inputTokenCountis exclusive of cached tokens (a real chunk showsinputTokenCount: 1alongsidecacheReadInputTokenCount: 421714), soinput_tokensdoes not overlap the cache counts downstream.stream_chunk_buildercannot reassemble, cache-write tokens were silently priced at zero. Regression test covers exactly that recovery path.Final Attestation