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(devex): move AI gateway and API token team actions to the admin console (#128989)

Julian Bezd7621d1a66d4
diff --git a/frontend/public/admin-console/crests/ai-gateway.svg b/frontend/public/admin-console/crests/ai-gateway.svg
new file mode 100644
index 000000000000..ec6994898665
--- /dev/null
+++ b/frontend/public/admin-console/crests/ai-gateway.svg
@@ -0,0 +1 @@
+<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 115 115"><mask id="crests-ai-gateway-path-1-outside-1_180_3992" width="99" height="74" x="8" y="20.5" fill="#000" maskUnits="userSpaceOnUse"><path fill="#fff" d="M8 20.5h99v74H8z"/><path d="M93.2 23.5a1.5 1.5 0 0 1 1.34 1.26l9.44 57.35q.09.57-.23 1.07l-5.03 7.65a1.5 1.5 0 0 1-1.26.67h-77.4a1.5 1.5 0 0 1-1.5-1.3l-7.55-58.64q-.07-.62.31-1.12l5.04-6.37.11-.13a1.5 1.5 0 0 1 1.06-.44z"/></mask><path fill="#f1ecc7" d="M93.2 23.5a1.5 1.5 0 0 1 1.34 1.26l9.44 57.35q.09.57-.23 1.07l-5.03 7.65a1.5 1.5 0 0 1-1.26.67h-77.4a1.5 1.5 0 0 1-1.5-1.3l-7.55-58.64q-.07-.62.31-1.12l5.04-6.37.11-.13a1.5 1.5 0 0 1 1.06-.44z"/><path fill="#fff" d="m93.2 23.5.26-2.98-.14-.01zm1.34 1.26 2.96-.5zm9.44 57.35 2.96-.49zm-.23 1.07 2.5 1.65zm-5.03 7.65 2.5 1.65zm-78.67.67v3zm-1.49-1.3-2.97.37zm-7.55-58.64-2.97.39zm.31-1.12-2.35-1.86zm5.04-6.37-2.25-1.99-.1.13zm.11-.13-2.13-2.11-.11.12zm1.06-.44v-3zm75.53 0 .13-3h-.13zm.13 0-.26 3a1.5 1.5 0 0 1-1.35-1.26l2.96-.48 2.96-.5a4.5 4.5 0 0 0-4.04-3.74zm1.35 1.26-2.96.48 9.44 57.36 2.96-.5 2.96-.48-9.44-57.35zm9.44 57.35-2.96.49a1.5 1.5 0 0 1 .23-1.07l2.5 1.65 2.5 1.65a4.5 4.5 0 0 0 .69-3.2zm-.23 1.07-2.5-1.65-5.04 7.65 2.5 1.65 2.51 1.64 5.04-7.64zm-5.03 7.65-2.5-1.66a1.5 1.5 0 0 1 1.24-.67v6a4.5 4.5 0 0 0 3.76-2.02zm-1.26.67v-3h-77.4v6h77.4zm-77.4 0v-3c.75 0 1.38.56 1.48 1.3l-2.98.4-2.97.37a4.5 4.5 0 0 0 4.46 3.93zm-1.5-1.3 2.98-.4-7.55-58.62-2.98.38-2.97.39 7.55 58.62zm-7.55-58.64 2.98-.38c.05.4-.06.8-.31 1.12l-2.36-1.86-2.35-1.86a4.5 4.5 0 0 0-.93 3.37zm.31-1.12 2.36 1.86 5.03-6.37-2.35-1.86L14 22.21l-5.03 6.37zm5.04-6.37 2.24 1.99.12-.13-2.25-1.99-2.24-1.99-.12.13zm.11-.13 2.13 2.12a1.5 1.5 0 0 1-1.06.44v-6a4.5 4.5 0 0 0-3.2 1.33zm1.06-.44v3h75.53v-6H17.53zm75.53 0-.13 3h.14l.12-3 .13-3h-.13z" mask="url(#crests-ai-gateway-path-1-outside-1_180_3992)"/><path fill="#c3e2e9" fill-rule="evenodd" d="m17.54 25 7.55 57.35h77.41L93.06 25z" clip-rule="evenodd"/><path fill="#a8c5cc" d="M25.09 82.35 17.54 25l-5.04 6.37L20.05 90z"/><path fill="#a8c5cc" d="M102.5 82.35H25.09L20.05 90h77.41z"/><path stroke="#475a5e" stroke-linejoin="round" stroke-width="3" d="m17.54 25 7.55 57.35M20.05 90h77.41l5.04-7.65L93.06 25H17.53l-5.03 6.37zm5.04-7.65h77.41m-77.41 0L20.05 90"/><path fill="#f1ecc7" d="M82.7 62.2h-8.4l-6.7-7.78H76zm-15.1-7.78-1.7-11.07h8.4L76 54.42zM82.7 70.5h-8.4v-8.3h8.4z"/><path fill="#c2bea1" d="m74.3 70.5-5.58-6.65-1.12-9.43 6.7 7.77z"/><path fill="#918e77" d="m65.9 43.35-1.48.22a1.5 1.5 0 0 1 1.48-1.72zm2.82 20.5-1.15.96a1.5 1.5 0 0 1-.34-.79zm5.57 6.65V72c-.44 0-.86-.2-1.15-.53zm8.41 0h1.5c0 .83-.67 1.5-1.5 1.5zm0-8.3 1.14-.99q.35.42.36.98zm-8.4-18.85v-1.5c.73 0 1.36.54 1.48 1.27zm-5.58 20.5 1.15-.97 5.57 6.66-1.15.96-1.15.97-5.57-6.66zm5.57 6.65V69h8.41v3h-8.4zm8.41 0h-1.5v-8.3h3v8.3zm0-8.3v1.5h-8.4v-3h8.4zm-8.4 0h1.5v8.3h-3v-8.3zm-8.4-18.85 1.48-.23 1.7 11.07-1.48.23-1.48.23-1.7-11.08zm1.7 11.07 1.5-.18 1.11 9.43-1.49.18-1.49.17-1.12-9.42zm6.7 7.77-1.14.98-6.7-7.77 1.14-.98 1.14-.98 6.69 7.77zm8.4 0-1.13.98-6.71-7.77 1.13-.98 1.14-.98 6.7 7.77zM76 54.42v1.5h-8.4v-3H76zM65.9 43.35v-1.5h8.4v3h-8.4zm8.4 0 1.48-.23 1.7 11.07-1.49.23-1.48.23-1.7-11.08z"/><path fill="#f1ecc7" d="m53 41.24-1.15-6.74h-8.08l1.15 6.74zm-5.77 11.22L53 41.24h-8.08l-5.74 11.22zM41.46 66.5h8.66l-2.89-14.04h-8.05z"/><path fill="#c2bea1" d="m44.92 41.24-1.15-6.74L38 45.16l1.18 7.3z"/><path stroke="#918e77" stroke-linejoin="round" stroke-width="3" d="M47.23 52.46 53 41.24l-1.15-6.74h-8.08L38 45.16l1.18 7.3 2.28 14.04h8.66zm0 0h-8.05M53 41.24h-8.08m-1.15-6.74 1.15 6.74m0 0-5.74 11.22"/><path stroke="#40396e" stroke-linejoin="round" stroke-width="3" d="m20 90.25-7.5-59 6-6.5h75l9 58-5.5 7.5z"/></svg>
\ No newline at end of file
diff --git a/posthog/admin/console/team_panels.py b/posthog/admin/console/team_panels.py
index 2d701664554c..6cddf122bc74 100644
--- a/posthog/admin/console/team_panels.py
+++ b/posthog/admin/console/team_panels.py
@@ -1,9 +1,13 @@
+from decimal import Decimal
+
+from django import forms
from django.db.models import Model
from posthog.admin.admins.team_admin import TeamAdmin
from posthog.admin.console.bridge import AdminViewBridge
-from posthog.admin.console.panels import Fact, Operation, Owner, Panel, Tone
+from posthog.admin.console.panels import Fact, Operation, Owner, Panel, ReasonForm, Tone
from posthog.admin.console.registry import console
+from posthog.models.activity_logging.activity_log import ActivityLog
from posthog.models.team.team import Team
# The console shell owns the core panels until a team claims one.
@@ -52,3 +56,120 @@ class RemoteConfigPanel(Panel):
description="Recomputes the remote config for this team and writes it to Redis and S3.",
)
]
+
+
+AI_GATEWAY = Owner(name="AI gateway", crest="ai-gateway")
+
+
+class AddCreditForm(forms.Form):
+ amount_usd = forms.DecimalField(label="Amount (USD)", min_value=Decimal("0.01"), decimal_places=2)
+ reason = forms.CharField(widget=forms.Textarea(attrs={"rows": 3}), help_text="Shown in the top-up history.")
+
+
+class SetApiTokenForm(forms.Form):
+ new_token = forms.CharField(label="New API token")
+ reason = forms.CharField(widget=forms.Textarea(attrs={"rows": 3}), help_text="Goes into the audit log.")
+
+
+RECENT_TOP_UPS = 3
+
+
+class AIGatewayPanel(Panel):
+ record = "team"
+ key = "ai-gateway"
+ title = "AI gateway"
+ owner = AI_GATEWAY
+
+ def facts(self, obj: Model) -> list[Fact]:
+ assert isinstance(obj, Team)
+ if obj.llm_gateway_revoked_at:
+ access = Fact(label="Access", value="Revoked", tone=Tone.DESTRUCTIVE)
+ elif obj.llm_gateway_enabled_at:
+ access = Fact(label="Access", value="Enrolled", tone=Tone.SUCCESS)
+ else:
+ access = Fact(label="Access", value="Not enrolled", tone=Tone.DEFAULT)
+ facts = [access]
+ if obj.llm_gateway_enabled_at:
+ facts.append(Fact(label="Enrolled since", value=obj.llm_gateway_enabled_at.strftime("%b %-d, %Y")))
+ # The wallet balance is a blocking call to the gateway, so the page shows the local top-up log instead.
+ top_ups = (
+ ActivityLog.objects.filter(scope="AIGatewayCredit", team_id=obj.pk, activity="credit_added")
+ .select_related("user")
+ .order_by("-created_at")[:RECENT_TOP_UPS]
+ )
+ for entry in top_ups:
+ context = (entry.detail or {}).get("context", {})
+ who = entry.user.email if entry.user else "unknown"
+ facts.append(
+ Fact(
+ label=f"Top-up {entry.created_at.strftime('%b %-d')}",
+ value=f"${context.get('amount_usd', '?')} by {who}: {context.get('reason', '')}",
+ )
+ )
+ return facts
+
+ def operations(self, obj: Model) -> list[Operation]:
+ assert isinstance(obj, Team)
+ operations = [
+ Operation(
+ key="add-credit",
+ label="Add credit",
+ handler=AdminViewBridge(Team, TeamAdmin.add_ai_gateway_credit_view),
+ description="Adds USD credit to the team's gateway wallet.",
+ form_class=AddCreditForm,
+ )
+ ]
+ if obj.llm_gateway_enabled_at is None:
+ operations.append(
+ Operation(
+ key="enable",
+ label="Enable access",
+ handler=AdminViewBridge(Team, TeamAdmin.enable_ai_gateway_view),
+ )
+ )
+ if obj.llm_gateway_revoked_at is None:
+ operations.append(
+ Operation(
+ key="revoke",
+ label="Revoke access",
+ handler=AdminViewBridge(Team, TeamAdmin.revoke_ai_gateway_view),
+ danger=True,
+ description="Blocks the team's gateway requests until the revoke is cleared.",
+ form_class=ReasonForm,
+ )
+ )
+ else:
+ operations.append(
+ Operation(
+ key="clear-revoke",
+ label="Clear revoke",
+ handler=AdminViewBridge(Team, TeamAdmin.clear_ai_gateway_revoke_view),
+ )
+ )
+ return operations
+
+
+class ApiTokenPanel(Panel):
+ record = "team"
+ key = "api-token"
+ title = "Project API token"
+ owner = DEV_EXPERIENCE
+ danger = True
+
+ def facts(self, obj: Model) -> list[Fact]:
+ assert isinstance(obj, Team)
+ return [Fact(label="Current token", value=obj.api_token, mono=True)]
+
+ def operations(self, obj: Model) -> list[Operation]:
+ return [
+ Operation(
+ key="set",
+ label="Set API token",
+ handler=AdminViewBridge(Team, TeamAdmin.set_api_token_view),
+ danger=True,
+ description="SDKs that still send the old token stop reaching this team.",
+ form_class=SetApiTokenForm,
+ )
+ ]
diff --git a/posthog/admin/test_admin_console.py b/posthog/admin/test_admin_console.py
index 78ffbbff4f79..9a4a4713c3be 100644
--- a/posthog/admin/test_admin_console.py
+++ b/posthog/admin/test_admin_console.py
@@ -2,7 +2,7 @@ import re
from pathlib import Path
from posthog.test.base import BaseTest
-from unittest.mock import patch
+from unittest.mock import MagicMock, patch
from django.conf import settings
from django.test import Client, SimpleTestCase
@@ -11,6 +11,7 @@ from parameterized import parameterized
from posthog.admin.console.panels import Operation, Owner, Panel
from posthog.admin.console.registry import ConsoleRegistry, console
+from posthog.llm.gateway_internal_client import CreditResult
from products.workflows.backend.facade.api import get_email_sending_state
@@ -112,7 +113,7 @@ class TestAdminConsoleViews(BaseTest):
assert response.status_code == 200
content = response.content.decode()
- for title in ("Overview", "Remote config cache", "Email sending"):
+ for title in ("Overview", "Remote config cache", "Email sending", "AI gateway", "Project API token"):
assert title in content
@parameterized.expand(
@@ -141,3 +142,22 @@ class TestAdminConsoleViews(BaseTest):
assert response["Location"] == f"/admin/console/teams/{self.team.pk}/"
state = get_email_sending_state(self.team.pk)
assert (state is not None and state.suspended_at is not None) == expect_suspended
+
+ @patch("posthog.admin.admins.team_admin.add_credit")
+ def test_a_double_submitted_credit_form_reuses_its_idempotency_key(self, add_credit: MagicMock) -> None:
+ add_credit.return_value = CreditResult(
+ team_id=self.team.pk, entry_id="entry-1", amount_usd="25", balance_usd="25", duplicate=False
+ )
+ form = self.client.get(f"/admin/console/teams/{self.team.pk}/ai-gateway/add-credit/").content.decode()
+ nonce = re.search(r'name="form_nonce" value="([^"]+)"', form)
+ assert nonce is not None
+
+ for _ in range(2):
+ self.client.post(
+ f"/admin/console/teams/{self.team.pk}/ai-gateway/add-credit/",
+ {"amount_usd": "25", "reason": "Outage goodwill", "form_nonce": nonce.group(1)},
+ )
+
+ keys = {call.args[3] for call in add_credit.call_args_list}
+ assert add_credit.call_count == 2
+ assert len(keys) == 1