Skip to content

The fastest, litest AI Gateway. Rust core with Python SDK. Call 100+ LLM APIs in OpenAI (or native) format with cost tracking, guardrails, load balancing, and logging [Bedrock, Azure, OpenAI, Anthropic, OpenAI, VertexAI, vLLM, Nvidia NIM]

made changes suggested by agent

Lucky Lodhi1e55e6190781
diff --git a/litellm/llms/bedrock/chat/converse_transformation.py b/litellm/llms/bedrock/chat/converse_transformation.py
index 0e4ceb02144..6591e152a14 100644
--- a/litellm/llms/bedrock/chat/converse_transformation.py
+++ b/litellm/llms/bedrock/chat/converse_transformation.py
@@ -30,6 +30,8 @@ from litellm.litellm_core_utils.prompt_templates.factory import (
from litellm.llms.anthropic.chat.transformation import AnthropicConfig
from litellm.llms.base_llm.chat.transformation import BaseConfig, BaseLLMException
from litellm.types.llms.bedrock import *
+
+from ..common_utils import is_claude_4_5_on_bedrock
from litellm.types.llms.openai import (
AllMessageValues,
ChatCompletionAssistantMessage,
@@ -924,6 +926,7 @@ class AmazonConverseConfig(BaseConfig):
ChatCompletionAssistantMessage,
],
block_type: Literal["system"],
+ model: Optional[str] = None,
) -> Optional[SystemContentBlock]:
pass
@@ -937,6 +940,7 @@ class AmazonConverseConfig(BaseConfig):
ChatCompletionAssistantMessage,
],
block_type: Literal["content_block"],
+ model: Optional[str] = None,
) -> Optional[ContentBlock]:
pass
@@ -949,6 +953,7 @@ class AmazonConverseConfig(BaseConfig):
ChatCompletionAssistantMessage,
],
block_type: Literal["system", "content_block"],
+ model: Optional[str] = None,
) -> Optional[Union[SystemContentBlock, ContentBlock]]:
cache_control = message_block.get("cache_control", None)
if cache_control is None:
@@ -957,8 +962,9 @@ class AmazonConverseConfig(BaseConfig):
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
+ if ttl in ["5m", "1h"] and model is not None:
+ if is_claude_4_5_on_bedrock(model):
+ cache_point["ttl"] = ttl
if block_type == "system":
return SystemContentBlock(cachePoint=cache_point)
@@ -966,7 +972,7 @@ class AmazonConverseConfig(BaseConfig):
return ContentBlock(cachePoint=cache_point)
def _transform_system_message(
- self, messages: List[AllMessageValues]
+ self, messages: List[AllMessageValues], model: Optional[str] = None
) -> Tuple[List[AllMessageValues], List[SystemContentBlock]]:
system_prompt_indices = []
system_content_blocks: List[SystemContentBlock] = []
@@ -978,7 +984,7 @@ class AmazonConverseConfig(BaseConfig):
SystemContentBlock(text=message["content"])
)
cache_block = self._get_cache_point_block(
- message, block_type="system"
+ message, block_type="system", model=model
)
if cache_block:
system_content_blocks.append(cache_block)
@@ -989,7 +995,7 @@ class AmazonConverseConfig(BaseConfig):
SystemContentBlock(text=m["text"])
)
cache_block = self._get_cache_point_block(
- m, block_type="system"
+ m, block_type="system", model=model
)
if cache_block:
system_content_blocks.append(cache_block)
@@ -1258,7 +1264,9 @@ class AmazonConverseConfig(BaseConfig):
litellm_params: dict,
headers: Optional[dict] = None,
) -> RequestObject:
- messages, system_content_blocks = self._transform_system_message(messages)
+ messages, system_content_blocks = self._transform_system_message(
+ messages, model=model
+ )
# Convert last user message to guarded_text if guardrailConfig is present
messages = self._convert_consecutive_user_messages_to_guarded_text(
@@ -1314,7 +1322,9 @@ class AmazonConverseConfig(BaseConfig):
litellm_params: dict,
headers: Optional[dict] = None,
) -> RequestObject:
- messages, system_content_blocks = self._transform_system_message(messages)
+ messages, system_content_blocks = self._transform_system_message(
+ messages, model=model
+ )
# Convert last user message to guarded_text if guardrailConfig is present
messages = self._convert_consecutive_user_messages_to_guarded_text(
diff --git a/litellm/llms/bedrock/common_utils.py b/litellm/llms/bedrock/common_utils.py
index 65d237bdbdf..4c87f6fa994 100644
--- a/litellm/llms/bedrock/common_utils.py
+++ b/litellm/llms/bedrock/common_utils.py
@@ -446,6 +446,29 @@ def get_bedrock_base_model(model: str) -> str:
return model
+def is_claude_4_5_on_bedrock(model: str) -> bool:
+ """
+ Check if the model is a Claude 4.5 model on Bedrock.
+ Claude 4.5 models support prompt caching with '5m' and '1h' TTL on Bedrock.
+ """
+ model_lower = model.lower()
+ claude_4_5_patterns = [
+ "sonnet-4.5",
+ "sonnet_4.5",
+ "sonnet-4-5",
+ "sonnet_4_5",
+ "haiku-4.5",
+ "haiku_4.5",
+ "haiku-4-5",
+ "haiku_4_5",
+ "opus-4.5",
+ "opus_4.5",
+ "opus-4-5",
+ "opus_4_5",
+ ]
+ return any(pattern in model_lower for pattern in claude_4_5_patterns)
+
+
# Import after standalone functions to avoid circular imports
from litellm.llms.bedrock.count_tokens.bedrock_token_counter import BedrockTokenCounter
@@ -815,21 +838,23 @@ def get_anthropic_beta_from_headers(headers: dict) -> List[str]:
# If it's already a list, return it
if isinstance(anthropic_beta_header, list):
return anthropic_beta_header
-
+
# Try to parse as JSON array first (e.g., '["interleaved-thinking-2025-05-14", "claude-code-20250219"]')
if isinstance(anthropic_beta_header, str):
anthropic_beta_header = anthropic_beta_header.strip()
- if anthropic_beta_header.startswith("[") and anthropic_beta_header.endswith("]"):
+ if anthropic_beta_header.startswith("[") and anthropic_beta_header.endswith(
+ "]"
+ ):
try:
parsed = json.loads(anthropic_beta_header)
if isinstance(parsed, list):
return [str(beta).strip() for beta in parsed]
except json.JSONDecodeError:
pass # Fall through to comma-separated parsing
-
+
# Fall back to comma-separated values
return [beta.strip() for beta in anthropic_beta_header.split(",")]
-
+
return []
diff --git a/litellm/llms/bedrock/messages/invoke_transformations/anthropic_claude3_transformation.py b/litellm/llms/bedrock/messages/invoke_transformations/anthropic_claude3_transformation.py
index d9f9ec8f4be..90de67a822f 100644
--- a/litellm/llms/bedrock/messages/invoke_transformations/anthropic_claude3_transformation.py
+++ b/litellm/llms/bedrock/messages/invoke_transformations/anthropic_claude3_transformation.py
@@ -23,7 +23,10 @@ from litellm.llms.bedrock.chat.invoke_handler import AWSEventStreamDecoder
from litellm.llms.bedrock.chat.invoke_transformations.base_invoke_transformation import (
AmazonInvokeConfig,
)
-from litellm.llms.bedrock.common_utils import get_anthropic_beta_from_headers
+from litellm.llms.bedrock.common_utils import (
+ get_anthropic_beta_from_headers,
+ is_claude_4_5_on_bedrock,
+)
from litellm.types.llms.anthropic import ANTHROPIC_TOOL_SEARCH_BETA_HEADER
from litellm.types.llms.openai import AllMessageValues
from litellm.types.router import GenericLiteLLMParams
@@ -147,8 +150,6 @@ class AmazonAnthropicClaudeMessagesConfig(
ttl = cache_control["ttl"]
if is_claude_4_5 and ttl in ["5m", "1h"]:
continue
- if ttl in ["5m", "1h"]:
- continue
cache_control.pop("ttl", None)
@@ -218,22 +219,7 @@ class AmazonAnthropicClaudeMessagesConfig(
Returns:
True if the model is Claude 4.5
"""
- model_lower = model.lower()
- claude_4_5_patterns = [
- "sonnet-4.5",
- "sonnet_4.5",
- "sonnet-4-5",
- "sonnet_4_5",
- "haiku-4.5",
- "haiku_4.5",
- "haiku-4-5",
- "haiku_4_5",
- "opus-4.5",
- "opus_4.5",
- "opus-4-5",
- "opus_4_5",
- ]
- return any(pattern in model_lower for pattern in claude_4_5_patterns)
+ return is_claude_4_5_on_bedrock(model)
def _supports_tool_search_on_bedrock(self, model: str) -> bool:
"""