: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(agent-runner): auth gateway dispatch via Authorization: Bearer (#64551)
Ben White5d74083e94ce
diff --git a/products/agent_platform/services/agent-runner/src/index.ts b/products/agent_platform/services/agent-runner/src/index.tsindex f8f1e102078e..342d17f80b80 100644--- a/products/agent_platform/services/agent-runner/src/index.ts+++ b/products/agent_platform/services/agent-runner/src/index.ts@@ -305,6 +305,8 @@ async function main(): Promise<void> {posthogAiGatewayModel({specModel,baseUrl: config.aiGatewayUrl,+ // Non-null: boot guard above throws when useAiGateway && !posthogAiGatewayKey.+ apiKey: config.posthogAiGatewayKey!,}): undefined,// Per-session bearer for pi-ai's `streamSimple` (no client-level default).diff --git a/products/agent_platform/services/agent-runner/src/models/ai-gateway-model.test.ts b/products/agent_platform/services/agent-runner/src/models/ai-gateway-model.test.tsnew file mode 100644index 000000000000..93702685e7bb--- /dev/null+++ b/products/agent_platform/services/agent-runner/src/models/ai-gateway-model.test.ts@@ -0,0 +1,42 @@+import { describe, expect, it } from 'vitest'++import { aiGatewaySkuFor, posthogAiGatewayModel } from './ai-gateway-model'++describe('aiGatewaySkuFor', () => {+ it.each([+ ['openai/gpt-4o', 'gpt-4o'],+ ['anthropic/claude-sonnet-4-6', 'claude-sonnet-4-6'],+ ['gpt-4o', 'gpt-4o'],+ ])('maps %s -> %s', (specModel, sku) => {+ expect(aiGatewaySkuFor(specModel)).toBe(sku)+ })+})++describe('posthogAiGatewayModel', () => {+ // The gateway reads only `Authorization: Bearer`. pi-ai's anthropic-messages+ // shape would otherwise send the key as `x-api-key` and 401 at the gateway —+ // so the Bearer header must be pinned regardless of provider shape.+ it.each(['anthropic/claude-sonnet-4-6', 'openai/gpt-4o'])('pins Authorization: Bearer for %s', (specModel) => {+ const model = posthogAiGatewayModel({+ specModel,+ baseUrl: 'https://ai-gateway.example.com/v1',+ apiKey: 'phs_test-key',+ })+ expect(model.headers?.Authorization).toBe('Bearer phs_test-key')+ expect(model.provider).toBe('posthog-ai-gateway')+ })++ // anthropic-messages: the SDK appends `/v1/messages`, so the trailing `/v1`+ // is stripped. openai shapes append their own suffix, so `/v1` is kept.+ it.each([+ ['anthropic/claude-sonnet-4-6', 'https://ai-gateway.example.com'],+ ['openai/gpt-4o', 'https://ai-gateway.example.com/v1'],+ ])('resolves baseUrl for %s', (specModel, expectedBaseUrl) => {+ const model = posthogAiGatewayModel({+ specModel,+ baseUrl: 'https://ai-gateway.example.com/v1',+ apiKey: 'phs_test-key',+ })+ expect(model.baseUrl).toBe(expectedBaseUrl)+ })+})diff --git a/products/agent_platform/services/agent-runner/src/models/ai-gateway-model.ts b/products/agent_platform/services/agent-runner/src/models/ai-gateway-model.tsindex 2c963ff46368..77d6acd8e054 100644--- a/products/agent_platform/services/agent-runner/src/models/ai-gateway-model.ts+++ b/products/agent_platform/services/agent-runner/src/models/ai-gateway-model.ts@@ -15,6 +15,15 @@* or `/responses` to baseUrl, so we keep the `/v1` suffix on baseUrl.* - anthropic-messages → Anthropic SDK appends `/v1/messages` itself, so* we strip the trailing `/v1` from baseUrl.+ *+ * Auth: the gateway authenticates the customer with `Authorization: Bearer+ * <phs_…>` (it reads only that header). pi-ai's openai shapes already send the+ * apiKey as `Authorization: Bearer`, but its anthropic-messages shape sends it+ * as `x-api-key` with no `Authorization` — so a Claude model would 401 at the+ * gateway's auth tier. We pin `Authorization: Bearer <key>` on the model so+ * every shape authenticates the way the gateway expects. pi-ai still sends the+ * key as the provider credential too (x-api-key for Anthropic), which the+ * gateway overrides when it forwards to the upstream provider.*/import type { Model } from '@earendil-works/pi-ai'@@ -26,6 +35,13 @@ export interface AiGatewayModelOpts {specModel: string/** Gateway root with `/v1` suffix, e.g. `http://localhost:8080/v1`. */baseUrl: string+ /**+ * The gateway bearer — a `phs_` project secret key with `llm_gateway:read`.+ * Pinned as `Authorization: Bearer <key>` on the model so the+ * anthropic-messages shape (which otherwise sends only `x-api-key`)+ * authenticates against the gateway. See the auth note above.+ */+ apiKey: string}/**@@ -46,6 +62,10 @@ export function posthogAiGatewayModel(opts: AiGatewayModelOpts): Model<string> {id: aiGatewaySkuFor(opts.specModel),provider: 'posthog-ai-gateway',baseUrl: gatewayBaseUrlForApi(native.api, opts.baseUrl),+ headers: {+ ...native.headers,+ Authorization: `Bearer ${opts.apiKey}`,+ },}}diff --git a/products/agent_platform/services/agent-tests/src/cases/real-inference.test.ts b/products/agent_platform/services/agent-tests/src/cases/real-inference.test.tsindex e432c55e7657..cc3948542822 100644--- a/products/agent_platform/services/agent-tests/src/cases/real-inference.test.ts+++ b/products/agent_platform/services/agent-tests/src/cases/real-inference.test.ts@@ -96,14 +96,23 @@ function discoverProviders(): ProviderSpec[] {const pin = process.env.REAL_INFERENCE_PROVIDER?.toLowerCase()const out: ProviderSpec[] = []if ((!pin || pin === 'gateway') && process.env.POSTHOG_AI_GATEWAY_KEY && process.env.POSTHOG_AI_GATEWAY_URL) {- out.push({- label: 'ai-gateway',- model: posthogAiGatewayModel({- specModel: process.env.REAL_INFERENCE_MODEL_ID ?? 'openai/gpt-4.1-mini',- baseUrl: process.env.POSTHOG_AI_GATEWAY_URL,- }),- apiKey: process.env.POSTHOG_AI_GATEWAY_KEY,- })+ const gatewayKey = process.env.POSTHOG_AI_GATEWAY_KEY+ const gatewayUrl = process.env.POSTHOG_AI_GATEWAY_URL+ // Exercise BOTH provider shapes through the gateway. openai authenticates+ // with `Authorization: Bearer` natively; anthropic-messages relies on+ // posthogAiGatewayModel pinning that header (the regression this guards —+ // pi-ai's anthropic shape otherwise sends only `x-api-key`, which the+ // gateway rejects). An explicit REAL_INFERENCE_MODEL_ID pins one model.+ const gatewayModels = process.env.REAL_INFERENCE_MODEL_ID+ ? [process.env.REAL_INFERENCE_MODEL_ID]+ : ['openai/gpt-4.1-mini', 'anthropic/claude-haiku-4-5']+ for (const specModel of gatewayModels) {+ out.push({+ label: `ai-gateway (${specModel})`,+ model: posthogAiGatewayModel({ specModel, baseUrl: gatewayUrl, apiKey: gatewayKey }),+ apiKey: gatewayKey,+ })+ }}if ((!pin || pin === 'anthropic') && process.env.ANTHROPIC_API_KEY) {out.push({