Skip to content

: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.

feat(mcp-analytics): route llm calls through the ai gateway (#128189)

Jonathan Mieloo579d3bf48b5a
diff --git a/products/mcp_analytics/backend/failure_classification.py b/products/mcp_analytics/backend/failure_classification.py
index 15d50aa6a508..afc3cdce6d37 100644
--- a/products/mcp_analytics/backend/failure_classification.py
+++ b/products/mcp_analytics/backend/failure_classification.py
@@ -8,15 +8,13 @@ pattern in ``intent_generation.py``.
import re
from enum import StrEnum
-from django.conf import settings
-
import openai
import structlog
-import posthoganalytics
-from posthoganalytics.ai.openai import OpenAI
+from openai import OpenAI
from pydantic import BaseModel, ValidationError
from posthog.clickhouse.query_tagging import Feature, Product, tags_context
+from posthog.llm.gateway_client import build_openai_client, resolve_ai_gateway_config
from posthog.models.team.team import Team
logger = structlog.get_logger(__name__)
@@ -160,7 +158,6 @@ def _build_batch_prompt(messages: list[str]) -> str:
def _call_llm(client: OpenAI, messages: list[str], team: Team) -> ClassificationBatch:
response = client.chat.completions.create(
model=FAILURE_CLASS_MODEL,
- temperature=0,
timeout=30,
messages=[
{"role": "system", "content": SYSTEM_PROMPT},
@@ -168,7 +165,6 @@ def _call_llm(client: OpenAI, messages: list[str], team: Team) -> Classification
],
response_format={"type": "json_object"},
user=f"team/{team.id}/mcp-analytics-failure-classifier",
- posthog_properties={"ai_product": "mcp_analytics", "ai_feature": "failure-classification"},
)
content = response.choices[0].message.content if response.choices else None
if not content:
@@ -216,7 +212,14 @@ def classify_fingerprints(messages_by_fingerprint: dict[str, str], team: Team) -
if not team.organization.is_ai_data_processing_approved:
raise FailureClassificationUnavailable("AI data processing is not approved for this organization")
- client = OpenAI(posthog_client=posthoganalytics.default_client, base_url=settings.OPENAI_BASE_URL)
+ if resolve_ai_gateway_config() is None:
+ raise FailureClassificationUnavailable("AI gateway is not configured")
+ client = build_openai_client(
+ "django",
+ ai_product="mcp_analytics",
+ properties={"ai_feature": "failure-classification"},
+ distinct_id=f"team/{team.id}/mcp-analytics-failure-classifier",
+ )
items = list(messages_by_fingerprint.items())
result: dict[str, str] = {}
with tags_context(
diff --git a/products/mcp_analytics/backend/intent_generation.py b/products/mcp_analytics/backend/intent_generation.py
index f3caca1f0188..8bc044ff4ef0 100644
--- a/products/mcp_analytics/backend/intent_generation.py
+++ b/products/mcp_analytics/backend/intent_generation.py
@@ -7,15 +7,11 @@ generation only — caching and persistence live in ``logic.generate_session_int
from datetime import datetime, timedelta
-from django.conf import settings
from django.utils import timezone
import openai
import structlog
-import posthoganalytics
from google.genai.types import GenerateContentConfig
-from posthoganalytics.ai.gemini import genai
-from posthoganalytics.ai.openai import OpenAI
from pydantic import BaseModel, Field, ValidationError
from posthog.hogql import ast
@@ -23,6 +19,7 @@ from posthog.hogql.parser import parse_select
from posthog.hogql.query import execute_hogql_query
from posthog.clickhouse.query_tagging import Feature, Product, tags_context
+from posthog.llm.gateway_client import build_gemini_client, build_openai_client, resolve_ai_gateway_config
from posthog.models.team.team import Team
from products.mcp_analytics.backend.constants import MCP_TOOL_CALL_EVENT
@@ -92,11 +89,11 @@ def _build_user_prompt(intents: list[str]) -> str:
return f"Per-tool-call intents (chronological):\n{numbered}\n\nSummarise the agent's overall goal in at most two short, concrete sentences."
-def _ensure_llm_available(team: Team, *, api_key: str, key_name: str) -> None:
+def _ensure_llm_available(team: Team, *, configured: bool) -> None:
"""Intents are user-authored text, so sending them to the LLM requires the
organization's AI data processing consent, same as every other AI-backed flow."""
- if not api_key:
- raise IntentGenerationUnavailable(f"{key_name} is not configured")
+ if not configured:
+ raise IntentGenerationUnavailable("AI gateway is not configured")
if not team.organization.is_ai_data_processing_approved:
raise IntentGenerationUnavailable("AI data processing is not approved for this organization")
@@ -108,9 +105,15 @@ def summarize_intents(intents: list[str], team: Team) -> str:
has not approved AI data processing, or the request fails, so the endpoint can answer
with a clean 503 rather than a 500.
"""
- _ensure_llm_available(team, api_key=settings.OPENAI_API_KEY, key_name="OPENAI_API_KEY")
-
- client = OpenAI(posthog_client=posthoganalytics.default_client, base_url=settings.OPENAI_BASE_URL)
+ _ensure_llm_available(team, configured=resolve_ai_gateway_config() is not None)
+
+ distinct_id = f"team/{team.id}/mcp-analytics-session-intent"
+ client = build_openai_client(
+ "django",
+ ai_product="mcp_analytics",
+ properties={"ai_feature": "session-intent-generation"},
+ distinct_id=distinct_id,
+ )
try:
response = client.chat.completions.create(
model=SESSION_INTENT_MODEL,
@@ -121,8 +124,7 @@ def summarize_intents(intents: list[str], team: Team) -> str:
{"role": "system", "content": SYSTEM_PROMPT},
{"role": "user", "content": _build_user_prompt(intents)},
],
- user=f"team/{team.id}/mcp-analytics-session-intent",
- posthog_properties={"ai_product": "mcp_analytics", "ai_feature": "session-intent-generation"},
+ user=distinct_id,
)
except openai.OpenAIError as e:
raise IntentGenerationUnavailable("LLM request failed") from e
@@ -235,13 +237,14 @@ def summarize_project_intents(intents: list[tuple[str, str]], team: Team) -> Int
has not approved AI data processing, or the request fails, so the endpoint can answer
with a clean 503 rather than a 500.
"""
- _ensure_llm_available(team, api_key=settings.GEMINI_API_KEY, key_name="GEMINI_API_KEY")
-
- client = genai.Client(
- api_key=settings.GEMINI_API_KEY,
- posthog_client=posthoganalytics.default_client,
- http_options={"timeout": DIGEST_TIMEOUT_MS},
+ client = build_gemini_client(
+ ai_product="mcp_analytics",
+ properties={"ai_feature": "activity-intent-digest"},
+ distinct_id=f"team/{team.id}/mcp-analytics-intent-digest",
+ timeout_ms=DIGEST_TIMEOUT_MS,
)
+ _ensure_llm_available(team, configured=client is not None)
+ assert client is not None
config = GenerateContentConfig(
system_instruction=DIGEST_SYSTEM_PROMPT,
response_mime_type="application/json",
@@ -253,9 +256,6 @@ def summarize_project_intents(intents: list[tuple[str, str]], team: Team) -> Int
model=DIGEST_THEMES_MODEL,
contents=_build_digest_prompt(intents),
config=config,
- posthog_distinct_id=f"team/{team.id}/mcp-analytics-intent-digest",
- posthog_properties={"ai_product": "mcp_analytics", "ai_feature": "activity-intent-digest"},
- posthog_groups={"project": str(team.id)},
)
except Exception as e:
# Broad: the Gemini client surfaces transport, timeout, and API errors as unrelated types,
diff --git a/products/mcp_analytics/backend/tests/test_api.py b/products/mcp_analytics/backend/tests/test_api.py
index d295d1aeb823..6a6af850ca11 100644
--- a/products/mcp_analytics/backend/tests/test_api.py
+++ b/products/mcp_analytics/backend/tests/test_api.py
@@ -766,8 +766,8 @@ class TestResolveIntentThemes(SimpleTestCase):
class TestLLMConsentGate(APIBaseTest):
@parameterized.expand(
[
- ("session_summary", intent_generation.summarize_intents, "OpenAI"),
- ("project_digest", intent_generation.summarize_project_intents, "genai"),
+ ("session_summary", intent_generation.summarize_intents, "build_openai_client"),
+ ("project_digest", intent_generation.summarize_project_intents, "build_gemini_client"),
]
)
def test_refuses_without_ai_data_processing_consent(self, _name, summarize, client_attr: str) -> None:
@@ -775,12 +775,12 @@ class TestLLMConsentGate(APIBaseTest):
self.organization.save()
with (
- self.settings(OPENAI_API_KEY="sk-test", GEMINI_API_KEY="gem-test"),
+ self.settings(AI_GATEWAY_URL="https://gateway.example.com/v1", AI_GATEWAY_API_KEY="phs_test"),
patch.object(intent_generation, client_attr) as mock_client,
self.assertRaises(contracts.IntentGenerationUnavailable),
):
summarize([("find the signups funnel", "query_run")], self.team)
- mock_client.assert_not_called()
+ assert mock_client.return_value.mock_calls == []
class TestActivityOverview(_MCPAnalyticsTeamScopedTestMixin, ClickhouseTestMixin, APIBaseTest):
diff --git a/products/mcp_analytics/backend/tests/test_failure_classification.py b/products/mcp_analytics/backend/tests/test_failure_classification.py
index 837a13090ab2..60b3cdd8c249 100644
--- a/products/mcp_analytics/backend/tests/test_failure_classification.py
+++ b/products/mcp_analytics/backend/tests/test_failure_classification.py
@@ -9,6 +9,8 @@ import json
from posthog.test.base import BaseTest
from unittest.mock import MagicMock, patch
+from django.test import override_settings
+
from parameterized import parameterized
from products.mcp_analytics.backend.failure_classification import (
@@ -68,6 +70,7 @@ def _classification_payload(fingerprints: list[str], failure_class: str) -> str:
)
+@override_settings(AI_GATEWAY_URL="https://gateway.example.com/v1", AI_GATEWAY_API_KEY="phs_test")
class TestClassifyFingerprints(BaseTest):
def setUp(self) -> None:
super().setUp()
@@ -77,7 +80,7 @@ class TestClassifyFingerprints(BaseTest):
def test_raises_without_ai_processing_consent(self) -> None:
self.organization.is_ai_data_processing_approved = False
self.organization.save()
- with patch("products.mcp_analytics.backend.failure_classification.OpenAI") as mock_openai_cls:
+ with patch("products.mcp_analytics.backend.failure_classification.build_openai_client") as mock_openai_cls:
with self.assertRaises(FailureClassificationUnavailable):
classify_fingerprints({"a fingerprint": "a raw message"}, self.team)
@@ -96,7 +99,7 @@ class TestClassifyFingerprints(BaseTest):
]
}
)
- with patch("products.mcp_analytics.backend.failure_classification.OpenAI") as mock_openai_cls:
+ with patch("products.mcp_analytics.backend.failure_classification.build_openai_client") as mock_openai_cls:
mock_create = mock_openai_cls.return_value.chat.completions.create
mock_create.return_value = _openai_response(payload)
result = classify_fingerprints(items, self.team)
@@ -110,7 +113,7 @@ class TestClassifyFingerprints(BaseTest):
def test_batches_at_25_fingerprints(self) -> None:
items = {f"fingerprint {i}": f"raw message {i}" for i in range(CLASSIFY_BATCH_SIZE + 1)}
keys = list(items)
- with patch("products.mcp_analytics.backend.failure_classification.OpenAI") as mock_openai_cls:
+ with patch("products.mcp_analytics.backend.failure_classification.build_openai_client") as mock_openai_cls:
mock_create = mock_openai_cls.return_value.chat.completions.create
mock_create.side_effect = [
_openai_response(_classification_payload(keys[:CLASSIFY_BATCH_SIZE], "internal_error")),
@@ -137,7 +140,7 @@ class TestClassifyFingerprints(BaseTest):
) -> None:
items = {"some fingerprint": "some raw message"}
payload = json.dumps({"classifications": classifications})
- with patch("products.mcp_analytics.backend.failure_classification.OpenAI") as mock_openai_cls:
+ with patch("products.mcp_analytics.backend.failure_classification.build_openai_client") as mock_openai_cls:
mock_create = mock_openai_cls.return_value.chat.completions.create
mock_create.side_effect = [_openai_response(payload), _openai_response(payload)]
result = classify_fingerprints(items, self.team)