Repository navigation
fix(bedrock): map Invoke streaming cacheRead/cacheWrite token counts into usage - #34542
devin-ai-integration[bot] wants to merge 2 commits into
Conversation
🤖 Devin AI EngineerI'll be helping with this pull request! Here's what you should know: ✅ I will automatically:
Note: I can only respond to comments from users who have write access to this repository. ⚙️ Control Options:
|
|
|
Greptile SummaryMaps Bedrock Invoke streaming cache metrics into Anthropic usage and preserves them through fallback reconstruction.
Confidence Score: 5/5The PR appears safe to merge with no actionable correctness or security issues identified. The mapped cache metrics retain the expected Anthropic usage semantics, flow through streaming reconstruction, and are covered by hermetic accounting and cost regression tests.
|
| Filename | Overview |
|---|---|
| litellm/llms/bedrock/messages/invoke_transformations/anthropic_claude3_transformation.py | Correctly maps Bedrock cache metrics into Anthropic usage while preserving non-conflicting usage fields. |
| litellm/proxy/pass_through_endpoints/llm_provider_handlers/anthropic_passthrough_logging_handler.py | Extends usage-only reconstruction to retain cache-write totals and tier details reported on the final delta. |
| tests/test_litellm/llms/bedrock/messages/invoke_transformations/test_anthropic_claude3_transformation.py | Adds hermetic unit and end-to-end coverage proving cache metrics survive decoding, reconstruction, and cost calculation. |
| tests/test_litellm/proxy/pass_through_endpoints/llm_provider_handlers/test_anthropic_passthrough_logging_handler.py | Adds focused regression coverage for recovering cache-creation usage from message-delta events. |
Reviews (1): Last reviewed commit: "fix(bedrock): map invoke streaming cache..." | Re-trigger Greptile
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
|
Closing as stale: no activity in the last 30 days. Feel free to reopen if this is still relevant. |
TLDR
Problem this solves:
How it solves it:
amazon-bedrock-invocationMetricsinto Anthropic usagecache_creation_input_tokensfrommessage_deltain the fallbackRelevant issues
Fixes #34497
Linear ticket
Pre-Submission checklist
Please complete all items before asking a LiteLLM maintainer to review your PR
@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
Pending: a live streaming call against a Bedrock Invoke Anthropic model with a large cached system prompt, showing
usage.cache_read_input_tokens/usage.cache_creation_input_tokensand the resulting spend before and after the fix. Will be added to this PR once capturedType
🐛 Bug Fix
Changes
On the Invoke path the cache breakdown exists only in the camelCase
amazon-bedrock-invocationMetricsblock on the final chunk, so the decoder was the last place it could be recovered; it readinputTokenCount/outputTokenCountonly and replaced the chunk'susagewholesale, which meant the promotion step added for LIT-2411 (which works on snake_case fields already onmessage_start/message_stop) never saw any cache tokens to promote. The decoder now merges the full metric set into whatever usage the chunk already carries:Since the cache counts now land on the final
message_delta,_build_usage_only_response_from_chunks(the recovery path used whenstream_chunk_buildercannot reassemble a large agentic stream) also had to stop reading cache-write tokens frommessage_startonly; it now picks upcache_creation_input_tokensand the 5m/1hcache_creationsplit frommessage_deltatoo, so those streams are no longer billed with cache-write at zero.For the reported numbers (1 uncached input, 421714 cache read, 1139 cache write, 162 output) reconstructed usage goes from
prompt_tokens=1toprompt_tokens=422854with the cached tokens priced at cache rates rather than as fresh input.Regression coverage: decoder mapping and non-clobbering of existing usage, an end-to-end Invoke stream (decoder ->
bedrock_sse_wrapper-> logging reconstruction ->completion_cost) asserting cache tokens survive and the cached cost is below the equivalent all-fresh-input cost, plus a usage-only fallback case with the cache breakdown onmessage_delta. All four fail on the parent commitFinal Attestation
Link to Devin session: https://app.devin.ai/sessions/9f4c540664624f00925465fcdc0ce405