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(agent-platform): authenticate runner→ai-gateway with a static phs_ bearer (#64494)

Ben White4e04e77a43bf
diff --git a/.github/scripts/smoke-test-agent-bundle.sh b/.github/scripts/smoke-test-agent-bundle.sh
index 7e0e3a9d44be..23ed05266551 100755
--- a/.github/scripts/smoke-test-agent-bundle.sh
+++ b/.github/scripts/smoke-test-agent-bundle.sh
@@ -47,6 +47,7 @@ docker run --rm \
-e MODAL_TOKEN_ID='smoke-modal-token-id' \
-e MODAL_TOKEN_SECRET='smoke-modal-token-secret' \
-e AGENT_USE_AI_GATEWAY='1' \
+ -e POSTHOG_AI_GATEWAY_KEY='phs_smoke-test-gateway-key' \
-e POSTHOG_AI_GATEWAY_URL='http://127.0.0.1:1/v1' \
-e POSTHOG_API_BASE_URL='http://127.0.0.1:1' \
-e HTTPS_PROXY='http://127.0.0.1:1' \
diff --git a/products/agent_platform/services/agent-runner/src/config.ts b/products/agent_platform/services/agent-runner/src/config.ts
index bae0dcaf8b72..b48ed7184f10 100644
--- a/products/agent_platform/services/agent-runner/src/config.ts
+++ b/products/agent_platform/services/agent-runner/src/config.ts
@@ -79,7 +79,9 @@ export const AgentRunnerConfigSchema = PlatformConfigSchema.extend({
posthogAiGatewayKey: z
.string()
.optional()
- .describe('PostHog ai-gateway PAT (`phx_...`). First non-empty wins for pi-ai default apiKey.'),
+ .describe(
+ 'Static ai-gateway bearer — a `phs_` project secret key with the `llm_gateway:read` scope. On the gateway path it authenticates every model + usage call (required there); on the direct path it falls through as the first-priority provider apiKey.'
+ ),
anthropicApiKey: z.string().optional().describe('Anthropic API key. Second-priority for pi-ai default apiKey.'),
openaiApiKey: z.string().optional().describe('OpenAI API key. Third-priority for pi-ai default apiKey.'),
modelApiKey: z.string().optional().describe('Catch-all model API key. Last-priority for pi-ai default apiKey.'),
diff --git a/products/agent_platform/services/agent-runner/src/index.ts b/products/agent_platform/services/agent-runner/src/index.ts
index 0ef26212b93b..f8f1e102078e 100644
--- a/products/agent_platform/services/agent-runner/src/index.ts
+++ b/products/agent_platform/services/agent-runner/src/index.ts
@@ -78,6 +78,14 @@ async function main(): Promise<void> {
)
}
+ // Gateway path authenticates every call with one static phs_ bearer; without
+ // it every request 401s, so fail fast rather than crash-loop at first turn.
+ if (config.useAiGateway && !config.posthogAiGatewayKey) {
+ throw new Error(
+ 'AGENT_USE_AI_GATEWAY requires POSTHOG_AI_GATEWAY_KEY — a phs_ project secret key with the llm_gateway:read scope.'
+ )
+ }
+
// Outbound HTTP — every tool fetch, gateway fetch, and MCP transport
// dispatches through here. In prod `config.httpsProxy` points at smokescreen
// so author-supplied URLs (web-fetch, http-request, external MCPs) get SSRF
@@ -158,9 +166,8 @@ async function main(): Promise<void> {
await logSink.connect()
// Resolves a team's `phc_` project key from the main PostHog DB (cached per
- // team). Two consumers: the ai-gateway bearer (below) and the LLM-analytics
- // sink (next). Constructed unconditionally so analytics can route per-team
- // even when the gateway is off. See ai-gateway-integration.md §3 (W1).
+ // team) for the LLM-analytics sink's per-team routing (below). The gateway
+ // bearer is a single static phs_ now, so this no longer feeds it.
const teamApiKeys = new PgTeamApiKeyResolver(posthogDb)
// LLM analytics sink. Captures `$ai_generation` per pi-ai call, `$ai_span`
@@ -300,19 +307,20 @@ async function main(): Promise<void> {
baseUrl: config.aiGatewayUrl,
})
: undefined,
- // The driver streams through pi-ai's `streamSimple`; the per-session
- // API key flows in here (no more client-level default). Gateway path
- // → resolve the owning team's `phc_`; direct path → fall back to the
- // boot-time default (ANTHROPIC_API_KEY / OPENAI_API_KEY / etc).
- resolveApiKey: config.useAiGateway ? (session) => teamApiKeys.resolve(session.team_id) : () => defaultApiKey,
+ // Per-session bearer for pi-ai's `streamSimple` (no client-level default).
+ // Gateway path → the static phs_ (cost bills to the team that owns it);
+ // direct path → boot-time provider key (ANTHROPIC_API_KEY / OPENAI / etc).
+ resolveApiKey: config.useAiGateway ? () => config.posthogAiGatewayKey : () => defaultApiKey,
resolveGatewayHeaders: config.useAiGateway
? (session) => ({
'X-PostHog-Distinct-Id': analyticsDistinctId(session),
'X-PostHog-Trace-Id': session.id,
})
: undefined,
+ // /v1/usage + /v1/wallet reads use the same static phs_ (the `phc` field
+ // is the read client's bearer; key presence is guaranteed at boot above).
resolveGatewayUsage: gatewayClient
- ? async (session) => ({ client: gatewayClient, phc: await teamApiKeys.resolve(session.team_id) })
+ ? () => ({ client: gatewayClient, phc: config.posthogAiGatewayKey! })
: undefined,
// On the gateway path pi-ai's cost numbers are client-side estimates;
// the gateway itself owns billing. We keep token counts. Cost is