Skip to content

fix(bedrock): use JSON flag for 1h cache TTL gate, fixes Opus 4.8 regression - #30454

Closed
Devansh30032004 wants to merge 1 commit into
BerriAI:mainfrom
Devansh30032004:bugfix/bedrock-cache-pattern-opus-4-8
Closed

Devansh30032004 wants to merge 1 commit into
BerriAI:mainfrom
Devansh30032004:bugfix/bedrock-cache-pattern-opus-4-8

Conversation

@Devansh30032004

@Devansh30032004 Devansh30032004 commented Jun 15, 2026 •

Copy link
Copy Markdown

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

  • I have added meaningful tests
  • My PR passes all CI/CD checks (e.g., lint, format, unit tests)
  • My PR's scope is as isolated as possible; it only solves 1 specific problem
  • I have requested a Greptile review by commenting @greptileai and received a Confidence Score of at least 4/5 before requesting a maintainer review

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

The fix is exercised against a local proxy hitting real AWS Bedrock with the us.anthropic.claude-opus-4-8 cross-region inference profile. The system block requests a 1h ephemeral cache; the response usage block reveals which TTL bucket Bedrock actually used

Start the proxy

python litellm/proxy/proxy_cli.py --config litellm/proxy/dev_config.yaml --detailed_debug --reload --use_v2_migration_resolver 2>&1 | tee litellm.log

First request after the proxy starts (cold cache; >2048 input tokens to satisfy Bedrock's Opus prompt-caching minimum)

curl -s http://localhost:4000/v1/messages \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer sk-1234" \
  -H "anthropic-version: 2023-06-01" \
  -d '{
    "model": "bedrock/invoke/us.anthropic.claude-opus-4-8",
    "max_tokens": 16,
    "system": [
      {"type":"text","text":"<<2048+ tokens of stable preamble>>","cache_control":{"type":"ephemeral","ttl":"1h"}}
    ],
    "messages": [{"role":"user","content":"ping"}]
  }' | jq .usage

Before this PR the response shows cache_creation.ephemeral_5m_input_tokens taking the full block and ephemeral_1h_input_tokens: 0, because _sanitize_cache_control dropped the ttl key when the old pattern-list predicate did not match the Opus 4.8 id

After this PR the same request returns ephemeral_1h_input_tokens: <full block> and ephemeral_5m_input_tokens: 0, confirming the 1h bucket is used. A second identical request within the hour returns cache_read_input_tokens: <full block> with both creation buckets at 0, confirming the 1h cached entry is read back

Type

🐛 Bug Fix

Changes

The Bedrock cache TTL gate (previously named is_claude_4_5_on_bedrock in litellm/llms/bedrock/common_utils.py) decides whether cache_control.ttl ("5m" / "1h") is preserved on Bedrock requests. It is consumed by the Converse path in converse_transformation.py for system, message, and parallel_tool_use_config merging; the Invoke /v1/messages path in anthropic_claude3_transformation.py for system, message, and tool-block TTL gating; and add_cache_point_tool_block in factory.py for tool-block caching

That 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 in BEDROCK_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 list

This PR replaces the pattern list with a data-driven check. The predicate is renamed bedrock_supports_extended_cache_ttl and now reads a new bedrock_supports_extended_cache_ttl boolean from model_prices_and_context_window.json via get_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 reusing cache_creation_input_token_cost_above_1hr would have widened behavior incorrectly

Future Bedrock Claude models inherit correct TTL handling by adding "bedrock_supports_extended_cache_ttl": true to their JSON entry; no Python code changes required

Files touched

  • litellm/llms/bedrock/common_utils.py: pattern-list predicate replaced with bedrock_supports_extended_cache_ttl
  • model_prices_and_context_window.json and litellm/model_prices_and_context_window_backup.json: 52 Bedrock entries gain "bedrock_supports_extended_cache_ttl": true
  • litellm/types/utils.py and litellm/utils.py: new field propagated through ModelInfo and get_model_info
  • Three call sites (converse_transformation.py, anthropic_claude3_transformation.py, litellm_core_utils/prompt_templates/factory.py) rewired to the new predicate
  • tests/test_litellm/test_utils.py: JSON schema validation extended for the new field

Tests

Tests live in tests/test_litellm/llms/bedrock/test_bedrock_common_utils.py, the parallel-path mirror of litellm/llms/bedrock/common_utils.py per the repo's tests/test_litellm/readme.md convention

The replaced predicate is covered by five tests. Behavior via monkeypatch verifies the predicate returns True when _get_model_info returns the flag, False when absent, and False (not raises) on a get_model_info exception. 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 listing

Two existing tests in test_anthropic_claude3_transformation.py and test_litellm_core_utils_prompt_templates_factory.py referenced a fictional Sonnet 4.5 id (20250514) that the old pattern list matched only because of substring laxness; they now use the real 20250929 id so the assertions exercise actual behavior against the JSON

Local verification

LITELLM_LOCAL_MODEL_COST_MAP=true uv run pytest \
  tests/test_litellm/llms/bedrock/ \
  tests/test_litellm/litellm_core_utils/prompt_templates/ \
  tests/test_litellm/test_utils.py \
  -q

1139 passed in ~10 seconds. make lint (ruff format, ruff check) is clean

@greptile-apps

greptile-apps Bot commented Jun 15, 2026 •

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR adds four string patterns (opus-4.8, opus_4.8, opus-4-8, opus_4_8) to is_claude_4_5_on_bedrock so that Claude Opus 4.8 on Bedrock receives the correct 5m/1h cache-TTL treatment that was accidentally omitted when the model was registered in PR #29204.

  • The fix is functionally correct for now: the patterns match all expected Bedrock model-ID forms, and the added tests cover region prefixes, route prefixes, versioned suffixes, and case normalization.
  • However, the approach extends a manually-maintained string-pattern list that the repo's engineering rule explicitly prohibits in favour of reading model flags from model_prices_and_context_window.json via get_model_info. The relevant JSON entries already carry cache_creation_input_token_cost_above_1hr, which is the precise signal needed; keying the predicate off that field would prevent the same regression from recurring for every future Claude release.

Confidence Score: 3/5

The 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.

Important Files Changed

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)

  1. litellm/llms/bedrock/common_utils.py, line 653-689 (link)

    P1 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.json and be read via get_model_info, not in hardcoded string-pattern lists. is_claude_4_5_on_bedrock is 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 off get_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

@Sameerlite

Copy link
Copy Markdown
Contributor

Thanks for the contribution! A couple of things to address before this is ready for merge:

  • Greptile's code review flagged some concerns (score: 3/5) — could you take a look at the review comments and address them?

Once those are in, we'll take another look!

@Devansh30032004 Devansh30032004 changed the title fix(bedrock): recognize Claude Opus 4.8 in claude_4_5 cache TTL predicate fix(bedrock): use JSON flag for 1h cache TTL gate; fixes Opus 4.8 regression Jun 29, 2026
@Devansh30032004
Devansh30032004 force-pushed the bugfix/bedrock-cache-pattern-opus-4-8 branch from 147a2ab to 3198471 Compare June 29, 2026 13:07
@Devansh30032004 Devansh30032004 changed the title fix(bedrock): use JSON flag for 1h cache TTL gate; fixes Opus 4.8 regression fix(bedrock): use JSON flag for 1h cache TTL gate, fixes Opus 4.8 regression Jun 29, 2026
@codecov

codecov Bot commented Jun 29, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@Devansh30032004 Devansh30032004 changed the title fix(bedrock): use JSON flag for 1h cache TTL gate, fixes Opus 4.8 regression fix(bedrock): use JSON flag for 1h cache TTL gate, fixes Opus 4.8 regression Jun 29, 2026
@Devansh30032004
Devansh30032004 force-pushed the bugfix/bedrock-cache-pattern-opus-4-8 branch from 3198471 to cc116c3 Compare June 29, 2026 13:19
@Devansh30032004

Copy link
Copy Markdown
Author

Thanks for the contribution! A couple of things to address before this is ready for merge:

  • Greptile's code review flagged some concerns (score: 3/5) — could you take a look at the review comments and address them?

Once those are in, we'll take another look!

Addressed them

@yuneng-berri
yuneng-berri deleted the branch BerriAI:main September 13, 2026 04:24
@yuneng-berri yuneng-berri reopened this Sep 13, 2026
@devin-ai-integration
devin-ai-integration Bot changed the base branch from litellm_internal_staging to main September 23, 2026 16:03
@devin-ai-integration
devin-ai-integration Bot requested a review from a team September 23, 2026 16:03
@CLAassistant

CLAassistant commented Nov 4, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@mateo-berri

Copy link
Copy Markdown
Contributor

Main now gates the Bedrock 1h cache TTL on the cost map since #31929, so Opus 4.8 works. Reopen if something is missing

@mateo-berri mateo-berri closed this Nov 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants