Repository navigation
fix(bedrock): use JSON flag for 1h cache TTL gate, fixes Opus 4.8 regression - #30454
Devansh30032004 wants to merge 1 commit into
Conversation
Greptile SummaryThis PR adds four string patterns (
Confidence Score: 3/5The fix resolves a real, narrowly scoped bug but does so through a hardcoded pattern list that will need another manual edit for every subsequent Claude model — the same gap it is closing for Opus 4.8. The change works correctly today and is well-tested, but it perpetuates the exact maintenance gap that caused this bug in the first place. The Bedrock Opus entries in model_prices_and_context_window.json already carry cache_creation_input_token_cost_above_1hr, which is the correct signal to key the predicate on; not using it means any future Claude model registration will silently fall back to the wrong TTL bucket again. litellm/llms/bedrock/common_utils.py — the is_claude_4_5_on_bedrock function and its manually-maintained pattern list are the root of the recurring issue; the test file is clean.
|
| Filename | Overview |
|---|---|
| litellm/llms/bedrock/common_utils.py | Adds four opus-4.8 string patterns to the manually-maintained claude_4_5_patterns list inside is_claude_4_5_on_bedrock, correctly fixing the immediate TTL routing bug but extending a hardcoded approach that violates the repo rule requiring model flags to live in model_prices_and_context_window.json. |
| tests/test_litellm/llms/bedrock/test_bedrock_common_utils.py | Adds well-structured parametrized tests covering all four string variants, region prefixes, route prefixes, versioned IDs, and a case-normalization guard for positive matching, plus a tight negative set that guards against adjacent versions and different model families. |
Comments Outside Diff (1)
-
litellm/llms/bedrock/common_utils.py, line 653-689 (link)Hardcoded model-name list violates the repo's model-flag policy
The repo rule requires model-specific capability flags to live in
model_prices_and_context_window.jsonand be read viaget_model_info, not in hardcoded string-pattern lists.is_claude_4_5_on_bedrockis already entirely built around such a list, and this PR extends it. Because the list is maintained manually, every future Claude release that supports the extended TTL will silently fall back to the 5-minute bucket again — the same failure this PR corrects for Opus 4.8.The JSON already encodes the right signal: all Bedrock Opus entries that support the 1h bucket carry
"cache_creation_input_token_cost_above_1hr". Keying the predicate offget_model_info(model).get("cache_creation_input_token_cost_above_1hr") is not None(or a dedicated boolean flag like"bedrock_supports_extended_cache_ttl") would make every future model registration in the JSON automatically inherit correct behaviour without any additional code change.Rule Used: What: Do not hardcode model-specific flags in the ... (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!
Reviews (1): Last reviewed commit: "fix(bedrock): recognize Claude Opus 4.8 ..." | Re-trigger Greptile
|
Thanks for the contribution! A couple of things to address before this is ready for merge:
Once those are in, we'll take another look! |
147a2ab to
3198471
Compare
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
3198471 to
cc116c3
Compare
Addressed them |
|
Main now gates the Bedrock 1h cache TTL on the cost map since #31929, so Opus 4.8 works. Reopen if something is missing |
Relevant issues
None filed; surfaces a gap left by #29204, which registered Opus 4.8 across providers but did not extend the Bedrock cache TTL gate
Linear ticket
N/A (external contributor)
Pre-Submission checklist
Please complete all items before asking a LiteLLM maintainer to review your PR
@greptileaiand received a Confidence Score of at least 4/5 before requesting a maintainer reviewDelays 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
The fix is exercised against a local proxy hitting real AWS Bedrock with the
us.anthropic.claude-opus-4-8cross-region inference profile. The system block requests a 1h ephemeral cache; the response usage block reveals which TTL bucket Bedrock actually usedStart the proxy
First request after the proxy starts (cold cache; >2048 input tokens to satisfy Bedrock's Opus prompt-caching minimum)
Before this PR the response shows
cache_creation.ephemeral_5m_input_tokenstaking the full block andephemeral_1h_input_tokens: 0, because_sanitize_cache_controldropped thettlkey when the old pattern-list predicate did not match the Opus 4.8 idAfter this PR the same request returns
ephemeral_1h_input_tokens: <full block>andephemeral_5m_input_tokens: 0, confirming the 1h bucket is used. A second identical request within the hour returnscache_read_input_tokens: <full block>with both creation buckets at 0, confirming the 1h cached entry is read backType
🐛 Bug Fix
Changes
The Bedrock cache TTL gate (previously named
is_claude_4_5_on_bedrockinlitellm/llms/bedrock/common_utils.py) decides whethercache_control.ttl("5m"/"1h") is preserved on Bedrock requests. It is consumed by the Converse path inconverse_transformation.pyfor system, message, andparallel_tool_use_configmerging; the Invoke/v1/messagespath inanthropic_claude3_transformation.pyfor system, message, and tool-block TTL gating; andadd_cache_point_tool_blockinfactory.pyfor tool-block cachingThat predicate was a hand-maintained string-pattern list whose tail ended at
opus-4-7, so Opus 4.8 traffic silently fell back to the default 5m bucket even after PR #29204 registered the model inBEDROCK_CONVERSE_MODELS, the cost map, and the setup wizard. Every future Claude release on Bedrock that supports the 1h TTL would have hit the same gap until someone remembered to extend the listThis PR replaces the pattern list with a data-driven check. The predicate is renamed
bedrock_supports_extended_cache_ttland now reads a newbedrock_supports_extended_cache_ttlboolean frommodel_prices_and_context_window.jsonviaget_model_info. The flag is set on every Bedrock entry that AWS documents as 1h-capable on the Bedrock pricing page (Opus 4.5/4.6/4.7/4.8, Sonnet 4.5/4.6, Haiku 4.5, and Fable 5; 52 entries across regional variants). The flag is Bedrock-specific by design rather than inferred from Anthropic API pricing fields, because Bedrock's 1h support is narrower than Anthropic's native API; the Bedrock prompt-caching docs list 3.5/3.7 Sonnet and Claude Opus 4 as 5m-only despite their JSON entries carrying 1h Anthropic pricing, so reusingcache_creation_input_token_cost_above_1hrwould have widened behavior incorrectlyFuture Bedrock Claude models inherit correct TTL handling by adding
"bedrock_supports_extended_cache_ttl": trueto their JSON entry; no Python code changes requiredFiles touched
litellm/llms/bedrock/common_utils.py: pattern-list predicate replaced withbedrock_supports_extended_cache_ttlmodel_prices_and_context_window.jsonandlitellm/model_prices_and_context_window_backup.json: 52 Bedrock entries gain"bedrock_supports_extended_cache_ttl": truelitellm/types/utils.pyandlitellm/utils.py: new field propagated throughModelInfoandget_model_infoconverse_transformation.py,anthropic_claude3_transformation.py,litellm_core_utils/prompt_templates/factory.py) rewired to the new predicatetests/test_litellm/test_utils.py: JSON schema validation extended for the new fieldTests
Tests live in
tests/test_litellm/llms/bedrock/test_bedrock_common_utils.py, the parallel-path mirror oflitellm/llms/bedrock/common_utils.pyper the repo'stests/test_litellm/readme.mdconventionThe replaced predicate is covered by five tests. Behavior via monkeypatch verifies the predicate returns True when
_get_model_inforeturns the flag, False when absent, and False (not raises) on aget_model_infoexception. A bundled-model regression set verifies that every Bedrock entry AWS documents as 1h-capable resolves to True against the real JSON, including the cross-region prefixes (us.,eu.,jp.,apac.,global.) and bare ids; anchored on Opus 4.8 specifically as the original regression. A negative set covers 3.5 Sonnet, 3.7 Sonnet, 3 Haiku, and Opus 4 (anthropic.claude-3-5-sonnet-20241022-v2:0,anthropic.claude-3-7-sonnet-20250219-v1:0,anthropic.claude-3-haiku-20240307-v1:0,anthropic.claude-opus-4) returning False, matching the AWS docs' 5m-only listingTwo existing tests in
test_anthropic_claude3_transformation.pyandtest_litellm_core_utils_prompt_templates_factory.pyreferenced a fictional Sonnet 4.5 id (20250514) that the old pattern list matched only because of substring laxness; they now use the real20250929id so the assertions exercise actual behavior against the JSONLocal verification
1139 passed in ~10 seconds.
make lint(ruff format, ruff check) is clean