: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
fix(wizard): pin the gateway obo to the customer's team id (#99853)
Brandon43a1a04233aa
diff --git a/posthog/api/wizard/http.py b/posthog/api/wizard/http.pyindex fb45242d9314..e8423caabeaf 100644--- a/posthog/api/wizard/http.py+++ b/posthog/api/wizard/http.py@@ -159,7 +159,7 @@ class SetupWizardViewSet(viewsets.ViewSet):def gateway_token(self, request: Request) -> Response:"""Mint a scoped gateway token for a wizard run.- The CLI uses the returned phe_ (pinned product=wizard / obo=<customer org>,+ The CLI uses the returned phe_ (pinned product=wizard / obo=<customer team id>,capped, expiring) as its gateway bearer and re-calls near expiry. There isno other gateway: every refusal ends the run, with the body's `detail`shown to the user and its `code` naming the outcome.@@ -300,7 +300,7 @@ class SetupWizardViewSet(viewsets.ViewSet):refuse("throttled", e, user=user)try:minted = mint_wizard_gateway_token(- obo=str(team.organization_id),+ obo=str(team.id),user=distinct_id,product=product,cap_usd=override.cap_usd,@@ -322,7 +322,7 @@ class SetupWizardViewSet(viewsets.ViewSet):"expires_at": minted["expires_at"],"cap_usd": minted.get("cap_usd"),"gateway_url": wizard_gateway_base_url(),- # Keeps a team breakdown beside the org-level obo attribution.+ # The CLI stamps this on each generation as `team_id`."team_id": team.id,},status=status.HTTP_201_CREATED,diff --git a/posthog/api/wizard/test/test_http.py b/posthog/api/wizard/test/test_http.pyindex cc7843cec2a5..d46f8e4051a4 100644--- a/posthog/api/wizard/test/test_http.py+++ b/posthog/api/wizard/test/test_http.py@@ -279,7 +279,7 @@ class SetupWizardGatewayTokenTests(APIBaseTest):assert body["gateway_url"] == "https://ai-gateway.us.posthog.com"assert body["team_id"] == self.team.idassert mock_mint.call_args.kwargs == {- "obo": str(self.team.organization_id),+ "obo": str(self.team.id),"user": str(self.user.distinct_id),"product": "wizard:integration","cap_usd": None,diff --git a/posthog/llm/test_wizard_gateway_token.py b/posthog/llm/test_wizard_gateway_token.pyindex dcb8dd58aadb..134181fb1581 100644--- a/posthog/llm/test_wizard_gateway_token.py+++ b/posthog/llm/test_wizard_gateway_token.py@@ -69,14 +69,14 @@ class TestMintWizardGatewayToken:def test_posts_pinned_attribution_and_bearer(self):minted = {"token": "phe_x", "expires_at": "2026-08-22T00:00:00Z", "cap_usd": "25"}with patch("posthog.llm.wizard_gateway_token.requests.post", return_value=_Response(201, minted)) as post:- assert mint_wizard_gateway_token(obo="org_1", user="user_1") == minted+ assert mint_wizard_gateway_token(obo="7", user="user_1") == mintedassert post.call_args[0][0] == "https://ai-gateway.us.posthog.com/v1/tokens"assert post.call_args.kwargs["json"] == {"cap_usd": "25.000000","ttl_seconds": 86400,"product": "wizard",- "obo": "org_1",+ "obo": "7","user": "user_1",# Derived: this case pins that the mint carries the pins, not which# models are on the table. TestWizardModelAllowlist owns the contents.@@ -123,21 +123,21 @@ class TestWizardModelAllowlist:assert wizard_gateway_base_url() == "https://ai-gateway.us.posthog.com"minted = {"token": "phe_x", "expires_at": "2026-08-22T00:00:00Z"}with patch("posthog.llm.wizard_gateway_token.requests.post", return_value=_Response(201, minted)) as post:- mint_wizard_gateway_token(obo="org_1", user="user_1")+ mint_wizard_gateway_token(obo="7", user="user_1")assert post.call_args[0][0] == "https://ai-gateway.us.posthog.com/v1/tokens"@override_settings(WIZARD_GATEWAY_TOKEN_TTL_SECONDS=172800)def test_ttl_clamped_to_gateway_ceiling(self):minted = {"token": "phe_x", "expires_at": "2026-08-22T00:00:00Z"}with patch("posthog.llm.wizard_gateway_token.requests.post", return_value=_Response(201, minted)) as post:- mint_wizard_gateway_token(obo="org_1", user="user_1")+ mint_wizard_gateway_token(obo="7", user="user_1")assert post.call_args.kwargs["json"]["ttl_seconds"] == 86400@override_settings(WIZARD_GATEWAY_TOKEN_TTL_SECONDS=5)def test_ttl_clamped_to_a_ttl_that_outlives_a_run(self):minted = {"token": "phe_x", "expires_at": "2026-08-22T00:00:00Z"}with patch("posthog.llm.wizard_gateway_token.requests.post", return_value=_Response(201, minted)) as post:- mint_wizard_gateway_token(obo="org_1", user="user_1")+ mint_wizard_gateway_token(obo="7", user="user_1")assert post.call_args.kwargs["json"]["ttl_seconds"] == 1800@pytest.mark.parametrize(@@ -151,7 +151,7 @@ class TestWizardModelAllowlist:def test_non_201_raises(self, response):with patch("posthog.llm.wizard_gateway_token.requests.post", return_value=response):with pytest.raises(WizardGatewayMintError):- mint_wizard_gateway_token(obo="org_1", user="user_1")+ mint_wizard_gateway_token(obo="7", user="user_1")def test_transport_failure_raises(self):with patch(@@ -159,7 +159,7 @@ class TestWizardModelAllowlist:side_effect=requests.RequestException("connection reset"),):with pytest.raises(WizardGatewayMintError):- mint_wizard_gateway_token(obo="org_1", user="user_1")+ mint_wizard_gateway_token(obo="7", user="user_1")def test_non_json_body_raises(self):with patch(@@ -167,7 +167,7 @@ class TestWizardModelAllowlist:return_value=_Response(201, raise_on_json=True),):with pytest.raises(WizardGatewayMintError):- mint_wizard_gateway_token(obo="org_1", user="user_1")+ mint_wizard_gateway_token(obo="7", user="user_1")@pytest.mark.parametrize("payload",@@ -180,7 +180,7 @@ class TestWizardModelAllowlist:def test_incomplete_payload_raises(self, payload):with patch("posthog.llm.wizard_gateway_token.requests.post", return_value=_Response(201, payload)):with pytest.raises(WizardGatewayMintError):- mint_wizard_gateway_token(obo="org_1", user="user_1")+ mint_wizard_gateway_token(obo="7", user="user_1")@pytest.mark.parametrize("configured",@@ -209,7 +209,7 @@ class TestWizardModelAllowlist:"posthog.llm.wizard_gateway_token.requests.post",return_value=_Response(201, minted),) as post:- mint_wizard_gateway_token(obo="org_1", user="user_1")+ mint_wizard_gateway_token(obo="7", user="user_1")assert post.call_args.kwargs["json"]["cap_usd"] == "7.000000"@pytest.mark.parametrize(@@ -225,7 +225,7 @@ class TestWizardModelAllowlist:def test_transport_failures_report_whether_a_token_may_exist(self, raised, token_may_exist):with patch("posthog.llm.wizard_gateway_token.requests.post", side_effect=raised):with pytest.raises(WizardGatewayMintError) as excinfo:- mint_wizard_gateway_token(obo="org_1", user="user_1")+ mint_wizard_gateway_token(obo="7", user="user_1")assert excinfo.value.token_may_exist is token_may_existdef test_a_refusal_reports_that_no_token_exists(self):@@ -234,7 +234,7 @@ class TestWizardModelAllowlist:return_value=_Response(400, {"error": "bad request"}),):with pytest.raises(WizardGatewayMintError) as excinfo:- mint_wizard_gateway_token(obo="org_1", user="user_1")+ mint_wizard_gateway_token(obo="7", user="user_1")assert excinfo.value.token_may_exist is Falsedef test_an_unreadable_201_reports_that_a_token_may_exist(self):@@ -245,7 +245,7 @@ class TestWizardModelAllowlist:return_value=_Response(201, {"not": "a token"}),):with pytest.raises(WizardGatewayMintError) as excinfo:- mint_wizard_gateway_token(obo="org_1", user="user_1")+ mint_wizard_gateway_token(obo="7", user="user_1")assert excinfo.value.token_may_exist is True@override_settings(WIZARD_GATEWAY_TOKEN_CAP_USD="12.5")@@ -255,19 +255,19 @@ class TestWizardModelAllowlist:"posthog.llm.wizard_gateway_token.requests.post",return_value=_Response(201, minted),) as post:- mint_wizard_gateway_token(obo="org_1", user="user_1")+ mint_wizard_gateway_token(obo="7", user="user_1")assert post.call_args.kwargs["json"]["cap_usd"] == "12.500000"def test_an_override_cap_replaces_the_setting(self):minted = {"token": "phe_x", "expires_at": "2026-08-22T00:00:00Z"}with patch("posthog.llm.wizard_gateway_token.requests.post", return_value=_Response(201, minted)) as post:- mint_wizard_gateway_token(obo="org_1", user="user_1", cap_usd=Decimal("30"))+ mint_wizard_gateway_token(obo="7", user="user_1", cap_usd=Decimal("30"))assert post.call_args.kwargs["json"]["cap_usd"] == "30.000000"def test_secret_never_appears_in_the_error(self):with patch("posthog.llm.wizard_gateway_token.requests.post", return_value=_Response(403)):with pytest.raises(WizardGatewayMintError) as raised:- mint_wizard_gateway_token(obo="org_1", user="user_1")+ mint_wizard_gateway_token(obo="7", user="user_1")assert "phs_wizard_secret" not in str(raised.value)@@ -367,7 +367,7 @@ class TestTieredMint:def _mint(self, **kwargs):minted = {"token": "phe_x", "expires_at": "2026-08-22T00:00:00Z"}with patch("posthog.llm.wizard_gateway_token.requests.post", return_value=_Response(201, minted)) as post:- mint_wizard_gateway_token(obo="org_1", user="user_1", **kwargs)+ mint_wizard_gateway_token(obo="7", user="user_1", **kwargs)return post.call_args.kwargs["json"]def test_the_tier_cap_and_ttl_apply(self):diff --git a/posthog/llm/wizard_gateway_token.py b/posthog/llm/wizard_gateway_token.pyindex 725f4b4717e2..d605a5fa72e8 100644--- a/posthog/llm/wizard_gateway_token.py+++ b/posthog/llm/wizard_gateway_token.py@@ -3,7 +3,7 @@Django mints a `phe_` off the PostHog-owned wizard team's `phs_`(WIZARD_GATEWAY_MINT_KEY) rather than sending the user's OAuth token to thegateway. The mint pins what the caller must not control (product=wizard, obo=the-customer organization, the acting user) plus a per-run cap and an expiry, and the+customer's team id, the acting user) plus a per-run cap and an expiry, and thedebit lands on the wizard team, never the customer's wallet. Kept separate fromproducts/tasks' sandbox mint (ai_gateway_token.py): the wizard needs`expires_at` back for CLI-side refresh, and an interactive mint answers one