From c4413e91f3c61dca4882287cf498dd3a1b5f4cc4 Mon Sep 17 00:00:00 2001 From: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> Date: Tue, 14 Jul 2026 17:16:36 +0000 Subject: [PATCH 1/3] fix(anthropic/passthrough): drop temperature and cap thinking budget when downgrading adaptive thinking for pre-4.6 models --- .../messages/transformation.py | 53 +++++++++- .../test_anthropic_messages_effort.py | 100 ++++++++++++++++++ .../test_reasoning_effort_translation.py | 6 +- 3 files changed, 154 insertions(+), 5 deletions(-) diff --git a/litellm/llms/anthropic/experimental_pass_through/messages/transformation.py b/litellm/llms/anthropic/experimental_pass_through/messages/transformation.py index 009415877533..c708c7ca9d90 100644 --- a/litellm/llms/anthropic/experimental_pass_through/messages/transformation.py +++ b/litellm/llms/anthropic/experimental_pass_through/messages/transformation.py @@ -188,11 +188,16 @@ def validate_anthropic_messages_environment( return headers, api_base @staticmethod - def _translate_reasoning_effort_to_anthropic(model: str, optional_params: Dict, custom_llm_provider: str) -> None: + def _translate_reasoning_effort_to_anthropic( + model: str, optional_params: dict, max_tokens: int | None, custom_llm_provider: str + ) -> None: """Map OpenAI-style ``reasoning_effort`` to native Anthropic params. Caller-supplied ``thinking`` / ``output_config`` win over the alias. - ``effort='none'`` clears both. Invalid efforts raise a 400. + ``effort='none'`` clears both. Invalid efforts raise a 400. Legacy + ``budget_tokens`` is capped below ``max_tokens`` (Anthropic requires + ``max_tokens > budget_tokens``) and thinking is dropped when ``max_tokens`` + can't fit even the minimum budget. """ from litellm.exceptions import BadRequestError as _BadRequestError from litellm.llms.anthropic.chat.transformation import ( @@ -218,7 +223,12 @@ def _translate_reasoning_effort_to_anthropic(model: str, optional_params: Dict, optional_params.pop("output_config", None) return - optional_params.setdefault("thinking", mapped_thinking) + if "thinking" not in optional_params: + capped_thinking = AnthropicConfig._cap_thinking_budget_to_max_tokens(mapped_thinking, max_tokens) + if capped_thinking is not None: + optional_params["thinking"] = capped_thinking + else: + verbose_logger.warning(DROP_UNSUPPORTED_ADAPTIVE_EFFORT_WARNING, model) if AnthropicModelInfo._is_adaptive_thinking_model(model, custom_llm_provider): mapped_effort = REASONING_EFFORT_TO_OUTPUT_CONFIG_EFFORT.get(reasoning_effort) if mapped_effort is None: @@ -375,6 +385,36 @@ def _translate_adaptive_effort_for_non_adaptive_model( else: optional_params.pop("output_config", None) + @staticmethod + def _drop_incompatible_temperature_for_thinking( + model: str, optional_params: dict, custom_llm_provider: str + ) -> None: + """Anthropic rejects any ``temperature`` other than 1 while extended thinking + is enabled ("temperature may only be set to 1 when thinking is enabled"). + + Clients like Claude Code send ``thinking``/``output_config.effort`` together + with a pinned ``temperature`` (e.g. the safety classifier uses ``temperature=0`` + for determinism). When the request lands on a non-adaptive model, the effort + interface is reshaped above into legacy ``thinking={type: enabled}`` (or kept + as ``output_config.effort`` on Opus 4.5), and the leftover ``temperature`` would + 400. Preserving the thinking the caller asked for wins over an unhonorable + sampling value (Anthropic forces ``temperature=1`` under thinking regardless), + so drop it and let the API default apply. + + Adaptive models (4.6+) own this natively and are left untouched. + """ + if AnthropicModelInfo._is_adaptive_thinking_model(model, custom_llm_provider): + return + temperature = optional_params.get("temperature") + if temperature is None or temperature == 1: + return + thinking = optional_params.get("thinking") + output_config = optional_params.get("output_config") + thinking_enabled = isinstance(thinking, dict) and thinking.get("type") == "enabled" + effort_enabled = isinstance(output_config, dict) and output_config.get("effort") is not None + if thinking_enabled or effort_enabled: + optional_params.pop("temperature", None) + def transform_anthropic_messages_request( self, model: str, @@ -399,6 +439,7 @@ def transform_anthropic_messages_request( self._translate_reasoning_effort_to_anthropic( model=model, optional_params=anthropic_messages_optional_request_params, + max_tokens=max_tokens, custom_llm_provider=self._resolved_provider, ) @@ -415,6 +456,12 @@ def transform_anthropic_messages_request( custom_llm_provider=self._resolved_provider, ) + self._drop_incompatible_temperature_for_thinking( + model=model, + optional_params=anthropic_messages_optional_request_params, + custom_llm_provider=self._resolved_provider, + ) + system_param = anthropic_messages_optional_request_params.get("system") if self.should_strip_billing_metadata() and system_param is not None: filtered_system = self._filter_billing_headers_from_system(system_param) diff --git a/tests/test_litellm/llms/anthropic/experimental_pass_through/messages/test_anthropic_messages_effort.py b/tests/test_litellm/llms/anthropic/experimental_pass_through/messages/test_anthropic_messages_effort.py index 06d3effcfbbe..9491780275c4 100644 --- a/tests/test_litellm/llms/anthropic/experimental_pass_through/messages/test_anthropic_messages_effort.py +++ b/tests/test_litellm/llms/anthropic/experimental_pass_through/messages/test_anthropic_messages_effort.py @@ -2,6 +2,7 @@ from litellm.constants import ( DEFAULT_REASONING_EFFORT_HIGH_THINKING_BUDGET, + DEFAULT_REASONING_EFFORT_LOW_THINKING_BUDGET, DEFAULT_REASONING_EFFORT_MEDIUM_THINKING_BUDGET, DEFAULT_REASONING_EFFORT_XHIGH_THINKING_BUDGET, ) @@ -174,6 +175,105 @@ def test_unrecognized_effort_raises_clean_400(): assert exc_info.value.status_code == 400 +def test_pinned_temperature_dropped_when_adaptive_downgraded_to_enabled(): + """Regression (#33203): Claude Code's safety classifier sends adaptive thinking + + temperature=0 to Haiku 4.5. The adaptive interface is downgraded to legacy enabled + thinking, but Anthropic rejects "temperature may only be set to 1 when thinking is + enabled". The pinned temperature must be dropped so the request succeeds while the + downgraded thinking is preserved.""" + params = _claude_code_payload(effort="medium") + params["temperature"] = 0 + result = _transform("claude-haiku-4-5", params) + + assert result["thinking"] == { + "type": "enabled", + "budget_tokens": DEFAULT_REASONING_EFFORT_MEDIUM_THINKING_BUDGET, + } + assert "temperature" not in result + + +def test_temperature_one_preserved_with_enabled_thinking(): + """temperature=1 is compatible with extended thinking, so it must be kept.""" + params = _claude_code_payload(effort="medium") + params["temperature"] = 1 + result = _transform("claude-haiku-4-5", params) + + assert result["thinking"]["type"] == "enabled" + assert result["temperature"] == 1 + + +def test_pinned_temperature_preserved_when_thinking_dropped(): + """When thinking is dropped entirely (non-reasoning model), there is no thinking + conflict, so a pinned temperature must survive untouched.""" + params = _claude_code_payload(effort="medium") + params["temperature"] = 0 + result = _transform("claude-3-5-haiku-latest", params) + + assert "thinking" not in result + assert result["temperature"] == 0 + + +def test_pinned_temperature_preserved_for_adaptive_model(): + """Adaptive models (4.6+) own the thinking/temperature relationship natively, so + the passthrough must not strip a pinned temperature for them.""" + params = _claude_code_payload(effort="high") + params["temperature"] = 0 + result = _transform("claude-sonnet-4-6", params) + + assert result["thinking"] == {"type": "adaptive"} + assert result["temperature"] == 0 + + +def test_pinned_temperature_dropped_for_opus_4_5_effort(): + """Opus 4.5 keeps native output_config.effort (extended thinking), which is equally + incompatible with a pinned non-1 temperature, so the temperature must be dropped.""" + params = _claude_code_payload(effort="medium") + params["temperature"] = 0 + result = _transform("claude-opus-4-5", params) + + assert result["output_config"] == {"effort": "medium"} + assert "temperature" not in result + + +def test_reasoning_effort_budget_capped_below_max_tokens(): + """Sibling defect (#33203): the reasoning_effort alias mapped effort to legacy + thinking without capping budget_tokens below max_tokens, emitting + budget_tokens >= max_tokens which Anthropic rejects. A high-effort budget with + max_tokens=3000 must be capped to 2999.""" + result = _transform( + "claude-haiku-4-5", + {"max_tokens": 3000, "reasoning_effort": "high"}, + ) + + assert result["thinking"] == {"type": "enabled", "budget_tokens": 2999} + + +def test_reasoning_effort_thinking_dropped_when_max_tokens_too_small(): + """reasoning_effort thinking is dropped when max_tokens can't fit even the minimum + budget, so the request still succeeds.""" + result = _transform( + "claude-haiku-4-5", + {"max_tokens": 512, "reasoning_effort": "medium"}, + ) + + assert "thinking" not in result + + +def test_reasoning_effort_with_pinned_temperature_drops_temperature(): + """The reasoning_effort alias synthesizes legacy enabled thinking on a non-adaptive + model; a co-pinned non-1 temperature must be dropped to avoid the Anthropic 400.""" + result = _transform( + "claude-haiku-4-5", + {"max_tokens": 8192, "reasoning_effort": "low", "temperature": 0}, + ) + + assert result["thinking"] == { + "type": "enabled", + "budget_tokens": DEFAULT_REASONING_EFFORT_LOW_THINKING_BUDGET, + } + assert "temperature" not in result + + def test_non_adaptive_request_without_effort_is_untouched(): """A non-adaptive model receiving a request with no adaptive interface (no effort, no adaptive thinking) must pass through untouched.""" diff --git a/tests/test_litellm/llms/anthropic/experimental_pass_through/messages/test_reasoning_effort_translation.py b/tests/test_litellm/llms/anthropic/experimental_pass_through/messages/test_reasoning_effort_translation.py index e3f0bbbcc69c..06c33ea5b4e1 100644 --- a/tests/test_litellm/llms/anthropic/experimental_pass_through/messages/test_reasoning_effort_translation.py +++ b/tests/test_litellm/llms/anthropic/experimental_pass_through/messages/test_reasoning_effort_translation.py @@ -87,7 +87,8 @@ def test_reasoning_effort_none_clears_thinking_and_output_config(): def test_reasoning_effort_on_non_adaptive_model_uses_thinking_budget(): config = AnthropicMessagesConfig() - optional_params = {"max_tokens": 1024, "reasoning_effort": "high"} + max_tokens = 8192 + optional_params = {"max_tokens": max_tokens, "reasoning_effort": "high"} result = config.transform_anthropic_messages_request( model="claude-opus-4-5", @@ -103,7 +104,8 @@ def test_reasoning_effort_on_non_adaptive_model_uses_thinking_budget(): assert isinstance(thinking, dict) assert thinking.get("type") == "enabled" assert isinstance(thinking.get("budget_tokens"), int) - assert thinking["budget_tokens"] >= 1024 + assert thinking["budget_tokens"] == DEFAULT_REASONING_EFFORT_HIGH_THINKING_BUDGET + assert thinking["budget_tokens"] < max_tokens @pytest.mark.parametrize("bad_effort", ["invalid", "disabled", ""]) From 08618ec80017925e731a870d75057121a10293e8 Mon Sep 17 00:00:00 2001 From: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> Date: Tue, 14 Jul 2026 17:29:19 +0000 Subject: [PATCH 2/3] test(anthropic/passthrough): use sufficient max_tokens for reasoning_effort thinking mapping --- .../test_openai_like_anthropic_messages_transformation.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tests/test_litellm/llms/openai_like/messages/test_openai_like_anthropic_messages_transformation.py b/tests/test_litellm/llms/openai_like/messages/test_openai_like_anthropic_messages_transformation.py index 33e677b000ed..d5b63a123df1 100644 --- a/tests/test_litellm/llms/openai_like/messages/test_openai_like_anthropic_messages_transformation.py +++ b/tests/test_litellm/llms/openai_like/messages/test_openai_like_anthropic_messages_transformation.py @@ -250,11 +250,12 @@ def test_request_strips_advisor_blocks_when_advisor_tool_absent(config): def test_request_maps_reasoning_effort_to_thinking(config): + max_tokens = 8192 payload = config.transform_anthropic_messages_request( model="claude-sonnet-4-20250514", messages=[{"role": "user", "content": "hi"}], anthropic_messages_optional_request_params={ - "max_tokens": 1024, + "max_tokens": max_tokens, "reasoning_effort": "medium", }, litellm_params=GenericLiteLLMParams(), @@ -264,6 +265,7 @@ def test_request_maps_reasoning_effort_to_thinking(config): assert "reasoning_effort" not in payload assert isinstance(payload.get("thinking"), dict) assert payload["thinking"].get("type") == "enabled" + assert payload["thinking"]["budget_tokens"] < max_tokens def test_passthrough_disables_anthropic_beta_filtering(config): From fbb2c1c2705c2f3c9b9154ba76d682374d9a0fa0 Mon Sep 17 00:00:00 2001 From: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> Date: Tue, 14 Jul 2026 18:12:34 +0000 Subject: [PATCH 3/3] fix(anthropic/passthrough): drop incompatible temperature when downgrading adaptive thinking for pre-4.6 models Narrow the fix to the temperature reconciliation; the reasoning_effort budget cap is reverted because the live translation grid relies on budget_tokens >= max_tokens to reject unsupported effort tiers (xhigh/max) on budget-mode models, so capping turned those 400s into 200s. --- .../messages/transformation.py | 17 +++---------- .../test_anthropic_messages_effort.py | 24 ------------------- .../test_reasoning_effort_translation.py | 6 ++--- ..._like_anthropic_messages_transformation.py | 4 +--- 4 files changed, 6 insertions(+), 45 deletions(-) diff --git a/litellm/llms/anthropic/experimental_pass_through/messages/transformation.py b/litellm/llms/anthropic/experimental_pass_through/messages/transformation.py index c708c7ca9d90..05679bf39ab9 100644 --- a/litellm/llms/anthropic/experimental_pass_through/messages/transformation.py +++ b/litellm/llms/anthropic/experimental_pass_through/messages/transformation.py @@ -188,16 +188,11 @@ def validate_anthropic_messages_environment( return headers, api_base @staticmethod - def _translate_reasoning_effort_to_anthropic( - model: str, optional_params: dict, max_tokens: int | None, custom_llm_provider: str - ) -> None: + def _translate_reasoning_effort_to_anthropic(model: str, optional_params: Dict, custom_llm_provider: str) -> None: """Map OpenAI-style ``reasoning_effort`` to native Anthropic params. Caller-supplied ``thinking`` / ``output_config`` win over the alias. - ``effort='none'`` clears both. Invalid efforts raise a 400. Legacy - ``budget_tokens`` is capped below ``max_tokens`` (Anthropic requires - ``max_tokens > budget_tokens``) and thinking is dropped when ``max_tokens`` - can't fit even the minimum budget. + ``effort='none'`` clears both. Invalid efforts raise a 400. """ from litellm.exceptions import BadRequestError as _BadRequestError from litellm.llms.anthropic.chat.transformation import ( @@ -223,12 +218,7 @@ def _translate_reasoning_effort_to_anthropic( optional_params.pop("output_config", None) return - if "thinking" not in optional_params: - capped_thinking = AnthropicConfig._cap_thinking_budget_to_max_tokens(mapped_thinking, max_tokens) - if capped_thinking is not None: - optional_params["thinking"] = capped_thinking - else: - verbose_logger.warning(DROP_UNSUPPORTED_ADAPTIVE_EFFORT_WARNING, model) + optional_params.setdefault("thinking", mapped_thinking) if AnthropicModelInfo._is_adaptive_thinking_model(model, custom_llm_provider): mapped_effort = REASONING_EFFORT_TO_OUTPUT_CONFIG_EFFORT.get(reasoning_effort) if mapped_effort is None: @@ -439,7 +429,6 @@ def transform_anthropic_messages_request( self._translate_reasoning_effort_to_anthropic( model=model, optional_params=anthropic_messages_optional_request_params, - max_tokens=max_tokens, custom_llm_provider=self._resolved_provider, ) diff --git a/tests/test_litellm/llms/anthropic/experimental_pass_through/messages/test_anthropic_messages_effort.py b/tests/test_litellm/llms/anthropic/experimental_pass_through/messages/test_anthropic_messages_effort.py index 9491780275c4..5254808e3157 100644 --- a/tests/test_litellm/llms/anthropic/experimental_pass_through/messages/test_anthropic_messages_effort.py +++ b/tests/test_litellm/llms/anthropic/experimental_pass_through/messages/test_anthropic_messages_effort.py @@ -235,30 +235,6 @@ def test_pinned_temperature_dropped_for_opus_4_5_effort(): assert "temperature" not in result -def test_reasoning_effort_budget_capped_below_max_tokens(): - """Sibling defect (#33203): the reasoning_effort alias mapped effort to legacy - thinking without capping budget_tokens below max_tokens, emitting - budget_tokens >= max_tokens which Anthropic rejects. A high-effort budget with - max_tokens=3000 must be capped to 2999.""" - result = _transform( - "claude-haiku-4-5", - {"max_tokens": 3000, "reasoning_effort": "high"}, - ) - - assert result["thinking"] == {"type": "enabled", "budget_tokens": 2999} - - -def test_reasoning_effort_thinking_dropped_when_max_tokens_too_small(): - """reasoning_effort thinking is dropped when max_tokens can't fit even the minimum - budget, so the request still succeeds.""" - result = _transform( - "claude-haiku-4-5", - {"max_tokens": 512, "reasoning_effort": "medium"}, - ) - - assert "thinking" not in result - - def test_reasoning_effort_with_pinned_temperature_drops_temperature(): """The reasoning_effort alias synthesizes legacy enabled thinking on a non-adaptive model; a co-pinned non-1 temperature must be dropped to avoid the Anthropic 400.""" diff --git a/tests/test_litellm/llms/anthropic/experimental_pass_through/messages/test_reasoning_effort_translation.py b/tests/test_litellm/llms/anthropic/experimental_pass_through/messages/test_reasoning_effort_translation.py index 06c33ea5b4e1..e3f0bbbcc69c 100644 --- a/tests/test_litellm/llms/anthropic/experimental_pass_through/messages/test_reasoning_effort_translation.py +++ b/tests/test_litellm/llms/anthropic/experimental_pass_through/messages/test_reasoning_effort_translation.py @@ -87,8 +87,7 @@ def test_reasoning_effort_none_clears_thinking_and_output_config(): def test_reasoning_effort_on_non_adaptive_model_uses_thinking_budget(): config = AnthropicMessagesConfig() - max_tokens = 8192 - optional_params = {"max_tokens": max_tokens, "reasoning_effort": "high"} + optional_params = {"max_tokens": 1024, "reasoning_effort": "high"} result = config.transform_anthropic_messages_request( model="claude-opus-4-5", @@ -104,8 +103,7 @@ def test_reasoning_effort_on_non_adaptive_model_uses_thinking_budget(): assert isinstance(thinking, dict) assert thinking.get("type") == "enabled" assert isinstance(thinking.get("budget_tokens"), int) - assert thinking["budget_tokens"] == DEFAULT_REASONING_EFFORT_HIGH_THINKING_BUDGET - assert thinking["budget_tokens"] < max_tokens + assert thinking["budget_tokens"] >= 1024 @pytest.mark.parametrize("bad_effort", ["invalid", "disabled", ""]) diff --git a/tests/test_litellm/llms/openai_like/messages/test_openai_like_anthropic_messages_transformation.py b/tests/test_litellm/llms/openai_like/messages/test_openai_like_anthropic_messages_transformation.py index d5b63a123df1..33e677b000ed 100644 --- a/tests/test_litellm/llms/openai_like/messages/test_openai_like_anthropic_messages_transformation.py +++ b/tests/test_litellm/llms/openai_like/messages/test_openai_like_anthropic_messages_transformation.py @@ -250,12 +250,11 @@ def test_request_strips_advisor_blocks_when_advisor_tool_absent(config): def test_request_maps_reasoning_effort_to_thinking(config): - max_tokens = 8192 payload = config.transform_anthropic_messages_request( model="claude-sonnet-4-20250514", messages=[{"role": "user", "content": "hi"}], anthropic_messages_optional_request_params={ - "max_tokens": max_tokens, + "max_tokens": 1024, "reasoning_effort": "medium", }, litellm_params=GenericLiteLLMParams(), @@ -265,7 +264,6 @@ def test_request_maps_reasoning_effort_to_thinking(config): assert "reasoning_effort" not in payload assert isinstance(payload.get("thinking"), dict) assert payload["thinking"].get("type") == "enabled" - assert payload["thinking"]["budget_tokens"] < max_tokens def test_passthrough_disables_anthropic_beta_filtering(config):