AutoGPT is the vision of accessible AI for everyone, to use and to build on. Our mission is to provide the tools, so that you can focus on what matters.
refactor(backend): drop Anthropic auto-reroute through OpenRouter (#13041)
Zamil Majdybd886bb7a85e
diff --git a/autogpt_platform/backend/backend/blocks/llm.py b/autogpt_platform/backend/backend/blocks/llm.pyindex 19c004c6b3..6d2d04c3ed 100644--- a/autogpt_platform/backend/backend/blocks/llm.py+++ b/autogpt_platform/backend/backend/blocks/llm.py@@ -925,21 +925,6 @@ async def llm_call(provider = llm_model.metadata.providercontext_window = llm_model.context_window- # Transparent OpenRouter routing for Anthropic models: when an OpenRouter- # API key is configured, route direct-Anthropic models through OpenRouter- # instead. This gives us per-request USD cost for free, so provider_cost- # is populated without manual token-rate arithmetic.- or_key = settings.secrets.open_router_api_key- or_model_id: str | None = None- if provider == "anthropic" and or_key:- provider = "open_router"- credentials = APIKeyCredentials(- provider=ProviderName.OPEN_ROUTER,- title="OpenRouter (auto)",- api_key=SecretStr(or_key),- )- or_model_id = f"anthropic/{llm_model.value}"-if compress_prompt_to_fit:result = await compress_context(messages=prompt,@@ -1200,7 +1185,7 @@ async def llm_call(# Ask OpenRouter to include the per-request USD cost on the usage# object. Same shape used by simulator.py — keep aligned.extra_body={"usage": {"include": True}},- model=or_model_id or llm_model.value,+ model=llm_model.value,messages=cast(list[ChatCompletionMessageParam], prompt),max_tokens=max_tokens,tools=(diff --git a/autogpt_platform/backend/backend/blocks/test/test_llm.py b/autogpt_platform/backend/backend/blocks/test/test_llm.pyindex 45e9be60ee..3acc8d16ed 100644--- a/autogpt_platform/backend/backend/blocks/test/test_llm.py+++ b/autogpt_platform/backend/backend/blocks/test/test_llm.py@@ -28,14 +28,7 @@ class TestLLMStatsTracking:mock_response.output = []mock_response.usage = MagicMock(input_tokens=10, output_tokens=20)- # Test with mocked OpenAI response. Suppress the OpenRouter key so the- # auto-reroute branch doesn't divert this test to the chat.completions- # path under a local .env that has open_router_api_key set.- with (- patch("openai.AsyncOpenAI") as mock_openai,- patch("backend.blocks.llm.settings") as mock_settings,- ):- mock_settings.secrets.open_router_api_key = ""+ with patch("openai.AsyncOpenAI") as mock_openai:mock_client = AsyncMock()mock_openai.return_value = mock_clientmock_client.responses.create = AsyncMock(return_value=mock_response)@@ -83,11 +76,7 @@ class TestLLMStatsTracking:mock_response.usage = mock_usagemock_response.stop_reason = "end_turn"- with (- patch("anthropic.AsyncAnthropic") as mock_anthropic,- patch("backend.blocks.llm.settings") as mock_settings,- ):- mock_settings.secrets.open_router_api_key = ""+ with patch("anthropic.AsyncAnthropic") as mock_anthropic:mock_client = AsyncMock()mock_anthropic.return_value = mock_clientmock_client.messages.create = AsyncMock(return_value=mock_response)@@ -106,56 +95,6 @@ class TestLLMStatsTracking:assert response.cache_creation_tokens == 50assert response.response == "Test anthropic response"- @pytest.mark.asyncio- async def test_anthropic_routes_through_openrouter_when_key_present(self):- """When open_router_api_key is set, Anthropic models route via OpenRouter."""- from pydantic import SecretStr-- import backend.blocks.llm as llm- from backend.data.model import APIKeyCredentials-- anthropic_creds = APIKeyCredentials(- id="test-anthropic-id",- provider="anthropic",- api_key=SecretStr("mock-anthropic-key"),- title="Mock Anthropic key",- )-- mock_choice = MagicMock()- mock_choice.message.content = "routed response"- mock_choice.message.tool_calls = None-- mock_usage = MagicMock()- mock_usage.prompt_tokens = 10- mock_usage.completion_tokens = 5-- mock_response = MagicMock()- mock_response.choices = [mock_choice]- mock_response.usage = mock_usage-- mock_create = AsyncMock(return_value=mock_response)-- with (- patch("openai.AsyncOpenAI") as mock_openai,- patch("backend.blocks.llm.settings") as mock_settings,- ):- mock_settings.secrets.open_router_api_key = "sk-or-test-key"- mock_client = MagicMock()- mock_openai.return_value = mock_client- mock_client.chat.completions.create = mock_create-- await llm.llm_call(- credentials=anthropic_creds,- llm_model=llm.LlmModel.CLAUDE_3_HAIKU,- prompt=[{"role": "user", "content": "Hello"}],- max_tokens=100,- )-- # Verify OpenAI client was used (not Anthropic SDK) and model was prefixed- mock_openai.assert_called_once()- call_kwargs = mock_create.call_args.kwargs- assert call_kwargs["model"] == "anthropic/claude-3-haiku-20240307"-@pytest.mark.asyncioasync def test_ai_structured_response_block_tracks_stats(self):"""Test that AIStructuredResponseGeneratorBlock correctly tracks stats."""@@ -529,11 +468,7 @@ class TestLLMStatsTracking:mock_response.usage = MagicMock(input_tokens=50, output_tokens=30)return mock_response- with (- patch("openai.AsyncOpenAI") as mock_openai,- patch("backend.blocks.llm.settings") as mock_settings,- ):- mock_settings.secrets.open_router_api_key = ""+ with patch("openai.AsyncOpenAI") as mock_openai:mock_client = AsyncMock()mock_openai.return_value = mock_clientmock_client.responses.create = mock_create@@ -1309,16 +1244,6 @@ class TestAnthropicCacheControl:"""Verify that llm_call attaches cache_control to the system prompt blockand to the last tool definition when calling the Anthropic API."""- @pytest.fixture(autouse=True)- def disable_openrouter_routing(self):- """Ensure tests exercise the direct-Anthropic path by suppressing the- OpenRouter API key. Without this, a local .env with OPEN_ROUTER_API_KEY- set would silently reroute all Anthropic calls through OpenRouter,- bypassing the cache_control code under test."""- with patch("backend.blocks.llm.settings") as mock_settings:- mock_settings.secrets.open_router_api_key = ""- yield mock_settings-def _make_anthropic_credentials(self) -> llm.APIKeyCredentials:from pydantic import SecretStr