Skip to content

fix #20326 - [Feature]: Support TTL(1h) field in prompt caching for Bedrock Claude 4.5 models #20338

Merged
Sameerlite merged 9 commits intoBerriAI:litellm_oss_staging_02_04_2026from
Lucky-Lodhi2004:ttl-prompt-caching-bedrock
Feb 5, 2026
Merged

fix #20326 - [Feature]: Support TTL(1h) field in prompt caching for Bedrock Claude 4.5 models #20338
Sameerlite merged 9 commits intoBerriAI:litellm_oss_staging_02_04_2026from
Lucky-Lodhi2004:ttl-prompt-caching-bedrock

Conversation

@Lucky-Lodhi2004
Copy link
Contributor

Relevant issues

fixes issue #20326

Pre-Submission checklist

Please complete all items before asking a LiteLLM maintainer to review your PR

  • I have Added testing in the tests/litellm/ directory, Adding at least 1 test is a hard requirement - see details
  • My PR passes all unit tests on make test-unit
  • My PR's scope is as isolated as possible, it only solves 1 specific problem

CI (LiteLLM team)

CI status guideline:

  • 50-55 passing tests: main is stable with minor issues.
  • 45-49 passing tests: acceptable but needs attention
  • <= 40 passing tests: unstable; be careful with your merges and assess the risk.
  • Branch creation CI run
    Link:

  • CI run for the last commit
    Link:

  • Merge / cherry-pick CI run
    Links:

Type

🆕 New Feature
🐛 Bug Fix
🧹 Refactoring
📖 Documentation
🚄 Infrastructure
✅ Test

Changes

added ttl support in claude 4.5 models.

@vercel
Copy link

vercel bot commented Feb 3, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
litellm Ready Ready Preview, Comment Feb 5, 2026 11:23am

Request Review

@greptile-apps
Copy link
Contributor

greptile-apps bot commented Feb 3, 2026

Greptile Overview

Greptile Summary

This PR adds support for TTL (Time-To-Live) field in prompt caching for AWS Bedrock Claude 4.5 models, allowing "5m" and "1h" TTL values to be passed through to the API.

Changes made:

  • Added ttl field to CachePointBlock TypedDict in litellm/types/llms/bedrock.py
  • Modified Converse API transformation to extract and validate TTL values from cache_control
  • Modified Invoke API transformation to conditionally preserve TTL based on model version
  • Added _is_claude_4_5_on_bedrock() method to detect Claude 4.5 models (Sonnet, Haiku, Opus)

Critical issues found:

  • Logic error (line 156-157 in anthropic_claude3_transformation.py): The code preserves TTL values for ALL models instead of only Claude 4.5, which will break the existing unit test test_remove_ttl_from_cache_control
  • No tests added: The PR checklist requires adding at least 1 test, but no tests were added to validate the new TTL support for Claude 4.5 models

Additional concerns:

  • Converse API implementation doesn't check model version before allowing TTL - needs verification if this is correct behavior
  • Debug comments left in code (lines 151-154)
  • Typo in comment ("bedock" instead of "Bedrock")

Confidence Score: 1/5

  • This PR is NOT safe to merge - it contains a critical logic error that will break existing tests and cause incorrect behavior
  • Score of 1/5 due to critical logic bug (lines 156-157 preserve TTL for all models instead of only Claude 4.5), missing required tests, and uncertainty about Converse API behavior
  • Pay immediate attention to litellm/llms/bedrock/messages/invoke_transformations/anthropic_claude3_transformation.py lines 156-157 - critical logic error must be fixed before merge

Important Files Changed

Filename Overview
litellm/types/llms/bedrock.py Added ttl field to CachePointBlock TypedDict to support TTL values in prompt caching
litellm/llms/bedrock/chat/converse_transformation.py Modified _get_cache_point_block to extract and validate TTL values ("5m" or "1h") from cache_control before creating cache point blocks
litellm/llms/bedrock/messages/invoke_transformations/anthropic_claude3_transformation.py Added Claude 4.5 detection method and modified TTL removal logic, but contains critical logic error that preserves TTL for all models instead of only Claude 4.5

Sequence Diagram

sequenceDiagram
    participant Client
    participant LiteLLM
    participant ConverseAPI as Bedrock Converse API
    participant InvokeAPI as Bedrock Invoke API
    
    Note over Client,InvokeAPI: TTL Prompt Caching Flow
    
    Client->>LiteLLM: messages with cache_control: {type: "ephemeral", ttl: "1h"}
    
    alt Converse API Path
        LiteLLM->>LiteLLM: _get_cache_point_block()
        LiteLLM->>LiteLLM: Extract ttl from cache_control
        alt ttl is "5m" or "1h"
            LiteLLM->>LiteLLM: Add ttl to CachePointBlock
        else other ttl value
            LiteLLM->>LiteLLM: Create CachePointBlock without ttl
        end
        LiteLLM->>ConverseAPI: Request with cachePoint: {type: "default", ttl: "1h"}
        ConverseAPI->>LiteLLM: Response with cache metrics
    else Invoke API Path
        LiteLLM->>LiteLLM: _remove_ttl_from_cache_control(model)
        LiteLLM->>LiteLLM: _is_claude_4_5_on_bedrock(model)
        alt Claude 4.5 model AND ttl is "5m" or "1h"
            LiteLLM->>LiteLLM: Keep ttl in cache_control
            Note over LiteLLM: BUG: Lines 156-157<br/>always keep "5m"/"1h"<br/>for ALL models
        else Other model OR other ttl
            LiteLLM->>LiteLLM: Remove ttl from cache_control
        end
        LiteLLM->>InvokeAPI: Request with modified cache_control
        InvokeAPI->>LiteLLM: Response with cache metrics
    end
    
    LiteLLM->>Client: Return response with cache tokens
Loading

Copy link
Contributor

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

3 files reviewed, 4 comments

Edit Code Review Agent Settings | Greptile

Comment on lines 156 to 157
if ttl in ["5m", "1h"]:
continue
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Critical logic error: this code preserves TTL values of "5m" and "1h" for ALL models, not just Claude 4.5 models. This will break the existing test at tests/test_litellm/llms/bedrock/messages/invoke_transformations/test_anthropic_claude3_transformation.py:108-113 which expects TTL to be removed when no model is passed.

The lines 148-149 already handle the Claude 4.5 case, so lines 156-157 should be deleted. Without them, the logic will be:

  • If Claude 4.5 and TTL is "5m" or "1h" → keep it (line 148-149)
  • Otherwise, if TTL exists → remove it (line 159)
Suggested change
if ttl in ["5m", "1h"]:
continue
cache_control.pop("ttl", None)
Prompt To Fix With AI
This is a comment left during a code review.
Path: litellm/llms/bedrock/messages/invoke_transformations/anthropic_claude3_transformation.py
Line: 156:157

Comment:
Critical logic error: this code preserves TTL values of `"5m"` and `"1h"` for ALL models, not just Claude 4.5 models. This will break the existing test at `tests/test_litellm/llms/bedrock/messages/invoke_transformations/test_anthropic_claude3_transformation.py:108-113` which expects TTL to be removed when no model is passed.

The lines 148-149 already handle the Claude 4.5 case, so lines 156-157 should be deleted. Without them, the logic will be:
- If Claude 4.5 and TTL is "5m" or "1h" → keep it (line 148-149)
- Otherwise, if TTL exists → remove it (line 159)

```suggestion
                                    cache_control.pop("ttl", None)
```

How can I resolve this? If you propose a fix, please make it concise.

Remove `ttl` field from cache_control in messages.
Bedrock doesn't support the ttl field in cache_control.

Update: bedock supports `5m` and `1h` for Claude 4.5 models.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo: "bedock" should be "Bedrock"

Suggested change
Update: bedock supports `5m` and `1h` for Claude 4.5 models.
Update: Bedrock supports `5m` and `1h` for Claude 4.5 models.
Prompt To Fix With AI
This is a comment left during a code review.
Path: litellm/llms/bedrock/messages/invoke_transformations/anthropic_claude3_transformation.py
Line: 125:125

Comment:
Typo: "bedock" should be "Bedrock"

```suggestion
        Update: Bedrock supports `5m` and `1h` for Claude 4.5 models.
```

How can I resolve this? If you propose a fix, please make it concise.

Comment on lines 957 to 961
cache_point = CachePointBlock(type="default")
if isinstance(cache_control, dict) and "ttl" in cache_control:
ttl = cache_control["ttl"]
if ttl in ["5m", "1h"]:
cache_point["ttl"] = ttl
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verify whether the Converse API supports TTL for all Claude models or only Claude 4.5 models. The Invoke API implementation (in anthropic_claude3_transformation.py) restricts TTL support to Claude 4.5 models. If the Converse API has the same restriction, this code should also check the model version using a method similar to _is_claude_4_5_on_bedrock().

Prompt To Fix With AI
This is a comment left during a code review.
Path: litellm/llms/bedrock/chat/converse_transformation.py
Line: 957:961

Comment:
Verify whether the Converse API supports TTL for all Claude models or only Claude 4.5 models. The Invoke API implementation (in `anthropic_claude3_transformation.py`) restricts TTL support to Claude 4.5 models. If the Converse API has the same restriction, this code should also check the model version using a method similar to `_is_claude_4_5_on_bedrock()`.

How can I resolve this? If you propose a fix, please make it concise.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Lucky-Lodhi2004 Please make fixes based on the reviews

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, i made the proposed changes, but i have a question, why do mcp tests always fail when i make a pull request? do failing mcp tests may cause my PR to be rejected (not merged)?

@Sameerlite Sameerlite changed the base branch from main to litellm_oss_staging_02_04_2026 February 5, 2026 11:21
@Sameerlite Sameerlite merged commit 34e5bb2 into BerriAI:litellm_oss_staging_02_04_2026 Feb 5, 2026
3 of 4 checks passed
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.

2 participants