: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
chore(posthog-ai): remove phai-llm-gateway-v2 flag and legacy gateway path (#129415)
Jonathan Mieloode976dc26056
diff --git a/ee/hogai/chat_agent/test/test_executables.py b/ee/hogai/chat_agent/test/test_executables.pyindex fa2bb6462cbe..9070e14b3c89 100644--- a/ee/hogai/chat_agent/test/test_executables.py+++ b/ee/hogai/chat_agent/test/test_executables.py@@ -1,129 +1,10 @@from posthog.test.base import BaseTest-from unittest.mock import AsyncMock, MagicMock, patch+from unittest.mock import MagicMock, patch-from langchain_core.runnables import RunnableConfigfrom parameterized import parameterized-class TestChatAgentWebSearchToolInclusion(BaseTest):- @parameterized.expand(- [- ("control", True),- ("gateway-anthropic", True),- ("gateway-bedrock", False),- ]- )- @patch("ee.hogai.core.agent_modes.toolkit.AgentToolkitManager.get_tools", new_callable=AsyncMock)- async def test_web_search_included_based_on_variant(self, variant, should_include, mock_get_tools):- from ee.hogai.chat_agent.toolkit import ChatAgentToolkitManager- from ee.hogai.utils.types.base import AssistantState-- mock_get_tools.return_value = []-- with (- patch("ee.hogai.chat_agent.toolkit.get_llm_gateway_variant", return_value=variant),- patch("ee.hogai.chat_agent.toolkit.settings") as mock_settings,- patch("ee.hogai.chat_agent.toolkit.has_mcp_gateway_feature_flag", return_value=False),- ):- mock_settings.LLM_GATEWAY_URL = "http://gateway:3308"- mock_settings.LLM_GATEWAY_API_KEY = "test-key"-- manager = ChatAgentToolkitManager(team=self.team, user=self.user, context_manager=MagicMock())- tools = await manager.get_tools(AssistantState(messages=[]), RunnableConfig(configurable={}))-- web_search_tools = [t for t in tools if isinstance(t, dict) and t.get("type") == "web_search_20250305"]-- if should_include:- self.assertEqual(len(web_search_tools), 1)- else:- self.assertEqual(len(web_search_tools), 0)---class TestChatAgentGatewayRouting(BaseTest):- @parameterized.expand(- [- (- "gateway_bedrock",- "gateway-bedrock",- {- "X-PostHog-Provider": "bedrock",- "X-POSTHOG-FLAG-phai-llm-gateway": "gateway-bedrock",- },- ),- (- "gateway_anthropic",- "gateway-anthropic",- {- "X-PostHog-Use-Bedrock-Fallback": "true",- "X-POSTHOG-FLAG-phai-llm-gateway": "gateway-anthropic",- },- ),- ]- )- @patch("ee.hogai.llm.MaxChatAnthropic.__init__", return_value=None)- @patch("ee.hogai.core.agent_modes.executables.get_llm_gateway_variant")- def test_get_model_routes_to_gateway(self, _name, variant, expected_headers, mock_get_variant, mock_model_init):- from ee.hogai.chat_agent.executables import ChatAgentExecutable- from ee.hogai.utils.types.base import AssistantState-- mock_get_variant.return_value = variant-- test_cases = [- ("http://gateway:3308", "http://gateway:3308/django"),- ("http://gateway:3308/", "http://gateway:3308/django"),- ]-- for configured_url, expected_gateway_url in test_cases:- with self.subTest(configured_url=configured_url):- with patch("ee.hogai.core.agent_modes.executables.settings") as mock_settings:- mock_settings.LLM_GATEWAY_URL = configured_url- mock_settings.LLM_GATEWAY_API_KEY = "test-key"-- executable = ChatAgentExecutable(- team=self.team,- user=self.user,- toolkit_manager_class=MagicMock(),- prompt_builder_class=MagicMock(),- node_path=(),- )-- state = AssistantState(messages=[])- executable._get_model(state, [])-- call_kwargs = mock_model_init.call_args.kwargs- self.assertEqual(call_kwargs["anthropic_api_url"], expected_gateway_url)- self.assertEqual(call_kwargs["anthropic_api_key"], "test-key")- self.assertEqual(call_kwargs["default_headers"], expected_headers)- self.assertIs(call_kwargs["bypass_proxy"], True)- self.assertEqual(call_kwargs["model"], "claude-sonnet-4-6")-- @patch("ee.hogai.llm.MaxChatAnthropic.__init__", return_value=None)- @patch(- "ee.hogai.core.agent_modes.executables.get_llm_gateway_variant",- return_value="control",- )- def test_get_model_does_not_route_when_control_variant(self, _mock_variant, mock_model_init):- from ee.hogai.chat_agent.executables import ChatAgentExecutable- from ee.hogai.utils.types.base import AssistantState-- executable = ChatAgentExecutable(- team=self.team,- user=self.user,- toolkit_manager_class=MagicMock(),- prompt_builder_class=MagicMock(),- node_path=(),- )-- state = AssistantState(messages=[])- executable._get_model(state, [])-- call_kwargs = mock_model_init.call_args.kwargs- self.assertNotIn("anthropic_api_url", call_kwargs)- self.assertNotIn("anthropic_api_key", call_kwargs)- self.assertNotIn("default_headers", call_kwargs)- self.assertIs(call_kwargs["bypass_proxy"], False)- self.assertEqual(call_kwargs["model"], "claude-sonnet-4-6")-+class TestChatAgentModelTagging(BaseTest):@parameterized.expand([("default", None, None, "product_analytics", None),@@ -133,10 +14,6 @@ class TestChatAgentGatewayRouting(BaseTest):])@patch("ee.hogai.llm.MaxChatAnthropic.__init__", return_value=None)- @patch(- "ee.hogai.core.agent_modes.executables.get_llm_gateway_variant",- return_value="control",- )def test_get_model_tags_generation_with_agent_mode(self,_name,@@ -144,7 +21,6 @@ class TestChatAgentGatewayRouting(BaseTest):supermode,expected_agent_mode,expected_supermode,- _mock_variant,mock_model_init,):from posthog.schema import AgentMode@@ -171,34 +47,3 @@ class TestChatAgentGatewayRouting(BaseTest):self.assertIn("posthog_properties", call_kwargs)self.assertEqual(call_kwargs["posthog_properties"]["agent_mode"], expected_agent_mode)self.assertEqual(call_kwargs["posthog_properties"]["supermode"], expected_supermode)-- @patch("ee.hogai.llm.MaxChatAnthropic.__init__", return_value=None)- @patch(- "ee.hogai.core.agent_modes.executables.get_llm_gateway_variant",- return_value="gateway-bedrock",- )- def test_get_model_falls_back_when_gateway_not_configured(self, _mock_variant, mock_model_init):- from ee.hogai.chat_agent.executables import ChatAgentExecutable- from ee.hogai.utils.types.base import AssistantState-- with patch("ee.hogai.core.agent_modes.executables.settings") as mock_settings:- mock_settings.LLM_GATEWAY_URL = ""- mock_settings.LLM_GATEWAY_API_KEY = ""-- executable = ChatAgentExecutable(- team=self.team,- user=self.user,- toolkit_manager_class=MagicMock(),- prompt_builder_class=MagicMock(),- node_path=(),- )-- state = AssistantState(messages=[])- executable._get_model(state, [])-- call_kwargs = mock_model_init.call_args.kwargs- self.assertNotIn("anthropic_api_url", call_kwargs)- self.assertNotIn("anthropic_api_key", call_kwargs)- self.assertNotIn("default_headers", call_kwargs)- self.assertIs(call_kwargs["bypass_proxy"], False)- self.assertEqual(call_kwargs["model"], "claude-sonnet-4-6")diff --git a/ee/hogai/chat_agent/toolkit.py b/ee/hogai/chat_agent/toolkit.pyindex d319fc748afb..35472dc5a275 100644--- a/ee/hogai/chat_agent/toolkit.py+++ b/ee/hogai/chat_agent/toolkit.py@@ -2,8 +2,6 @@ import asynciofrom collections.abc import Awaitablefrom typing import Any-from django.conf import settings-from langchain_core.runnables import RunnableConfigfrom products.skills.backend.tools.skills import GetLLMSkillFileTool, GetLLMSkillTool, ListLLMSkillsTool@@ -36,7 +34,6 @@ from ee.hogai.tools import (from ee.hogai.tools.call_mcp_server.tool import CallMCPServerToolfrom ee.hogai.tools.finalize_plan.tool import FinalizePlanToolfrom ee.hogai.utils.feature_flags import (- get_llm_gateway_variant,has_mcp_gateway_feature_flag,has_memory_tool_feature_flag,has_phai_tasks_feature_flag,@@ -148,12 +145,6 @@ class ChatAgentToolkitManager(AgentToolkitManager):if mcp_tool._installations:available_tools.append(mcp_tool)- # Web Search isn't supported by AWS Bedrock as primary provider- variant = get_llm_gateway_variant(self._team, self._user)- uses_bedrock_primary = (- variant == "gateway-bedrock" and settings.LLM_GATEWAY_URL and settings.LLM_GATEWAY_API_KEY- )- if not uses_bedrock_primary:- available_tools.append({"type": "web_search_20250305", "name": "web_search", "max_uses": 5})+ available_tools.append({"type": "web_search_20250305", "name": "web_search", "max_uses": 5})return available_toolsdiff --git a/ee/hogai/core/agent_modes/executables.py b/ee/hogai/core/agent_modes/executables.pyindex b9cd2c8dbf30..eebabf3f28b5 100644--- a/ee/hogai/core/agent_modes/executables.py+++ b/ee/hogai/core/agent_modes/executables.py@@ -3,8 +3,6 @@ from collections.abc import Mapping, Sequencefrom typing import Any, Literal, TypeVar, castfrom uuid import uuid4-from django.conf import settings-import structlogimport posthoganalyticsfrom langchain_core.messages import (@@ -50,7 +48,6 @@ from ee.hogai.tool import MaxTool, ToolMessagesArtifactfrom ee.hogai.tool_errors import MaxToolErrorfrom ee.hogai.utils.anthropic import add_cache_control, convert_to_anthropic_messagesfrom ee.hogai.utils.conversation_summarizer import AnthropicConversationSummarizer-from ee.hogai.utils.feature_flags import get_llm_gateway_variantfrom ee.hogai.utils.helpers import convert_tool_messages_to_dict, normalize_ai_messagefrom ee.hogai.utils.types import (AssistantMessageUnion,@@ -227,37 +224,6 @@ class AgentExecutable(BaseAgentLoopRootExecutable):for tool_call in last_message.tool_calls]- def _get_llm_gateway_product(self) -> str:- return "django"-- def _get_gateway_kwargs(self) -> dict[str, Any]:- variant = get_llm_gateway_variant(self._team, self._user)- if variant == "control":- return {}- if not settings.LLM_GATEWAY_URL or not settings.LLM_GATEWAY_API_KEY:- logger.warning(- "llm_gateway settings are not configured",- product=self._get_llm_gateway_product(),- team_id=self._team.id,- variant=variant,- )- return {}-- headers: dict[str, str] = {- "X-POSTHOG-FLAG-phai-llm-gateway": variant,- }-- if variant == "gateway-bedrock":- headers["X-PostHog-Provider"] = "bedrock"- elif variant == "gateway-anthropic":- headers["X-PostHog-Use-Bedrock-Fallback"] = "true"-- return {- "anthropic_api_url": f"{settings.LLM_GATEWAY_URL.rstrip('/')}/{self._get_llm_gateway_product()}",- "anthropic_api_key": settings.LLM_GATEWAY_API_KEY,- "default_headers": headers,- }-def _get_agent_mode_posthog_properties(self, state: AssistantState) -> dict[str, Any]:"""Telemetry props so $ai_generation events can be scoped to a specific agent mode."""supermode = state.supermode@@ -297,8 +263,7 @@ class AgentExecutable(BaseAgentLoopRootExecutable):if resolve_ai_gateway_config() is not None:base_model = MaxChatAnthropic(**model_kwargs)else:- gateway_kwargs = self._get_gateway_kwargs()- base_model = MaxChatAnthropic(**model_kwargs, bypass_proxy=bool(gateway_kwargs), **gateway_kwargs)+ base_model = MaxChatAnthropic(**model_kwargs)# The agent can operate in loops. Since insight building is an expensive operation, we want to limit a recursion depth.# This will remove the functions, so the agent doesn't have any other option but to exit.diff --git a/ee/hogai/core/agent_modes/test/test_executables.py b/ee/hogai/core/agent_modes/test/test_executables.pyindex 3d1e0050f413..707e30f2b7fd 100644--- a/ee/hogai/core/agent_modes/test/test_executables.py+++ b/ee/hogai/core/agent_modes/test/test_executables.py@@ -373,10 +373,6 @@ class TestAgentNode(ClickhouseTestMixin, BaseTest):"ee.hogai.core.agent_modes.compaction_manager.AnthropicConversationCompactionManager.calculate_token_count",return_value=1000,),- patch(- "ee.hogai.core.agent_modes.executables.get_llm_gateway_variant",- return_value="control",- ),):node = _create_agent_node(self.team, self.user)@@ -1247,11 +1243,8 @@ class TestAgentNodeModelRouting(BaseTest):@override_settings(AI_GATEWAY_URL="https://ai-gateway.test/v1",AI_GATEWAY_API_KEY="phs_gateway",- LLM_GATEWAY_URL="http://llm-gateway.test",- LLM_GATEWAY_API_KEY="legacy-key",)- @patch("ee.hogai.core.agent_modes.executables.get_llm_gateway_variant", return_value="gateway-bedrock")- def test_root_model_routes_through_the_ai_gateway_when_configured(self, mock_variant):+ def test_root_model_routes_through_the_ai_gateway_when_configured(self):root_model = self._root_model()self.assertEqual(root_model.anthropic_api_url, "https://ai-gateway.test")@@ -1259,20 +1252,15 @@ class TestAgentNodeModelRouting(BaseTest):assert isinstance(root_model.ai_gateway_fallback, MaxChatAnthropic)self.assertEqual(root_model.ai_gateway_fallback.model, "claude-sonnet-4-6")self.assertTrue(root_model.billable)- mock_variant.assert_not_called()@override_settings(AI_GATEWAY_URL="",AI_GATEWAY_API_KEY="",- LLM_GATEWAY_URL="http://llm-gateway.test",- LLM_GATEWAY_API_KEY="legacy-key",)- @patch("ee.hogai.core.agent_modes.executables.get_llm_gateway_variant", return_value="gateway-bedrock")- def test_root_model_keeps_the_legacy_gateway_arm_without_ai_gateway_config(self, _mock_variant):+ def test_root_model_goes_direct_to_anthropic_without_ai_gateway_config(self):root_model = self._root_model()self.assertIsNone(root_model.ai_gateway_fallback)- self.assertEqual(root_model.anthropic_api_url, "http://llm-gateway.test/django")- assert root_model.default_headers is not None- self.assertEqual(root_model.default_headers["X-PostHog-Provider"], "bedrock")- self.assertTrue(root_model.bypass_proxy)+ self.assertEqual(root_model.anthropic_api_url, "https://api.anthropic.com")+ self.assertIsNone(root_model.default_headers)+ self.assertFalse(root_model.bypass_proxy)diff --git a/ee/hogai/utils/feature_flags.py b/ee/hogai/utils/feature_flags.pyindex a6fff10d6a6e..3964c66ebb0f 100644--- a/ee/hogai/utils/feature_flags.py+++ b/ee/hogai/utils/feature_flags.py@@ -1,15 +1,8 @@-from typing import Literal, cast--import posthoganalytics-from posthog.models import Team, Userfrom posthog.ph_client import feature_enabled_or_falsefrom products.business_knowledge.backend.logic import has_feature_flag as bk_has_feature_flag-LlmGatewayVariant = Literal["control", "gateway-anthropic", "gateway-bedrock"]-_VALID_LLM_GATEWAY_VARIANTS: set[str] = {"control", "gateway-anthropic", "gateway-bedrock"}-def is_privacy_mode_enabled(team: Team) -> bool:"""@@ -145,22 +138,6 @@ def has_customer_analytics_mode_feature_flag(team: Team, user: User) -> bool:)-def get_llm_gateway_variant(team: Team, user: User) -> LlmGatewayVariant:- variant = cast(- "str | bool | None",- posthoganalytics.get_feature_flag(- "phai-llm-gateway-v2",- str(user.distinct_id),- groups={"organization": str(team.organization_id)},- group_properties={"organization": {"id": str(team.organization_id)}},- send_feature_flag_events=False,- ),- )- if isinstance(variant, str) and variant in _VALID_LLM_GATEWAY_VARIANTS:- return cast("LlmGatewayVariant", variant)- return "control"--def has_business_knowledge_feature_flag(team: Team) -> bool:# Canonical check lives in the owning product; kept as a delegate so existing# ee call sites and test patches don't move.