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.

chore(posthog-ai): drop the legacy gateway fallback from turn suggestion drafts (#128206)

Jonathan Mielooca7bf5ae58e4
diff --git a/products/posthog_ai/backend/tests/test_turn_suggestions.py b/products/posthog_ai/backend/tests/test_turn_suggestions.py
index 38617672c804..20bbb063e181 100644
--- a/products/posthog_ai/backend/tests/test_turn_suggestions.py
+++ b/products/posthog_ai/backend/tests/test_turn_suggestions.py
@@ -983,7 +983,9 @@ class TestDraftScout(SimpleTestCase):
def _draft(self, content: str) -> tuple[ScoutDraft | None, MagicMock]:
client = _gateway_reply({})
client.chat.completions.create.return_value = MagicMock(choices=[MagicMock(message=MagicMock(content=content))])
- with patch("products.posthog_ai.backend.turn_suggestions.drafter.build_openai_client", return_value=client):
+ with patch(
+ "products.posthog_ai.backend.turn_suggestions.drafter.build_ai_gateway_openai_client", return_value=client
+ ):
draft = draft_scout(
build_turn_transcript(_metric_turn()),
team_id=1,
@@ -1022,26 +1024,11 @@ class TestDraftScout(SimpleTestCase):
assert draft is None
- @parameterized.expand(
- [
- (
- "go_gateway",
- {"AI_GATEWAY_URL": "https://ai-gateway.example.com/v1", "AI_GATEWAY_API_KEY": "phs_test"},
- "https://ai-gateway.example.com/v1/",
- ),
- # The posthog_ai route bills the customer's AI credits, so the fallback must use an unbilled one.
- (
- "python_fallback",
- {"LLM_GATEWAY_URL": "http://llm-gateway.example.com", "LLM_GATEWAY_API_KEY": "phx_test"},
- "http://llm-gateway.example.com/growth/v1/",
- ),
- ]
- )
- def test_drafts_are_billed_to_posthog_on_either_gateway(self, _name: str, gateway: dict, base_url: str):
- unset = {"AI_GATEWAY_URL": "", "AI_GATEWAY_API_KEY": "", "LLM_GATEWAY_URL": "", "LLM_GATEWAY_API_KEY": ""}
+ def test_drafts_are_billed_to_posthog_on_the_go_gateway(self):
+ gateway = {"AI_GATEWAY_URL": "https://ai-gateway.example.com/v1", "AI_GATEWAY_API_KEY": "phs_test"}
reply = MagicMock(choices=[MagicMock(message=MagicMock(content="{}"))])
with (
- override_settings(**{**unset, **gateway}),
+ override_settings(**gateway),
patch.object(Completions, "create", autospec=True, return_value=reply) as create,
):
draft_scout(
@@ -1053,12 +1040,24 @@ class TestDraftScout(SimpleTestCase):
)
client = create.call_args.args[0]._client
- assert str(client.base_url) == base_url
+ assert str(client.base_url) == "https://ai-gateway.example.com/v1/"
assert create.call_args.kwargs["user"] == "team-7"
- def test_an_unconfigured_gateway_returns_none(self):
+ @parameterized.expand(
+ [
+ ("no_gateway", {}),
+ (
+ "python_gateway_only",
+ {"LLM_GATEWAY_URL": "http://llm-gateway.example.com", "LLM_GATEWAY_API_KEY": "phx_test"},
+ ),
+ ]
+ )
+ def test_an_unconfigured_go_gateway_returns_none(self, _name: str, gateway: dict):
unset = {"AI_GATEWAY_URL": "", "AI_GATEWAY_API_KEY": "", "LLM_GATEWAY_URL": "", "LLM_GATEWAY_API_KEY": ""}
- with override_settings(**unset):
+ with (
+ override_settings(**{**unset, **gateway}),
+ patch.object(Completions, "create", autospec=True) as create,
+ ):
draft = draft_scout(
build_turn_transcript(_metric_turn()),
team_id=1,
@@ -1068,6 +1067,7 @@ class TestDraftScout(SimpleTestCase):
)
assert draft is None
+ create.assert_not_called()
class TestEnqueueTurnSuggestion(BaseTest):
diff --git a/products/posthog_ai/backend/turn_suggestions/drafter.py b/products/posthog_ai/backend/turn_suggestions/drafter.py
index 2790a350fb53..045db62d146e 100644
--- a/products/posthog_ai/backend/turn_suggestions/drafter.py
+++ b/products/posthog_ai/backend/turn_suggestions/drafter.py
@@ -9,11 +9,13 @@ never decides.
from datetime import date
from typing import Any, TypeVar
+from django.conf import settings
+
import structlog
from openai.types.shared_params import ResponseFormatJSONSchema
from pydantic import BaseModel, ConfigDict, ValidationError
-from posthog.llm.gateway_client import Product, build_openai_client, team_distinct_id
+from posthog.llm.gateway_client import build_ai_gateway_openai_client, team_distinct_id
from posthog.llm.semantic_enrichment import extract_json_object
from products.posthog_ai.backend.turn_suggestions.transcript import TurnTranscript, truncate_text
@@ -31,8 +33,7 @@ logger = structlog.get_logger(__name__)
DRAFT_MODEL = "gpt-6-luna"
# PostHog pays for drafts, not the customer's AI credits. The Go gateway bills the wallet of the team
-# that owns its key, and the Python fallback route is one that bills no credit bucket.
-DRAFT_FALLBACK_PRODUCT: Product = "growth"
+# that owns its key.
DRAFT_AI_PRODUCT = "posthog_ai_turn_suggestions"
# A reasoning model spends the same token budget on its thinking and on the reply, and the reply
@@ -121,8 +122,8 @@ def _complete(
try:
# Inside the guard: an instance without a configured gateway raises here, and a failed draft offers nothing.
distinct_id = team_distinct_id(team_id)
- client = build_openai_client(
- DRAFT_FALLBACK_PRODUCT,
+ client = build_ai_gateway_openai_client(
+ settings.AI_GATEWAY_API_KEY,
ai_product=DRAFT_AI_PRODUCT,
properties={"team_id": str(team_id)},
distinct_id=distinct_id,