fix(bedrock): preserve cache_control for ARN models in /v1/messages adapter - #29822
Closed
mateo-berri wants to merge 1 commit into
Closed
fix(bedrock): preserve cache_control for ARN models in /v1/messages adapter#29822mateo-berri wants to merge 1 commit into
mateo-berri wants to merge 1 commit into
Conversation
…dapter Bedrock Application Inference Profile ARNs contain neither "anthropic" nor "claude", so is_anthropic_claude_model could not detect them and the /v1/messages adapter silently dropped cache_control during the Anthropic to OpenAI translation. Prompt caching never activated for these models, while the same profile cached correctly through /v1/chat/completions. Add an is_bedrock_arn_model check scoped to _add_cache_control_if_applicable so cache_control is preserved for ARN-based models without broadening the shared is_anthropic_claude_model helper, which also drives thinking translation. Fixes #26625
Contributor
Author
|
Superseded by #29823, which carries the same change on a Generated by Claude Code |
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
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.
Relevant issues
Fixes #26625
Linear ticket
Pre-Submission checklist
make test-unit@greptileaiand received a Confidence Score of at least 4/5 before requesting a maintainer reviewCI (LiteLLM team)
Branch creation CI run
Link:
CI run for the last commit
Link:
Merge / cherry-pick CI run
Links:
Screenshots / Proof of Fix
I don't have Bedrock credentials in this environment to hit a real Application Inference Profile, so here is a runbook to confirm the fix against a live proxy. Point a profile at any Claude model and run both endpoints; only the
/v1/messagespath was broken before.cache_creation_input_tokens: 0andcache_read_input_tokens: 0. After this fix the first call returns a non-zerocache_creation_input_tokens, and an immediate identical second call returns a non-zerocache_read_input_tokens, matching what/v1/chat/completionsalready returned for the same profileType
🐛 Bug Fix
Changes
When a Bedrock Application Inference Profile is called through the Anthropic
/v1/messagesendpoint (the path Claude Code uses),cache_controlwas silently dropped during the Anthropic to OpenAI message translation, so prompt caching never activated. Several users reported large unexpected Bedrock bills from this since the same profile cached correctly through/v1/chat/completions.The root cause is in
litellm/llms/anthropic/experimental_pass_through/adapters/transformation.py._add_cache_control_if_applicable()only preservescache_controlwhenis_anthropic_claude_model()returns true, and that helper looks for the substringsanthropicorclaudein the model string. An Application Inference Profile model is an ARN likebedrock/converse/arn:aws:bedrock:us-east-1:ACCOUNT:application-inference-profile/ID, which contains neither substring, so the check failed and the directive was dropped before reaching the Bedrock ConversecachePointconversion.The fix adds a small
is_bedrock_arn_model()helper and uses it as an additional condition in_add_cache_control_if_applicable()only. I deliberately did not broadenis_anthropic_claude_model()itself, because that helper also gatesthinkingtoreasoning_efforttranslation; widening it would letthinkingpass through unmodified for a genuinely non-Claude Bedrock profile and break that request. Scoping the change to cache control keeps the blast radius to the one path that was broken, and preservingcache_controlis safe even for the rare non-Claude ARN since Bedrock Converse ignorescachePointblocks it doesn't support rather than rejecting the request.Tests live in the mapped file
tests/test_litellm/llms/anthropic/experimental_pass_through/adapters/test_anthropic_experimental_pass_through_adapters_transformation.py.test_cache_control_preserved_for_bedrock_arn_inference_profileis the regression for this issue and fails onmainwith aKeyErroron the missingcache_control.test_is_bedrock_arn_modelpins the ARN-plus-bedrock detection including the GovCloud partition and rejects non-ARN bedrock model ids and non-bedrock ARNs.test_cache_control_fix_does_not_broaden_claude_detectionguards the scoping decision so a future change can't quietly widenis_anthropic_claude_modeland regress thinking translation.This is the same problem and approach validated in the discussion on the stale community PR #26627; this revives it on top of current internal staging with added regression coverage so it can land
Generated by Claude Code