: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
feat(tasks): admit served catalog models in first-party gateway pins (#116025)
Alessandro Pogliaghif0091ab23adb
diff --git a/products/tasks/backend/logic/services/gateway_model_pin.py b/products/tasks/backend/logic/services/gateway_model_pin.pyindex 790677b05884..00b0ee01ba10 100644--- a/products/tasks/backend/logic/services/gateway_model_pin.py+++ b/products/tasks/backend/logic/services/gateway_model_pin.py@@ -11,11 +11,22 @@ GATEWAY_PRODUCT_STATE_KEY = "ai_gateway_product"# subagent's bare `sonnet` alias resolves to. The catalog does not offer them for selection.SDK_IMPLICIT_MODELS: tuple[str, ...] = ("claude-haiku-4-5", "claude-sonnet-4-5")-# Every model a run calls; an off-pin call fails with no fallback. Slash-namespaced catalog ids stay out:-# Go serves them only on OpenAI shapes, so runs on them keep the Python gateway.-_FIRST_PARTY_AGENT_MODELS: list[str] = list(- dict.fromkeys([*SDK_IMPLICIT_MODELS, *(model.id for model in MODELS if "/" not in model.id)])-)+_GATEWAY_RESTRICTED_MODEL_PRODUCTS: dict[str, frozenset[str]] = {+ "deepseek-ai/deepseek-v4-flash-0731": frozenset({"posthog_code", "review_hog"}),+ "zai-org/glm-5.3": frozenset({"posthog_code", "review_hog"}),+ "zai-org/glm-5.3-flash": frozenset({"posthog_code", "review_hog"}),+}+++def _first_party_pin(product: str) -> list[str]:+ routed = (model.id for model in MODELS if not model.id.startswith("@cf/"))+ admitted = (+ model_id+ for model_id in routed+ if product in _GATEWAY_RESTRICTED_MODEL_PRODUCTS.get(model_id, frozenset({product}))+ )+ return list(dict.fromkeys([*SDK_IMPLICIT_MODELS, *admitted]))+# Ids the legacy gateway serves to PostHog Desktop that the catalog lacks; older app builds call them._DESKTOP_LEGACY_MODELS: tuple[str, ...] = ("gpt-5.4", "gpt-5.3-codex", "gpt-5.2", "gpt-5-mini")@@ -41,12 +52,12 @@ FREE_TIER_MODELS: list[str] = [FREE_TIER_PIN_KEY = "posthog_code:free"PRODUCT_ALLOWED_MODELS: dict[str, list[str]] = {- "posthog_ai": _FIRST_PARTY_AGENT_MODELS,+ "posthog_ai": _first_party_pin("posthog_ai"),"posthog_code": DESKTOP_AGENT_MODELS,FREE_TIER_PIN_KEY: FREE_TIER_MODELS,- "review_hog": _FIRST_PARTY_AGENT_MODELS,- "slack_app": _FIRST_PARTY_AGENT_MODELS,- "workflows": _FIRST_PARTY_AGENT_MODELS,+ "review_hog": _first_party_pin("review_hog"),+ "slack_app": _first_party_pin("slack_app"),+ "workflows": _first_party_pin("workflows"),}diff --git a/products/tasks/backend/temporal/process_task/tests/test_ai_gateway_token.py b/products/tasks/backend/temporal/process_task/tests/test_ai_gateway_token.pyindex 8e2309a0a0b5..4a1e72d176ea 100644--- a/products/tasks/backend/temporal/process_task/tests/test_ai_gateway_token.py+++ b/products/tasks/backend/temporal/process_task/tests/test_ai_gateway_token.py@@ -297,8 +297,10 @@ class TestMintScopedToken:)pin = _PRODUCT_ALLOWED_MODELS[product]assert len(pin) == len(set(pin))- assert set(pin) == {model for model in registry if "/" not in model} | set(SDK_IMPLICIT_MODELS)- assert any("/" in model for model in registry), "the catalog no longer offers a gateway-served model"+ expected = {model for model in registry if not model.startswith("@cf/")} | set(SDK_IMPLICIT_MODELS)+ if product != "review_hog":+ expected -= {"deepseek-ai/deepseek-v4-flash-0731", "zai-org/glm-5.3", "zai-org/glm-5.3-flash"}+ assert set(pin) == expecteddef test_limit_tier_is_sent_only_when_given(self, mint_settings):with patch("products.tasks.backend.temporal.process_task.ai_gateway_token.requests.post") as post:@@ -1041,14 +1043,14 @@ class TestSlackAppMint:mint.assert_not_called()@pytest.mark.parametrize(- "model", ["zai-org/glm-5.3", "@cf/zai-org/glm-5.2", "moonshotai/kimi-k3", "deepseek-ai/deepseek-v4-flash-0731"]+ "model", ["claude-opus-3", "@cf/zai-org/glm-5.2", "zai-org/glm-5.3", "deepseek-ai/deepseek-v4-flash-0731"])def test_model_outside_the_pin_does_not_mint(self, mint_settings, model):env, mint = self._env(mint_settings, model=model)assert "AI_GATEWAY_TOKEN" not in envmint.assert_not_called()- @pytest.mark.parametrize("model", [None, "anthropic/claude-opus-5", "gpt-5.6-terra"])+ @pytest.mark.parametrize("model", [None, "anthropic/claude-opus-5", "gpt-5.6-terra", "moonshotai/kimi-k3"])def test_unset_or_pinned_model_mints(self, mint_settings, model):env, mint = self._env(mint_settings, model=model)assert env["AI_GATEWAY_TOKEN"] == "phe_abc"@@ -1110,7 +1112,7 @@ class TestSlackAppMint:assert "AI_GATEWAY_TOKEN" not in envmint.assert_not_called()- @pytest.mark.parametrize("overrides", [{"runtime": "pi"}, {"model": "zai-org/glm-5.3"}])+ @pytest.mark.parametrize("overrides", [{"runtime": "pi"}, {"model": "claude-opus-3"}])def test_other_gates_still_refuse_an_internal_helper_run(self, mint_settings, overrides):env, mint = self._env(mint_settings, state={"ai_stage": "repo_selection"}, internal=True, **overrides)assert "AI_GATEWAY_TOKEN" not in env@@ -1197,9 +1199,11 @@ class TestSlackAppMint:assert body["product"] == "slack_app"assert body["allowed_models"] == _PRODUCT_ALLOWED_MODELS["slack_app"]- def test_pin_covers_every_first_party_catalog_model(self):- first_party = {entry.id for entry in model_catalog.MODELS if "/" not in entry.id}- missing = first_party - set(_PRODUCT_ALLOWED_MODELS["slack_app"])+ def test_pin_covers_every_routed_catalog_model(self):+ routed = {entry.id for entry in model_catalog.MODELS if not entry.id.startswith("@cf/")}+ reserved = {"deepseek-ai/deepseek-v4-flash-0731", "zai-org/glm-5.3", "zai-org/glm-5.3-flash"}+ assert reserved <= set(_PRODUCT_ALLOWED_MODELS["review_hog"])+ missing = routed - reserved - set(_PRODUCT_ALLOWED_MODELS["slack_app"])assert not missing, f"catalog models absent from the slack_app pin: {sorted(missing)}"def test_ttl_covers_the_sandbox_lifetime(self, mint_settings):@@ -1264,7 +1268,7 @@ class TestMintRefusalScope:@pytest.mark.parametrize("product", ["posthog_ai", "workflows", "review_hog"])def test_pinned_products_refuse_an_off_pin_model(self, product):- refusal = mint_refusal(product, team_id=2, state=None, model="zai-org/glm-5.3", runtime="acp")+ refusal = mint_refusal(product, team_id=2, state=None, model="claude-opus-3", runtime="acp")assert refusal == "model_outside_pin"def test_unpinned_products_take_any_model(self):@@ -1308,12 +1312,17 @@ class TestWorkflowsMint:assert "AI_GATEWAY_TOKEN" not in envmint.assert_not_called()- @pytest.mark.parametrize("model", ["zai-org/glm-5.3", "moonshotai/kimi-k3"])+ @pytest.mark.parametrize("model", ["claude-opus-3", "@cf/zai-org/glm-5.2", "zai-org/glm-5.3"])def test_model_outside_the_pin_does_not_mint(self, mint_settings, model):env, mint = self._env(mint_settings, model=model)assert "AI_GATEWAY_TOKEN" not in envmint.assert_not_called()+ def test_served_model_mints(self, mint_settings):+ env, mint = self._env(mint_settings, model="moonshotai/kimi-k3")+ assert env["AI_GATEWAY_TOKEN"] == "phe_abc"+ mint.assert_called_once()+def test_team_out_of_ai_credits_does_not_mint(self, mint_settings):env, mint = self._env(mint_settings, over_quota=True)assert "AI_GATEWAY_TOKEN" not in env@@ -1376,12 +1385,17 @@ class TestPosthogAiMint:assert "AI_GATEWAY_TOKEN" not in envmint.assert_not_called()- @pytest.mark.parametrize("model", ["zai-org/glm-5.3", "moonshotai/kimi-k3"])+ @pytest.mark.parametrize("model", ["claude-opus-3", "@cf/zai-org/glm-5.2", "zai-org/glm-5.3"])def test_model_outside_the_pin_does_not_mint(self, mint_settings, model):env, mint = self._env(mint_settings, model=model)assert "AI_GATEWAY_TOKEN" not in envmint.assert_not_called()+ def test_served_model_mints(self, mint_settings):+ env, mint = self._env(mint_settings, model="moonshotai/kimi-k3")+ assert env["AI_GATEWAY_TOKEN"] == "phe_abc"+ mint.assert_called_once()+def test_team_out_of_ai_credits_does_not_mint(self, mint_settings):env, mint = self._env(mint_settings, over_quota=True)assert "AI_GATEWAY_TOKEN" not in env