:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
fix(llm-gateway): enable Opus 5 thinking for high effort (#73877)
Richard Solomoub218112104b4
diff --git a/services/llm-gateway/src/llm_gateway/anthropic_request.py b/services/llm-gateway/src/llm_gateway/anthropic_request.pyindex e1eb514102aa..a8b22119e353 100644--- a/services/llm-gateway/src/llm_gateway/anthropic_request.py+++ b/services/llm-gateway/src/llm_gateway/anthropic_request.py@@ -7,6 +7,23 @@ from typing import Any, Finalfrom llm_gateway.metrics.prometheus import CLEAR_THINKING_EDIT_DROPPEDCLEAR_THINKING_EDIT: Final[str] = "clear_thinking_20251015"+OPUS_5_REQUIRED_THINKING_EFFORTS: Final[frozenset[str]] = frozenset({"xhigh", "max"})+++def enable_required_opus_5_thinking(request_data: dict[str, Any]) -> dict[str, Any]:+ output_config = request_data.get("output_config")+ effort = output_config.get("effort") if isinstance(output_config, dict) else None+ thinking = request_data.get("thinking")++ if (+ request_data.get("model") != "claude-opus-5"+ or effort not in OPUS_5_REQUIRED_THINKING_EFFORTS+ or not isinstance(thinking, dict)+ or thinking.get("type") != "disabled"+ ):+ return request_data++ return {**request_data, "thinking": {"type": "adaptive"}}def drop_orphaned_clear_thinking(request_data: dict[str, Any], *, product: str) -> dict[str, Any]:diff --git a/services/llm-gateway/src/llm_gateway/api/anthropic.py b/services/llm-gateway/src/llm_gateway/api/anthropic.pyindex d5285250409a..fcbf4d04bc64 100644--- a/services/llm-gateway/src/llm_gateway/api/anthropic.py+++ b/services/llm-gateway/src/llm_gateway/api/anthropic.py@@ -11,7 +11,7 @@ from botocore.exceptions import ClientErrorfrom fastapi import APIRouter, HTTPException, Requestfrom fastapi.responses import StreamingResponse-from llm_gateway.anthropic_request import drop_orphaned_clear_thinking+from llm_gateway.anthropic_request import drop_orphaned_clear_thinking, enable_required_opus_5_thinkingfrom llm_gateway.api.handler import (ANTHROPIC_CONFIG,BEDROCK_CONFIG,@@ -520,6 +520,7 @@ async def _handle_anthropic_messages(product: str = "llm_gateway",) -> dict[str, Any] | StreamingResponse:data = body.model_dump(exclude_none=True, exclude=GATEWAY_ONLY_FIELDS)+ data = enable_required_opus_5_thinking(data)provider = _get_provider_from_headers(request)use_bedrock_fallback = _get_use_bedrock_fallback_from_headers(request)diff --git a/services/llm-gateway/tests/test_anthropic.py b/services/llm-gateway/tests/test_anthropic.pyindex 662f85aedb4b..b5dacb6248e9 100644--- a/services/llm-gateway/tests/test_anthropic.py+++ b/services/llm-gateway/tests/test_anthropic.py@@ -451,6 +451,32 @@ class TestAnthropicMessagesEndpoint:data = response.json()assert data["id"] == "msg_123"+ @patch("llm_gateway.api.anthropic.litellm.anthropic_messages")+ def test_wizard_opus_5_high_effort_enables_thinking(+ self,+ mock_anthropic: MagicMock,+ authenticated_client: TestClient,+ provider_request_headers: dict[str, str],+ provider_mock_response: dict,+ ) -> None:+ mock_response = MagicMock()+ mock_response.model_dump = MagicMock(return_value=provider_mock_response)+ mock_anthropic.return_value = mock_response++ response = authenticated_client.post(+ "/wizard/v1/messages",+ json={+ "model": "claude-opus-5",+ "messages": [{"role": "user", "content": "Hello"}],+ "output_config": {"effort": "xhigh"},+ "thinking": {"type": "disabled"},+ },+ headers=provider_request_headers,+ )++ assert response.status_code == 200+ assert mock_anthropic.call_args.kwargs["thinking"] == {"type": "adaptive"}+@pytest.mark.parametrize("product",[diff --git a/services/llm-gateway/tests/test_anthropic_request.py b/services/llm-gateway/tests/test_anthropic_request.pyindex d44886b2f2a9..ad8e3f9d0d38 100644--- a/services/llm-gateway/tests/test_anthropic_request.py+++ b/services/llm-gateway/tests/test_anthropic_request.py@@ -2,7 +2,7 @@ from typing import Anyimport pytest-from llm_gateway.anthropic_request import drop_orphaned_clear_thinking+from llm_gateway.anthropic_request import drop_orphaned_clear_thinking, enable_required_opus_5_thinkingfrom llm_gateway.metrics.prometheus import CLEAR_THINKING_EDIT_DROPPEDPRODUCT = "posthog_code"@@ -11,6 +11,34 @@ CLEAR_THINKING = {"type": "clear_thinking_20251015", "keep": "all"}CLEAR_TOOL_USES = {"type": "clear_tool_uses_20250919"}[email protected]("effort", ["xhigh", "max"])+def test_opus_5_high_effort_enables_adaptive_thinking(effort: str) -> None:+ request = {+ "model": "claude-opus-5",+ "output_config": {"effort": effort},+ "thinking": {"type": "disabled"},+ }++ normalized = enable_required_opus_5_thinking(request)++ assert normalized["thinking"] == {"type": "adaptive"}+ assert request["thinking"] == {"type": "disabled"}+++ ("model", "effort", "thinking"),+ [+ pytest.param("claude-opus-5", "high", {"type": "disabled"}, id="lower_effort"),+ pytest.param("claude-opus-4-8", "xhigh", {"type": "disabled"}, id="other_model"),+ pytest.param("claude-opus-5", "xhigh", {"type": "adaptive"}, id="already_adaptive"),+ ],+)+def test_other_thinking_configurations_are_untouched(model: str, effort: str, thinking: dict[str, str]) -> None:+ request = {"model": model, "output_config": {"effort": effort}, "thinking": thinking}++ assert enable_required_opus_5_thinking(request) is request++def _dropped_count(product: str) -> float:return CLEAR_THINKING_EDIT_DROPPED.labels(product=product)._value.get()