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]

fix(anthropic): never carry cache_control on translated thinking blocks (#39815)

moe-berriaa3d59d086ac
diff --git a/litellm/llms/anthropic/experimental_pass_through/adapters/transformation.py b/litellm/llms/anthropic/experimental_pass_through/adapters/transformation.py
index 573a461e89e..64f10046109 100644
--- a/litellm/llms/anthropic/experimental_pass_through/adapters/transformation.py
+++ b/litellm/llms/anthropic/experimental_pass_through/adapters/transformation.py
@@ -524,18 +524,20 @@ class LiteLLMAnthropicMessagesAdapter:
self._add_cache_control_if_applicable(content, tool_call, model)
tool_calls.append(tool_call)
elif content.get("type") == "thinking":
+ # Anthropic's schema has no cache_control on thinking or
+ # redacted_thinking blocks, and anthropic_messages_pt replays
+ # these verbatim at content[0], so carrying one here (or
+ # inventing an empty one) is a guaranteed 400 on the way back.
thinking_block = ChatCompletionThinkingBlock(
type="thinking",
thinking=content.get("thinking") or "",
signature=content.get("signature") or "",
- cache_control=content.get("cache_control", {}),
)
thinking_blocks.append(thinking_block)
elif content.get("type") == "redacted_thinking":
redacted_thinking_block = ChatCompletionRedactedThinkingBlock(
type="redacted_thinking",
data=content.get("data") or "",
- cache_control=content.get("cache_control", {}),
)
thinking_blocks.append(redacted_thinking_block)
diff --git a/tests/test_litellm/llms/anthropic/experimental_pass_through/adapters/test_anthropic_experimental_pass_through_adapters_transformation.py b/tests/test_litellm/llms/anthropic/experimental_pass_through/adapters/test_anthropic_experimental_pass_through_adapters_transformation.py
index 2d74c00071b..ea3b19fba2b 100644
--- a/tests/test_litellm/llms/anthropic/experimental_pass_through/adapters/test_anthropic_experimental_pass_through_adapters_transformation.py
+++ b/tests/test_litellm/llms/anthropic/experimental_pass_through/adapters/test_anthropic_experimental_pass_through_adapters_transformation.py
@@ -1,5 +1,5 @@
import base64
-from typing import Any, cast
+from typing import Any, Final, cast
import pytest
@@ -4630,3 +4630,87 @@ def test_a_bedrock_target_still_takes_output_config_not_the_declared_gate():
assert openai_request["output_config"] == {"effort": "max"}
assert "reasoning_effort" not in openai_request
assert openai_request["thinking"] == {"type": "adaptive", "display": "omitted"}
+
+
+ "client_cache_control",
+ [
+ pytest.param(None, id="client_sent_none"),
+ pytest.param({"type": "ephemeral"}, id="client_sent_one"),
+ ],
+)
+def test_thinking_blocks_never_carry_cache_control_back_to_anthropic(client_cache_control):
+ """A cache_control surviving the round trip is a `messages.N.content.0.thinking.
+ cache_control: Extra inputs are not permitted` 400 from Anthropic, whether the client
+ sent one or the adapter invented an empty one."""
+ from litellm.llms.anthropic.chat.transformation import AnthropicConfig
+
+ thinking_block: Final = {
+ "type": "thinking",
+ "thinking": "let me think",
+ "signature": "sig_abc",
+ **({"cache_control": client_cache_control} if client_cache_control is not None else {}),
+ }
+
+ openai_request, _ = LiteLLMAnthropicMessagesAdapter().translate_anthropic_to_openai(
+ {
+ "model": "claude-sonnet-5",
+ "max_tokens": 4096,
+ "messages": [
+ {"role": "user", "content": [{"type": "text", "text": "hi"}]},
+ {"role": "assistant", "content": [thinking_block, {"type": "text", "text": "hello"}]},
+ {"role": "user", "content": [{"type": "text", "text": "and now?"}]},
+ ],
+ }
+ )
+
+ translated_blocks = openai_request["messages"][1]["thinking_blocks"]
+ assert [b["type"] for b in translated_blocks] == ["thinking"]
+ assert "cache_control" not in translated_blocks[0]
+
+ outbound = AnthropicConfig().transform_request(
+ model="claude-sonnet-5",
+ messages=openai_request["messages"],
+ optional_params={"max_tokens": 4096},
+ litellm_params={},
+ headers={},
+ )
+
+ replayed = outbound["messages"][1]["content"][0]
+ assert replayed["type"] == "thinking"
+ assert "cache_control" not in replayed
+
+
+def test_redacted_thinking_blocks_never_carry_cache_control():
+ """`redacted_thinking` carries no signature and is always replayed, so it hits the
+ same Anthropic 400 as `thinking` if it picks up a cache_control on the way through."""
+ from litellm.llms.anthropic.chat.transformation import AnthropicConfig
+
+ openai_request, _ = LiteLLMAnthropicMessagesAdapter().translate_anthropic_to_openai(
+ {
+ "model": "claude-sonnet-5",
+ "max_tokens": 4096,
+ "messages": [
+ {"role": "user", "content": [{"type": "text", "text": "hi"}]},
+ {
+ "role": "assistant",
+ "content": [
+ {"type": "redacted_thinking", "data": "abc", "cache_control": {"type": "ephemeral"}},
+ {"type": "text", "text": "hello"},
+ ],
+ },
+ ],
+ }
+ )
+
+ outbound: Final = AnthropicConfig().transform_request(
+ model="claude-sonnet-5",
+ messages=openai_request["messages"],
+ optional_params={"max_tokens": 4096},
+ litellm_params={},
+ headers={},
+ )
+
+ replayed: Final = outbound["messages"][1]["content"][0]
+ assert replayed["type"] == "redacted_thinking"
+ assert "cache_control" not in replayed