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(ai-gateway): add developer mode tokens for staff (#120754)

Brandon2bfaea5aaefb
diff --git a/.semgrep/rules/security/idor-team-scoped-models.yaml b/.semgrep/rules/security/idor-team-scoped-models.yaml
index d738ef13efc5..37e0c677d76f 100644
--- a/.semgrep/rules/security/idor-team-scoped-models.yaml
+++ b/.semgrep/rules/security/idor-team-scoped-models.yaml
@@ -1662,6 +1662,7 @@ rules:
regex: |
(?x)(
AgentMemory
+ |DevGatewayToken
|FileSystemHomeFolder
|FileSystemShortcut
|FileSystemViewLog
@@ -1737,6 +1738,7 @@ rules:
regex: |
(?x)(
AgentMemory
+ |DevGatewayToken
|FileSystemHomeFolder
|FileSystemShortcut
|FileSystemViewLog
diff --git a/frontend/package.json b/frontend/package.json
index e4b9b3ff5f23..14a893b19bfc 100644
--- a/frontend/package.json
+++ b/frontend/package.json
@@ -84,6 +84,7 @@
"@posthog/products-access-control": "workspace:*",
"@posthog/products-account-audits": "workspace:*",
"@posthog/products-actions": "workspace:*",
+ "@posthog/products-ai-gateway": "workspace:*",
"@posthog/products-ai-observability": "workspace:*",
"@posthog/products-ai-training": "workspace:*",
"@posthog/products-alerts": "workspace:*",
diff --git a/frontend/snapshots.yml b/frontend/snapshots.yml
index 554044675cd4..5d1b91fa4f26 100644
--- a/frontend/snapshots.yml
+++ b/frontend/snapshots.yml
@@ -2605,9 +2605,9 @@ snapshots:
components-search--product-recents-and-starred--light:
hash: v1.k794b7964.de38039f5c4dd76a9caf4c523b664147d93a5920443ff6afa6353227e58ef2e6.NsoucGXVlw4tPpndBq9jFa-71CN-nHMr1VhLxOoPNUM
components-search--searching--dark:
- hash: v1.k794b7964.8c219491666e60b1ad086370c8f852cdf1acfee4535f64982efeb7613c19050e.1gjDWCOin1IZ-0bvNOSsHei1nJpA2_MCOU4otfuRxaM
+ hash: v1.k794b7964.4876b7eb9be38867981cb775fb84f75f9ccf1d5638203f81be9c3e058eb62446.4s3Zy0VeZxNoAny9paspwjFJ6WNRyFvEVII6oj2Jlh8
components-search--searching--light:
- hash: v1.k794b7964.9b06546bad6e583e43eed94fcfe5f99d97bbc30754f181095a127a1a16b336ef.ueejxmtIVer_cwBqE1T-UiEguosuhQPFz7AbasCihCg
+ hash: v1.k794b7964.f412be0e3c1c9d0c9e60b1607bc4ad3b229fbc55edacb37be6633401654d4dcd.EKt8wvyXXBhMLk_dTvmFTVjs0bSVcdtBYUHT_b5WH88
components-sentencelist--full-sentence--dark:
hash: v1.k794b7964.7e8e5dd2e09d246cc3668f362f2391fe67618bd1143aa393832297f5dc30e558.yptEWKc5RAUY613Fwd5cQ0VIinMvpdoI6cuKgagAGxk
components-sentencelist--full-sentence--light:
diff --git a/frontend/src/lib/components/ActivityLog/humanizeActivity.test.tsx b/frontend/src/lib/components/ActivityLog/humanizeActivity.test.tsx
index 8493b205eb6e..fe32bbcd17c5 100644
--- a/frontend/src/lib/components/ActivityLog/humanizeActivity.test.tsx
+++ b/frontend/src/lib/components/ActivityLog/humanizeActivity.test.tsx
@@ -6,6 +6,7 @@ import {
ActivityLogUserName,
Describer,
Description,
+ defaultDescriber,
userNameForLogItem,
} from 'lib/components/ActivityLog/humanizeActivity'
import { cohortActivityDescriber } from 'scenes/cohorts/activityDescriptions'
@@ -161,6 +162,24 @@ describe('humanizeActivity', () => {
}
})
+ it.each([
+ ['minted', 'A user created Developer mode token for [email protected]'],
+ ['revoked', 'A user revoked Developer mode token for [email protected]'],
+ ])('describes a %s DevGatewayToken row with the default describer', (activity, expected) => {
+ const logItem = makeLogItem({
+ scope: ActivityScope.DEV_GATEWAY_TOKEN,
+ activity,
+ detail: { merge: null, trigger: null, changes: null, name: 'Developer mode token for [email protected]' },
+ })
+ const { description } = defaultDescriber(logItem)
+
+ expect(
+ render(<>{description}</>)
+ .container.textContent?.replace(/\s+/g, ' ')
+ .trim()
+ ).toBe(expected)
+ })
+
describe('userNameForLogItem', () => {
it.each([
[
diff --git a/frontend/src/lib/components/ActivityLog/humanizeActivity.tsx b/frontend/src/lib/components/ActivityLog/humanizeActivity.tsx
index 9edd5a6ac073..d9be5010424b 100644
--- a/frontend/src/lib/components/ActivityLog/humanizeActivity.tsx
+++ b/frontend/src/lib/components/ActivityLog/humanizeActivity.tsx
@@ -221,6 +221,7 @@ const SCOPE_DISPLAY_NAMES: Partial<Record<ActivityScope, { singular: string; plu
[ActivityScope.ALERT_CONFIGURATION]: { singular: 'Alert', plural: 'Alerts' },
[ActivityScope.BATCH_EXPORT]: { singular: 'Destination', plural: 'Destinations' },
[ActivityScope.CANVAS]: { singular: 'Canvas', plural: 'Canvases' },
+ [ActivityScope.DEV_GATEWAY_TOKEN]: { singular: 'Developer mode token', plural: 'Developer mode tokens' },
[ActivityScope.EXTERNAL_DATA_SOURCE]: { singular: 'Source', plural: 'Sources' },
[ActivityScope.HOG_FUNCTION]: { singular: 'Data pipeline', plural: 'Data pipelines' },
[ActivityScope.PERSONAL_API_KEY]: { singular: 'Personal API key', plural: 'Personal API keys' },
@@ -260,6 +261,8 @@ const SIMPLE_ACTIVITY_VERBS: Record<string, string> = {
created: 'created',
restored: 'restored',
updated: 'updated',
+ minted: 'created',
+ revoked: 'revoked',
}
function describeComment(
diff --git a/frontend/src/scenes/settings/SettingsMap.tsx b/frontend/src/scenes/settings/SettingsMap.tsx
index ab2bd2fa51d8..0c89594fa451 100644
--- a/frontend/src/scenes/settings/SettingsMap.tsx
+++ b/frontend/src/scenes/settings/SettingsMap.tsx
@@ -52,6 +52,7 @@ import {
} from '~/layout/navigation-3000/sidepanel/panels/access_control/RolesAccessControls'
import { AccessControlLevel, AccessControlResourceType, AvailableFeature, Realm } from '~/types'
+import { DevModeSetting } from 'products/ai_gateway/frontend/components/DevModeSetting'
import { GithubReposSetting } from 'products/business_knowledge/frontend/scenes/settings/GithubReposSetting'
import { LearnFromSupportSetting } from 'products/business_knowledge/frontend/scenes/settings/LearnFromSupportSetting'
import { AISection } from 'products/conversations/frontend/scenes/settings/AISection'
@@ -2627,6 +2628,21 @@ export const SETTINGS_MAP: SettingSection[] = [
},
],
},
+ {
+ level: 'user',
+ id: 'user-ai-gateway-dev-mode',
+ title: 'AI gateway developer mode',
+ settings: [
+ {
+ id: 'ai-gateway-dev-mode',
+ title: 'Developer mode tokens',
+ description: 'AI gateway tokens for PostHog staff.',
+ component: <DevModeSetting />,
+ staffOnly: true,
+ keywords: ['ai gateway', 'llm gateway', 'token', 'developer mode', 'phe_'],
+ },
+ ],
+ },
{
level: 'user',
id: 'user-customization',
diff --git a/frontend/src/scenes/settings/project/ProjectSecretAPIKeys.test.tsx b/frontend/src/scenes/settings/project/ProjectSecretAPIKeys.test.tsx
index 1beac1c2d923..d69596b27442 100644
--- a/frontend/src/scenes/settings/project/ProjectSecretAPIKeys.test.tsx
+++ b/frontend/src/scenes/settings/project/ProjectSecretAPIKeys.test.tsx
@@ -4,6 +4,9 @@ import '@testing-library/jest-dom'
import { cleanup, render, screen } from '@testing-library/react'
+import { LemonDialog } from '@posthog/lemon-ui'
+
+import api from 'lib/api'
import { OrganizationMembershipLevel } from 'lib/constants'
import { teamLogic } from 'scenes/teamLogic'
@@ -11,7 +14,11 @@ import { ProjectSecretAPIKeyApi } from '~/generated/core/api.schemas'
import { initKeaTests } from '~/test/init'
import { ProjectSecretAPIKeys } from './ProjectSecretAPIKeys'
-import { MAX_PROJECT_API_KEYS_PER_PROJECT, projectSecretAPIKeysLogic } from './projectSecretAPIKeysLogic'
+import {
+ EditingProjectKeyFormValues,
+ MAX_PROJECT_API_KEYS_PER_PROJECT,
+ projectSecretAPIKeysLogic,
+} from './projectSecretAPIKeysLogic'
const createKeys = (count: number): ProjectSecretAPIKeyApi[] =>
Array.from({ length: count }, (_, index) => ({
@@ -49,6 +56,47 @@ describe('<ProjectSecretAPIKeys />', () => {
afterEach(() => {
cleanup()
logic.unmount()
+ jest.restoreAllMocks()
+ })
+
+ it.each([
+ { scopes: ['llm_gateway:read'], expectsConfirmation: true },
+ { scopes: ['endpoint:read'], expectsConfirmation: false },
+ ])(
+ 'asks to confirm a new key only when it has the AI gateway scope: $scopes',
+ async ({ scopes, expectsConfirmation }) => {
+ const create = jest.spyOn(api.projectSecretApiKeys, 'create').mockResolvedValue(createKeys(1)[0])
+ const dialog = jest.spyOn(LemonDialog, 'open').mockImplementation(() => {})
+ logic.actions.setEditingKeyId('new')
+ logic.actions.setEditingKeyValues({
+ label: 'Gateway',
+ scopes: scopes as EditingProjectKeyFormValues['scopes'],
+ })
+
+ await logic.asyncActions.submitEditingKey()
+
+ expect(dialog).toHaveBeenCalledTimes(expectsConfirmation ? 1 : 0)
+ expect(create).toHaveBeenCalledTimes(expectsConfirmation ? 0 : 1)
+ if (expectsConfirmation) {
+ logic.actions.confirmGatewayScope()
+ await logic.asyncActions.submitEditingKey()
+ expect(create).toHaveBeenCalledWith(expect.objectContaining({ scopes: ['llm_gateway:read'] }))
+ }
+ }
+ )
+
+ it.each([
+ { dialog: 'created', show: (key: ProjectSecretAPIKeyApi) => logic.actions.createKeySuccess(key) },
+ { dialog: 'rolled', show: (key: ProjectSecretAPIKeyApi) => logic.actions.showRollKeySuccessDialog(key) },
+ ])('hides the $dialog key value from session replay and autocapture', ({ show }) => {
+ const dialog = jest.spyOn(LemonDialog, 'open').mockImplementation(() => {})
+ const key = { ...createKeys(1)[0], value: 'phs_fixture_key_value' }
+
+ show(key)
+
+ expect(dialog).toHaveBeenCalledTimes(1)
+ render(<>{dialog.mock.calls[0][0].content}</>)
+ expect(screen.getByText(key.value).closest('.ph-no-capture')).not.toBeNull()
})
it.each([
diff --git a/frontend/src/scenes/settings/project/projectSecretAPIKeysLogic.tsx b/frontend/src/scenes/settings/project/projectSecretAPIKeysLogic.tsx
index a95a19d5e0cf..14ca09b8ce73 100644
--- a/frontend/src/scenes/settings/project/projectSecretAPIKeysLogic.tsx
+++ b/frontend/src/scenes/settings/project/projectSecretAPIKeysLogic.tsx
@@ -2,6 +2,7 @@ import { MakeLogicType, actions, afterMount, connect, kea, listeners, path, redu
import { forms } from 'kea-forms'
import type { DeepPartial, DeepPartialMap, FieldName, ValidationErrorType } from 'kea-forms'
import { loaders } from 'kea-loaders'
+import { router } from 'kea-router'
import { LemonDialog } from '@posthog/lemon-ui'
@@ -18,6 +19,7 @@ import {
} from 'lib/scopes'
import { capitalizeFirstLetter } from 'lib/utils/strings'
import { teamLogic } from 'scenes/teamLogic'
+import { urls } from 'scenes/urls'
import { userLogic } from 'scenes/userLogic'
import { ProjectSecretAPIKeyApi } from '~/generated/core/api.schemas'
@@ -29,6 +31,8 @@ export type EditingProjectKeyFormValues = ProjectSecretAPIKeyRequest & {
export const MAX_PROJECT_API_KEYS_PER_PROJECT = 50
+const GATEWAY_SCOPE = 'llm_gateway:read'
+
// PAKs label this scope "LLM gateway"; here it is the AI gateway service credential.
const PROJECT_SECRET_SCOPE_OBJECT_NAMES: Record<string, string> = {
llm_gateway: 'AI gateway',
@@ -55,6 +59,7 @@ export interface projectSecretAPIKeysLogicValues {
label: string
}[]
formScopeRadioValues: Record<string, string>
+ gatewayScopeConfirmed: boolean
isEditingKeySubmitting: boolean
isEditingKeyValid: boolean
keys: ProjectSecretAPIKeyApi[]
@@ -65,6 +70,9 @@ export interface projectSecretAPIKeysLogicValues {
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface projectSecretAPIKeysLogicActions {
+ confirmGatewayScope: () => {
+ value: true
+ }
createKeySuccess: (key: ProjectSecretAPIKeyApi) => {
key: ProjectSecretAPIKeyApi
}
@@ -163,6 +171,9 @@ export interface projectSecretAPIKeysLogicActions {
setSearchTerm: (searchTerm: string) => {
searchTerm: string
}
+ showGatewayScopeDialog: () => {
+ value: true
+ }
showRollKeySuccessDialog: (key: ProjectSecretAPIKeyApi) => {
key: ProjectSecretAPIKeyApi
}
@@ -226,6 +237,8 @@ export const projectSecretAPIKeysLogic = kea<projectSecretAPIKeysLogicType>([
setScopeRadioValue: (key: string, action: string) => ({ key, action }),
resetScopes: true,
setSearchTerm: (searchTerm: string) => ({ searchTerm }),
+ showGatewayScopeDialog: true,
+ confirmGatewayScope: true,
}),
reducers({
@@ -242,6 +255,13 @@ export const projectSecretAPIKeysLogic = kea<projectSecretAPIKeysLogicType>([
setEditingKeyId: () => '',
},
],
+ gatewayScopeConfirmed: [
+ false,
+ {
+ confirmGatewayScope: () => true,
+ setEditingKeyId: () => false,
+ },
+ ],
}),
loaders(({ values, actions }) => ({
@@ -306,6 +326,16 @@ export const projectSecretAPIKeysLogic = kea<projectSecretAPIKeysLogicType>([
return
}
+ // The generated scope type omits staff-only scopes, so compare as plain strings.
+ const savedScopes: string[] = values.keys.find((k) => k.id === values.editingKeyId)?.scopes ?? []
+ const addsGatewayScope =
+ (payload.scopes as string[] | undefined)?.includes(GATEWAY_SCOPE) &&
+ !savedScopes.includes(GATEWAY_SCOPE)
+ if (addsGatewayScope && !values.gatewayScopeConfirmed) {
+ actions.showGatewayScopeDialog()
+ return
+ }
+
try {
// Remove preset from payload as it's frontend-only
const { preset, ...apiPayload } = payload
@@ -450,6 +480,39 @@ export const projectSecretAPIKeysLogic = kea<projectSecretAPIKeysLogicType>([
})
},
+ showGatewayScopeDialog: () => {
+ LemonDialog.open({
+ title: 'This key is for PostHog products only',
+ width: 536,
+ content: (
+ <>
+ <p>
+ Use AI gateway keys for PostHog services that call the gateway in production. These keys
+ don't expire and their requests aren't attributed to a person.
+ </p>
+ <p className="mb-0">
+ To test against the gateway from your machine, create a developer mode token instead. It
+ expires after a few hours and its requests are attributed to you.
+ </p>
+ </>
+ ),
+ primaryButton: {
+ children: 'Create a developer mode token',
+ onClick: () => {
+ actions.setEditingKeyId(null)
+ router.actions.push(urls.settings('user-ai-gateway-dev-mode'))
+ },
+ },
+ secondaryButton: {
+ children: 'Create key for a PostHog product',
+ onClick: () => {
+ actions.confirmGatewayScope()
+ actions.submitEditingKey()
+ },
+ },
+ })
+ },
+
showRollKeySuccessDialog: ({ key }) => {
if (!key.value) {
return
@@ -461,7 +524,9 @@ export const projectSecretAPIKeysLogic = kea<projectSecretAPIKeysLogicType>([
content: (
<>
<p>Your new key for "{key.label}":</p>
- <CodeSnippet thing="project secret API key">{key.value}</CodeSnippet>
+ <CodeSnippet className="ph-no-capture" thing="project secret API key">
+ {key.value}
+ </CodeSnippet>
<p className="text-warning mt-4">
<strong>Warning:</strong> The previous key is no longer valid. This key will never be shown
again. Copy it now.
diff --git a/frontend/src/scenes/settings/settingsLogic.ts b/frontend/src/scenes/settings/settingsLogic.ts
index c38adb692800..5dac735cf8da 100644
--- a/frontend/src/scenes/settings/settingsLogic.ts
+++ b/frontend/src/scenes/settings/settingsLogic.ts
@@ -16,7 +16,7 @@ import { userLogic } from 'scenes/userLogic'
import type { FeatureFlagsSet } from '../../lib/logic/featureFlagLogic'
import type { IntegrationType, PreflightStatus, TeamPublicType, TeamType } from '../../types'
-import type { AvailableFeature, OrganizationType } from '../../types'
+import type { AvailableFeature, OrganizationType, UserType } from '../../types'
import { matchesFlagDefinition } from './flagGating'
import { ADMIN_ONLY_SECTION_IDS, findUnavailableSection } from './sectionGating'
import { SETTINGS_MAP } from './SettingsMap'
@@ -91,6 +91,7 @@ export interface settingsLogicValues {
preflight: PreflightStatus | null // preflightLogic
currentTeam: TeamPublicType | TeamType | null // teamLogic
hasAvailableFeature: (feature: AvailableFeature, currentUsage?: number | undefined) => boolean // userLogic
+ user: UserType | null // userLogic
collapsedGroups: Record<string, boolean>
collapsedLevels: Record<SettingLevelId, boolean>
defaultSectionId: SettingSectionId | null
@@ -177,7 +178,8 @@ export interface settingsLogicMeta {
preflight: PreflightStatus | null,
billingEntryUrl: string | null,
isAdminOrOwner: boolean | null,
- featureFlags: FeatureFlagsSet
+ featureFlags: FeatureFlagsSet,
+ user: UserType | null
) => SettingSection[]
selectedLevel: (
selectedLevelRaw: 'environment' | 'organization' | 'project' | 'user',
@@ -210,7 +212,8 @@ export interface settingsLogicMeta {
doesMatchFlags: (flagDefinition: Pick<Setting, 'flag'>) => boolean,
preflight: PreflightStatus | null,
currentTeam: TeamPublicType | TeamType | null,
- isAdminOrOwner: boolean | null
+ isAdminOrOwner: boolean | null,
+ user: UserType | null
) => Setting[]
selectedSetting: (settings: Setting[], selectedSettingId: SettingId | null) => Setting | null
doesMatchFlags: (featureFlags: FeatureFlagsSet) => (flagDefinition: Pick<Setting, 'flag'>) => boolean
@@ -222,7 +225,8 @@ export interface settingsLogicMeta {
doesMatchFlags: (flagDefinition: Pick<Setting, 'flag'>) => boolean,
preflight: PreflightStatus | null,
currentTeam: TeamPublicType | TeamType | null,
- isAdminOrOwner: boolean | null
+ isAdminOrOwner: boolean | null,
+ user: UserType | null
) => GlobalSearchFuse
searchResults: (searchTerm: string, globalSearchIndex: GlobalSearchFuse) => SearchResultGroup[]
filteredLevels: (
@@ -264,7 +268,7 @@ export const settingsLogic = kea<settingsLogicType>([
featureFlagLogic,
['featureFlags', 'receivedFeatureFlags'],
userLogic,
- ['hasAvailableFeature'],
+ ['hasAvailableFeature', 'user'],
preflightLogic,
['preflight', 'isCloudOrDev'],
teamLogic,
@@ -454,6 +458,7 @@ export const settingsLogic = kea<settingsLogicType>([
s.billingEntryUrl,
s.isAdminOrOwner,
s.featureFlags,
+ s.user,
],
(
doesMatchFlags: (flagDefinition: Pick<Setting, 'flag'>) => boolean,
@@ -464,7 +469,8 @@ export const settingsLogic = kea<settingsLogicType>([
preflight: null | import('../../types').PreflightStatus,
billingEntryUrl: string | null,
isAdminOrOwner: boolean | null,
- featureFlags: FeatureFlagsSet
+ featureFlags: FeatureFlagsSet,
+ user: UserType | null
): SettingSection[] => {
const isSettingVisible = (setting: Setting): boolean => {
if (!doesMatchFlags(setting)) {
@@ -479,6 +485,9 @@ export const settingsLogic = kea<settingsLogicType>([
if (setting.organizationAdminOnly && !isAdminOrOwner) {
return false
}
+ if (setting.staffOnly && !user?.is_staff) {
+ return false
+ }
return true
}
@@ -627,6 +636,7 @@ export const settingsLogic = kea<settingsLogicType>([
s.preflight,
s.currentTeam,
s.isAdminOrOwner,
+ s.user,
],
(
selectedLevel: SettingLevelId,
@@ -636,7 +646,8 @@ export const settingsLogic = kea<settingsLogicType>([
doesMatchFlags: (flagDefinition: Pick<Setting, 'flag'>) => boolean,
preflight: null | import('../../types').PreflightStatus,
currentTeam: null | import('../../types').TeamPublicType | import('../../types').TeamType,
- isAdminOrOwner: boolean | null
+ isAdminOrOwner: boolean | null,
+ user: UserType | null
): Setting[] => {
const effectiveSectionId = selectedSectionId ?? defaultSectionId
@@ -663,6 +674,9 @@ export const settingsLogic = kea<settingsLogicType>([
if (x.organizationAdminOnly && !isAdminOrOwner) {
return false
}
+ if (x.staffOnly && !user?.is_staff) {
+ return false
+ }
if (x.allowForTeam) {
return x.allowForTeam(currentTeam)
}
@@ -718,13 +732,14 @@ export const settingsLogic = kea<settingsLogicType>([
isSearching: [(s) => [s.searchTerm], (searchTerm: string): boolean => searchTerm.trim().length > 0],
globalSearchIndex: [
- (s) => [s.sections, s.doesMatchFlags, s.preflight, s.currentTeam, s.isAdminOrOwner],
+ (s) => [s.sections, s.doesMatchFlags, s.preflight, s.currentTeam, s.isAdminOrOwner, s.user],
(
sections: SettingSection[],
doesMatchFlags: (flagDefinition: Pick<Setting, 'flag'>) => boolean,
preflight: null | import('../../types').PreflightStatus,
currentTeam: null | import('../../types').TeamPublicType | import('../../types').TeamType,
- isAdminOrOwner: boolean | null
+ isAdminOrOwner: boolean | null,
+ user: UserType | null
): GlobalSearchFuse => {
const isSettingVisible = (setting: Setting): boolean => {
if (!doesMatchFlags(setting)) {
@@ -736,6 +751,9 @@ export const settingsLogic = kea<settingsLogicType>([
if (setting.organizationAdminOnly && !isAdminOrOwner) {
return false
}
+ if (setting.staffOnly && !user?.is_staff) {
+ return false
+ }
return !setting.allowForTeam || setting.allowForTeam(currentTeam)
}
diff --git a/frontend/src/scenes/settings/staffOnly.test.ts b/frontend/src/scenes/settings/staffOnly.test.ts
new file mode 100644
index 000000000000..4742b85d7159
--- /dev/null
+++ b/frontend/src/scenes/settings/staffOnly.test.ts
@@ -0,0 +1,33 @@
+import { MOCK_DEFAULT_USER } from 'lib/api.mock'
+
+import { userLogic } from 'scenes/userLogic'
+
+import { initKeaTests } from '~/test/init'
+
+import { settingsLogic } from './settingsLogic'
+
+jest.mock('posthog-js/dist/surveys-preview', () => ({
+ renderFeedbackWidgetPreview: jest.fn(),
+ renderSurveysPreview: jest.fn(),
+}))
+
+describe('staffOnly settings', () => {
+ it.each([
+ [true, true],
+ [false, false],
+ ])('is_staff=%s shows AI gateway developer mode: %s', (isStaff, visible) => {
+ initKeaTests()
+ userLogic.mount()
+ userLogic.actions.loadUserSuccess({ ...MOCK_DEFAULT_USER, is_staff: isStaff })
+ const logic = settingsLogic({ logicKey: 'staff-only-test', sectionId: 'user-ai-gateway-dev-mode' })
+ logic.mount()
+
+ expect(logic.values.sections.some((section) => section.id === 'user-ai-gateway-dev-mode')).toBe(visible)
+ expect(logic.values.settings.some((setting) => setting.id === 'ai-gateway-dev-mode')).toBe(visible)
+ expect(
+ logic.values.globalSearchIndex
+ .search('developer mode')
+ .some(({ item }) => item.settingId === 'ai-gateway-dev-mode')
+ ).toBe(visible)
+ })
+})
diff --git a/frontend/src/scenes/settings/types.ts b/frontend/src/scenes/settings/types.ts
index 82dfecba2204..81cb1b1fe73a 100644
--- a/frontend/src/scenes/settings/types.ts
+++ b/frontend/src/scenes/settings/types.ts
@@ -102,6 +102,7 @@ export type SettingSectionId =
| 'user-profile'
| 'user-api-keys'
| 'user-connected-apps'
+ | 'user-ai-gateway-dev-mode'
| 'user-customization'
| 'user-feature-previews'
| 'user-notifications'
@@ -137,6 +138,7 @@ export type SettingId =
| 'changelog'
| 'channel-type'
| 'connected-apps'
+ | 'ai-gateway-dev-mode'
| 'conversations-ai'
| 'conversations-channels'
| 'conversations-general'
@@ -358,6 +360,9 @@ export type Setting = {
*/
organizationAdminOnly?: boolean
+ /** Shows the setting only to PostHog staff. The API it calls must enforce the same check. */
+ staffOnly?: boolean
+
/**
* If true, this setting will be hidden when viewing all settings (no specific section selected),
* but will still appear when viewing its specific section directly
diff --git a/frontend/src/types.ts b/frontend/src/types.ts
index 19fac82fa5cd..4ab98f8ff54f 100644
--- a/frontend/src/types.ts
+++ b/frontend/src/types.ts
@@ -6128,6 +6128,7 @@ export type PromptFlag = {
// Should be kept in sync with "posthog/models/activity_logging/activity_log.py"
export enum ActivityScope {
DATA_QUALITY_CHECK_SCHEDULE = 'DataQualityCheckSchedule',
+ DEV_GATEWAY_TOKEN = 'DevGatewayToken',
ACTION = 'Action',
ACCOUNT_VIEW = 'AccountView',
ALERT_CONFIGURATION = 'AlertConfiguration',
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index 9c2e24431677..a22d1b49df43 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -644,6 +644,9 @@ importers:
'@posthog/products-actions':
specifier: workspace:*
version: link:../products/actions
+ '@posthog/products-ai-gateway':
+ specifier: workspace:*
+ version: link:../products/ai_gateway
'@posthog/products-ai-observability':
specifier: workspace:*
version: link:../products/ai_observability
@@ -2385,6 +2388,31 @@ importers:
products/aeo: {}
+ products/ai_gateway:
+ dependencies:
+ '@posthog/lemon-ui':
+ specifier: '*'
+ version: 0.0.0([email protected]([email protected])([email protected])([email protected])([email protected]([email protected]))([email protected]))([email protected]([email protected](patch_hash=139b8d1f1304f9d9da452a9a1244c94ea679dbcb85687d8999563146879fb6f5)([email protected])))([email protected](patch_hash=139b8d1f1304f9d9da452a9a1244c94ea679dbcb85687d8999563146879fb6f5)([email protected]))([email protected]([email protected]))([email protected])
+ '@types/react':
+ specifier: 18.3.27
+ version: 18.3.27
+ kea:
+ specifier: 'catalog:'
+ version: 4.0.0-pre.6(patch_hash=139b8d1f1304f9d9da452a9a1244c94ea679dbcb85687d8999563146879fb6f5)([email protected])
+ kea-loaders:
+ specifier: 'catalog:'
+ version: 3.1.1([email protected](patch_hash=139b8d1f1304f9d9da452a9a1244c94ea679dbcb85687d8999563146879fb6f5)([email protected]))
+ kea-router:
+ specifier: 'catalog:'
+ version: 3.4.2([email protected](patch_hash=139b8d1f1304f9d9da452a9a1244c94ea679dbcb85687d8999563146879fb6f5)([email protected]))
+ react:
+ specifier: 18.3.1
+ version: 18.3.1
+ devDependencies:
+ '@testing-library/react':
+ specifier: '*'
+
products/ai_observability:
dependencies:
'@posthog/brand':
diff --git a/posthog/middleware.py b/posthog/middleware.py
index c77f775a7711..402bdc79214a 100644
--- a/posthog/middleware.py
+++ b/posthog/middleware.py
@@ -1695,6 +1695,7 @@ class ImpersonationReadOnlyMiddleware:
IMPERSONATION_BLOCKED_PATHS: list[str] = [
"/api/users/",
"/api/personal_api_keys/",
+ "/api/ai_gateway/dev_tokens/",
]
diff --git a/posthog/models/activity_logging/activity_log.py b/posthog/models/activity_logging/activity_log.py
index e62cfa2d3e4c..98bd5ee8bf4d 100644
--- a/posthog/models/activity_logging/activity_log.py
+++ b/posthog/models/activity_logging/activity_log.py
@@ -49,6 +49,7 @@ ACTIVITY_LOG_WRITE_FAILURES = Counter(
ActivityScope = Literal[
"Cohort",
"CrossProjectDashboard",
+ "DevGatewayToken",
"FeatureFlag",
"Person",
"Group",
diff --git a/posthog/settings/web.py b/posthog/settings/web.py
index c565b78e3749..6ed69906f979 100644
--- a/posthog/settings/web.py
+++ b/posthog/settings/web.py
@@ -64,6 +64,7 @@ PRODUCTS_APPS = [
"products.revenue_analytics.backend.apps.RevenueAnalyticsConfig",
"products.user_interviews.backend.apps.UserInterviewsConfig",
"products.ai_observability.backend.apps.AIObservabilityConfig",
+ "products.ai_gateway.backend.apps.AiGatewayConfig",
"products.llm_analytics.backend.apps.LlmAnalyticsConfig",
"products.skills.backend.apps.SkillsConfig",
"products.endpoints.backend.apps.EndpointsConfig",
@@ -1416,6 +1417,19 @@ DESKTOP_GATEWAY_TOKEN_TTL_SECONDS = get_from_env("DESKTOP_GATEWAY_TOKEN_TTL_SECO
# Per-user mint ceiling because OAuth callers skip DRF's default throttles. At the default TTL each
# open project on each device mints about 13 times an hour.
DESKTOP_GATEWAY_MINTS_PER_HOUR = get_from_env("DESKTOP_GATEWAY_MINTS_PER_HOUR", 120, type_cast=int)
+
+# AI gateway developer mode. DEV_GATEWAY_MINT_KEY is a phs_ key with llm_gateway:read on
+# DEV_GATEWAY_TEAM_ID, whose wallet pays.
+DEV_GATEWAY_URL = get_from_env("DEV_GATEWAY_URL", "")
+DEV_GATEWAY_MINT_KEY = get_from_env("DEV_GATEWAY_MINT_KEY", "")
+DEV_GATEWAY_TEAM_ID = get_from_env("DEV_GATEWAY_TEAM_ID", 0, type_cast=int)
+DEV_GATEWAY_TOKEN_CAP_USD = get_from_env("DEV_GATEWAY_TOKEN_CAP_USD", "25")
+DEV_GATEWAY_TOKEN_TTL_SECONDS = get_from_env("DEV_GATEWAY_TOKEN_TTL_SECONDS", 8 * 60 * 60, type_cast=int)
+# Caps minted in the trailing 24 hours count against this, so the gateway's reserved cap per token
+# makes it a hard ceiling on what one user can spend in a day.
+DEV_GATEWAY_DAILY_CAP_USD = get_from_env("DEV_GATEWAY_DAILY_CAP_USD", "50")
+DEV_GATEWAY_MINTS_PER_HOUR = get_from_env("DEV_GATEWAY_MINTS_PER_HOUR", 20, type_cast=int)
+
# Users who joined PostHog at or after this ISO 8601 instant cannot use Desktop while the
# posthog-desktop-signup-gate flag is on for them, unless posthog-desktop-access-override matches
# them. An empty value turns the signup gate off.
diff --git a/products/ai_gateway/__init__.py b/products/ai_gateway/__init__.py
new file mode 100644
index 000000000000..e69de29bb2d1
diff --git a/products/ai_gateway/backend/__init__.py b/products/ai_gateway/backend/__init__.py
new file mode 100644
index 000000000000..e69de29bb2d1
diff --git a/products/ai_gateway/backend/apps.py b/products/ai_gateway/backend/apps.py
new file mode 100644
index 000000000000..9222da79667e
--- /dev/null
+++ b/products/ai_gateway/backend/apps.py
@@ -0,0 +1,8 @@
+"""Django app configuration for ai_gateway."""
+
+from django.apps import AppConfig
+
+
+class AiGatewayConfig(AppConfig):
+ name = "products.ai_gateway.backend"
+ label = "ai_gateway"
diff --git a/products/ai_gateway/backend/facade/__init__.py b/products/ai_gateway/backend/facade/__init__.py
new file mode 100644
index 000000000000..e69de29bb2d1
diff --git a/products/ai_gateway/backend/facade/api.py b/products/ai_gateway/backend/facade/api.py
new file mode 100644
index 000000000000..ee53124a1d5a
--- /dev/null
+++ b/products/ai_gateway/backend/facade/api.py
@@ -0,0 +1,39 @@
+"""
+Facade for ai_gateway.
+
+The ONLY module other products are allowed to import.
+Accept frozen dataclasses, call logic/, return frozen
+dataclasses. Never return ORM instances or import DRF.
+"""
+
+from __future__ import annotations
+
+from posthog.models.user import User
+
+from .. import logic
+from . import contracts
+from .enums import DevTokenClient
+
+
+def eligibility_refusal(user: User) -> contracts.DevTokenRefused | None:
+ return logic.eligibility_refusal(user)
+
+
+def mint_dev_token(user: User, client: DevTokenClient) -> contracts.MintedDevToken:
+ return logic.mint(user, client)
+
+
+def dev_token_usage(user: User) -> contracts.DevTokenUsage:
+ return logic.usage(user)
+
+
+def revoke_dev_tokens(user: User, token_id: str | None) -> int:
+ return logic.revoke(user, token_id)
+
+
+def authorize_cli(user: User, code_challenge: str) -> str:
+ return logic.authorize_cli(user, code_challenge)
+
+
+def exchange_cli_code(code: str, code_verifier: str) -> contracts.MintedDevToken:
+ return logic.exchange_cli_code(code, code_verifier)
diff --git a/products/ai_gateway/backend/facade/contracts.py b/products/ai_gateway/backend/facade/contracts.py
new file mode 100644
index 000000000000..89f2b4362a47
--- /dev/null
+++ b/products/ai_gateway/backend/facade/contracts.py
@@ -0,0 +1,54 @@
+"""
+Contract types for ai_gateway.
+
+Frozen dataclasses that define what this product exposes.
+No Django imports. Used by facade as inputs/outputs.
+"""
+
+from datetime import datetime
+from decimal import Decimal
+from uuid import UUID
+
+from pydantic.dataclasses import dataclass
+
+from .enums import DevTokenClient, DevTokenStatus
+
+
+@dataclass(frozen=True)
+class DevToken:
+ id: UUID
+ client: DevTokenClient
+ cap_usd: Decimal
+ created_at: datetime
+ expires_at: datetime | None
+ revoked_at: datetime | None
+ status: DevTokenStatus
+
+
+@dataclass(frozen=True)
+class MintedDevToken:
+ id: UUID
+ token: str
+ cap_usd: Decimal
+ expires_at: datetime
+ gateway_url: str
+ product: str
+
+
+@dataclass(frozen=True)
+class DevTokenUsage:
+ daily_cap_usd: Decimal
+ used_usd: Decimal
+ token_cap_usd: Decimal
+ ttl_seconds: int
+ tokens: list[DevToken]
+
+
+class DevTokenRefused(Exception):
+ """A mint, exchange or revoke the caller cannot have. `code` is stable for clients."""
+
+ def __init__(self, code: str, detail: str, http_status: int) -> None:
+ super().__init__(detail)
+ self.code = code
+ self.detail = detail
+ self.http_status = http_status
diff --git a/products/ai_gateway/backend/facade/enums.py b/products/ai_gateway/backend/facade/enums.py
new file mode 100644
index 000000000000..9c95325dca6c
--- /dev/null
+++ b/products/ai_gateway/backend/facade/enums.py
@@ -0,0 +1,23 @@
+"""
+Exported enums for ai_gateway.
+
+If an enum appears in a contract dataclass field, it belongs here.
+Internal-only constants (DB magic values, feature flags) stay in
+the implementation (logic.py, models.py).
+
+No Django imports: use LabeledStrEnum or LabeledIntEnum, not
+models.TextChoices, for an enum that backs model or serializer choices.
+"""
+
+from posthog.enums import LabeledStrEnum
+
+
+class DevTokenClient(LabeledStrEnum):
+ WEB = "web", "Web"
+ CLI = "cli", "CLI"
+
+
+class DevTokenStatus(LabeledStrEnum):
+ LIVE = "live", "Live"
+ REVOKED = "revoked", "Revoked"
+ EXPIRED = "expired", "Expired"
diff --git a/products/ai_gateway/backend/logic.py b/products/ai_gateway/backend/logic.py
new file mode 100644
index 000000000000..e648331c7814
--- /dev/null
+++ b/products/ai_gateway/backend/logic.py
@@ -0,0 +1,339 @@
+"""Short-lived AI gateway tokens for PostHog staff.
+
+The gateway pins the staff user's distinct_id on each token, so no request header can
+change who a call is attributed to.
+"""
+
+from __future__ import annotations
+
+import hmac
+import json
+import base64
+import hashlib
+import secrets
+from datetime import datetime, timedelta
+from decimal import Decimal
+
+from django.conf import settings
+from django.db import transaction
+from django.db.models import Sum
+from django.utils import timezone
+
+import requests
+import structlog
+from urllib3.exceptions import NewConnectionError
+
+from posthog.models.activity_logging.activity_log import Change, Detail, log_activity
+from posthog.models.scoping import team_scope
+from posthog.models.team import Team
+from posthog.models.user import User
+from posthog.redis import get_client
+
+from .facade.contracts import DevToken, DevTokenRefused, DevTokenUsage, MintedDevToken
+from .facade.enums import DevTokenClient, DevTokenStatus
+from .models import DevGatewayToken
+
+logger = structlog.get_logger(__name__)
+
+DEV_PRODUCT = "dev"
+STAFF_EMAIL_DOMAIN = "@posthog.com"
+GATEWAY_TIMEOUT_SECONDS = 5
+CLI_CODE_TTL_SECONDS = 120
+_CLI_CODE_CACHE_PREFIX = "ai_gateway_dev_cli_code:"
+
+
+def configured() -> bool:
+ return bool(settings.DEV_GATEWAY_URL and settings.DEV_GATEWAY_MINT_KEY and settings.DEV_GATEWAY_TEAM_ID)
+
+
+def gateway_base_url() -> str:
+ return (settings.DEV_GATEWAY_URL or "").rstrip("/").removesuffix("/v1")
+
+
+def token_cap_usd() -> Decimal:
+ return Decimal(settings.DEV_GATEWAY_TOKEN_CAP_USD)
+
+
+def daily_cap_usd() -> Decimal:
+ return Decimal(settings.DEV_GATEWAY_DAILY_CAP_USD)
+
+
+def eligibility_refusal(user: User) -> DevTokenRefused | None:
+ """Impersonation is request state, so the caller checks it before this."""
+ if not user.is_active or not user.is_staff:
+ return DevTokenRefused("not_staff", "Developer mode is only available to PostHog staff.", 403)
+ # None is a legacy account that predates verification.
+ if not (user.email or "").lower().endswith(STAFF_EMAIL_DOMAIN) or user.is_email_verified is False:
+ return DevTokenRefused("not_staff", "Developer mode needs a verified @posthog.com email.", 403)
+ if not user.distinct_id:
+ return DevTokenRefused(
+ "not_staff", "Your account has no distinct_id, so spend can't be attributed to you.", 403
+ )
+ if not configured():
+ return DevTokenRefused("unconfigured", "Developer mode is not configured on this instance.", 503)
+ return None
+
+
+def _window_start() -> datetime:
+ return timezone.now() - timedelta(days=1)
+
+
+def _used_usd(user_id: int) -> Decimal:
+ used = DevGatewayToken.objects.filter(user_id=user_id, created_at__gte=_window_start()).aggregate(
+ total=Sum("cap_usd")
+ )["total"]
+ return used or Decimal("0")
+
+
+def _to_dto(row: DevGatewayToken) -> DevToken:
+ return DevToken(
+ id=row.id,
+ client=DevTokenClient(row.client),
+ cap_usd=row.cap_usd,
+ created_at=row.created_at,
+ expires_at=row.expires_at,
+ revoked_at=row.revoked_at,
+ status=(
+ DevTokenStatus.REVOKED
+ if row.revoked_at
+ else DevTokenStatus.EXPIRED
+ if row.expires_at and row.expires_at <= timezone.now()
+ else DevTokenStatus.LIVE
+ ),
+ )
+
+
+def _reserve(user: User, client: DevTokenClient) -> DevGatewayToken:
+ cap = token_cap_usd()
+ with transaction.atomic():
+ # Serializes one user's mints so two concurrent requests cannot both fit under the cap.
+ User.objects.select_for_update().filter(pk=user.pk).first()
+ used = _used_usd(user.pk)
+ if used + cap > daily_cap_usd():
+ oldest = (
+ DevGatewayToken.objects.filter(user_id=user.pk, created_at__gte=_window_start())
+ .order_by("created_at")
+ .values_list("created_at", flat=True)
+ .first()
+ )
+ available_at = (oldest + timedelta(days=1)) if oldest else timezone.now()
+ raise DevTokenRefused(
+ "daily_limit",
+ f"You've used ${used:.2f} of your ${daily_cap_usd():.2f} limit for the last 24 hours. "
+ f"You can create another token at {available_at:%Y-%m-%d %H:%M} UTC.",
+ 429,
+ )
+ return DevGatewayToken.objects.create(
+ team_id=settings.DEV_GATEWAY_TEAM_ID, user=user, client=client.value, cap_usd=cap
+ )
+
+
+def _gateway_post(path: str, body: dict) -> requests.Response:
+ return requests.post(
+ f"{gateway_base_url()}{path}",
+ json=body,
+ headers={"Authorization": f"Bearer {settings.DEV_GATEWAY_MINT_KEY}"},
+ timeout=GATEWAY_TIMEOUT_SECONDS,
+ allow_redirects=False,
+ )
+
+
+class _UnreadableMint(DevTokenRefused):
+ """The gateway answered 201, so it may have minted a token PostHog can't read."""
+
+
+def _call_mint(user: User, cap: Decimal) -> dict:
+ body = {
+ "cap_usd": str(cap),
+ "ttl_seconds": settings.DEV_GATEWAY_TOKEN_TTL_SECONDS,
+ "product": DEV_PRODUCT,
+ "user": user.distinct_id,
+ }
+ response = _gateway_post("/v1/tokens", body)
+ if response.status_code != 201:
+ logger.warning("ai_gateway_dev_mint_refused", status=response.status_code)
+ raise DevTokenRefused(
+ "mint_failed",
+ f"The AI gateway rejected the request (HTTP {response.status_code}). Try again in a minute.",
+ 503,
+ )
+ try:
+ minted = response.json()
+ except ValueError as e:
+ raise _UnreadableMint("mint_failed", "The AI gateway sent a response PostHog couldn't read.", 503) from e
+ if not isinstance(minted, dict) or not minted.get("token") or not minted.get("expires_at"):
+ raise _UnreadableMint("mint_failed", "The AI gateway sent a response without a token.", 503)
+ return minted
+
+
+def _log(user: User, row: DevGatewayToken, activity: str, changes: list[Change]) -> None:
+ log_activity(
+ organization_id=Team.objects.only("organization_id").get(pk=row.team_id).organization_id,
+ team_id=row.team_id,
+ user=user,
+ was_impersonated=False,
+ item_id=row.id,
+ scope="DevGatewayToken",
+ activity=activity,
+ detail=Detail(name=f"Developer mode token for {user.email}", changes=changes),
+ )
+
+
+def _never_sent(error: requests.RequestException) -> bool:
+ if isinstance(error, requests.ConnectTimeout):
+ return True
+ if not isinstance(error, (requests.Timeout, requests.ConnectionError)):
+ return True
+ # Refused connections and DNS failures arrive as a MaxRetryError whose reason is a NewConnectionError.
+ return isinstance(getattr(error.args[0] if error.args else None, "reason", None), NewConnectionError)
+
+
+def mint(user: User, client: DevTokenClient) -> MintedDevToken:
+ with team_scope(settings.DEV_GATEWAY_TEAM_ID):
+ row = _reserve(user, client)
+ try:
+ minted = _call_mint(user, row.cap_usd)
+ except requests.RequestException as e:
+ logger.warning("ai_gateway_dev_mint_unreachable", token_id=str(row.id), exc_info=True)
+ # A read timeout or dropped response may follow a successful mint, so that reservation keeps counting.
+ if _never_sent(e):
+ row.delete()
+ raise DevTokenRefused("mint_failed", "Couldn't reach the AI gateway. Try again in a minute.", 503) from e
+ except _UnreadableMint:
+ logger.warning("ai_gateway_dev_mint_unreadable", token_id=str(row.id))
+ raise
+ except Exception:
+ row.delete()
+ raise
+ token: str = minted["token"]
+ row.token_hash = hashlib.sha256(token.encode()).hexdigest()
+ row.expires_at = minted["expires_at"]
+ row.save(update_fields=["token_hash", "expires_at"])
+ row.refresh_from_db(fields=["expires_at"])
+ _log(
+ user,
+ row,
+ "minted",
+ [
+ Change(type="DevGatewayToken", action="created", field="client", after=row.client),
+ Change(type="DevGatewayToken", action="created", field="cap_usd", after=str(row.cap_usd)),
+ Change(type="DevGatewayToken", action="created", field="expires_at", after=row.expires_at.isoformat()),
+ ],
+ )
+ logger.info("ai_gateway_dev_token_minted", user_id=user.pk, token_id=str(row.id), client=client.value)
+ assert row.expires_at is not None
+ return MintedDevToken(
+ id=row.id,
+ token=token,
+ cap_usd=row.cap_usd,
+ expires_at=row.expires_at,
+ gateway_url=gateway_base_url(),
+ product=DEV_PRODUCT,
+ )
+
+
+def usage(user: User) -> DevTokenUsage:
+ with team_scope(settings.DEV_GATEWAY_TEAM_ID):
+ # Every token that counts against the daily limit, including revoked and expired ones.
+ recent = DevGatewayToken.objects.filter(
+ user_id=user.pk, token_hash__isnull=False, created_at__gte=_window_start()
+ ).order_by("-created_at")
+ tokens = [_to_dto(row) for row in recent]
+ used = _used_usd(user.pk)
+ return DevTokenUsage(
+ daily_cap_usd=daily_cap_usd(),
+ used_usd=used,
+ token_cap_usd=token_cap_usd(),
+ ttl_seconds=settings.DEV_GATEWAY_TOKEN_TTL_SECONDS,
+ tokens=tokens,
+ )
+
+
+def revoke(user: User, token_id: str | None) -> int:
+ """Returns the gateway's revoked count. Rows are marked revoked only after the gateway confirms."""
+ with team_scope(settings.DEV_GATEWAY_TEAM_ID):
+ rows = DevGatewayToken.objects.filter(
+ user_id=user.pk, token_hash__isnull=False, revoked_at__isnull=True, expires_at__gt=timezone.now()
+ )
+ if token_id is not None:
+ rows = rows.filter(id=token_id)
+ targets = list(rows)
+ if token_id is not None and not targets:
+ raise DevTokenRefused("not_found", f"Token {token_id} is already revoked or expired.", 404)
+ if not targets:
+ return 0
+ try:
+ response = _gateway_post("/v1/tokens/revoke", {"token_hashes": [row.token_hash for row in targets]})
+ except requests.RequestException as e:
+ logger.warning("ai_gateway_dev_revoke_unreachable", user_id=user.pk, exc_info=True)
+ raise DevTokenRefused("revoke_failed", "Couldn't reach the AI gateway. Try again in a minute.", 503) from e
+ if response.status_code != 200:
+ logger.warning("ai_gateway_dev_revoke_refused", user_id=user.pk, status=response.status_code)
+ raise DevTokenRefused(
+ "revoke_failed",
+ f"The AI gateway rejected the revoke (HTTP {response.status_code}). Try again in a minute.",
+ 503,
+ )
+ try:
+ revoked_count = int(response.json().get("revoked_count", 0))
+ except (ValueError, AttributeError, TypeError):
+ revoked_count = 0
+ # The gateway fails the whole request if any hash fails, so a short count means some tokens
+ # were already gone (expired or revoked elsewhere) and every target is dead either way.
+ if revoked_count != len(targets):
+ logger.warning(
+ "ai_gateway_dev_revoke_count_mismatch", user_id=user.pk, sent=len(targets), revoked=revoked_count
+ )
+ revoked_at = timezone.now()
+ DevGatewayToken.objects.filter(
+ user_id=user.pk, team_id=settings.DEV_GATEWAY_TEAM_ID, id__in=[row.id for row in targets]
+ ).update(revoked_at=revoked_at)
+ # The bulk update skips save(), so each revoke is logged by hand.
+ for row in targets:
+ _log(
+ user,
+ row,
+ "revoked",
+ [Change(type="DevGatewayToken", action="changed", field="revoked_at", after=revoked_at.isoformat())],
+ )
+ logger.info("ai_gateway_dev_tokens_revoked", user_id=user.pk, count=len(targets))
+ return revoked_count
+
+
+def _s256(verifier: str) -> str:
+ return base64.urlsafe_b64encode(hashlib.sha256(verifier.encode()).digest()).rstrip(b"=").decode()
+
+
+def _code_key(code: str) -> str:
+ return _CLI_CODE_CACHE_PREFIX + hashlib.sha256(code.encode()).hexdigest()
+
+
+def authorize_cli(user: User, code_challenge: str) -> str:
+ """Issues a one-time code the browser hands to the CLI's loopback listener. Only the
+ holder of the PKCE verifier behind `code_challenge` can exchange it."""
+ code = secrets.token_urlsafe(32)
+ # The plain client talks to the primary, so the exchange can't miss a grant on a lagging replica.
+ get_client().set(
+ _code_key(code), json.dumps({"user_id": user.pk, "challenge": code_challenge}), ex=CLI_CODE_TTL_SECONDS
+ )
+ return code
+
+
+def exchange_cli_code(code: str, code_verifier: str) -> MintedDevToken:
+ # GET and DEL in one MULTI, so a replayed code reads nothing. GETDEL would need Redis 6.2.
+ pipe = get_client().pipeline(transaction=True)
+ pipe.get(_code_key(code))
+ pipe.delete(_code_key(code))
+ raw, _ = pipe.execute()
+ grant = json.loads(raw) if raw else None
+ if not isinstance(grant, dict):
+ raise DevTokenRefused("invalid_code", "code is unknown, expired or already used", 400)
+ if not hmac.compare_digest(_s256(code_verifier), grant["challenge"]):
+ raise DevTokenRefused("invalid_code", "code_verifier does not match the code_challenge", 400)
+ user = User.objects.filter(pk=grant["user_id"]).first()
+ if user is None:
+ raise DevTokenRefused("invalid_code", "code is unknown, expired or already used", 400)
+ refusal = eligibility_refusal(user)
+ if refusal is not None:
+ raise refusal
+ return mint(user, DevTokenClient.CLI)
diff --git a/products/ai_gateway/backend/migrations/0001_initial.py b/products/ai_gateway/backend/migrations/0001_initial.py
new file mode 100644
index 000000000000..aeecbcdac3cb
--- /dev/null
+++ b/products/ai_gateway/backend/migrations/0001_initial.py
@@ -0,0 +1,55 @@
+import django.db.models.deletion
+from django.conf import settings
+from django.db import migrations, models
+
+import posthog.models.utils
+
+
+class Migration(migrations.Migration):
+ initial = True
+
+ dependencies = [
+ ("posthog", "1404_organization_inactive_quota_index"),
+ migrations.swappable_dependency(settings.AUTH_USER_MODEL),
+ ]
+
+ operations = [
+ migrations.CreateModel(
+ name="DevGatewayToken",
+ fields=[
+ (
+ "id",
+ models.UUIDField(
+ default=posthog.models.utils.uuid7, editable=False, primary_key=True, serialize=False
+ ),
+ ),
+ ("client", models.CharField(choices=[("web", "Web"), ("cli", "CLI")], max_length=16)),
+ ("cap_usd", models.DecimalField(decimal_places=2, max_digits=10)),
+ ("token_hash", models.CharField(blank=True, max_length=64, null=True, unique=True)),
+ ("created_at", models.DateTimeField(auto_now_add=True)),
+ ("expires_at", models.DateTimeField(blank=True, null=True)),
+ ("revoked_at", models.DateTimeField(blank=True, null=True)),
+ (
+ "team",
+ models.ForeignKey(
+ db_constraint=False,
+ on_delete=django.db.models.deletion.CASCADE,
+ related_name="+",
+ to="posthog.team",
+ ),
+ ),
+ (
+ "user",
+ models.ForeignKey(
+ db_constraint=False,
+ on_delete=django.db.models.deletion.CASCADE,
+ related_name="+",
+ to=settings.AUTH_USER_MODEL,
+ ),
+ ),
+ ],
+ options={
+ "indexes": [models.Index(fields=["team", "user", "created_at"], name="ai_gw_devtoken_user_created")],
+ },
+ ),
+ ]
diff --git a/products/ai_gateway/backend/migrations/__init__.py b/products/ai_gateway/backend/migrations/__init__.py
new file mode 100644
index 000000000000..e69de29bb2d1
diff --git a/products/ai_gateway/backend/migrations/max_migration.txt b/products/ai_gateway/backend/migrations/max_migration.txt
new file mode 100644
index 000000000000..cbab66dde92a
--- /dev/null
+++ b/products/ai_gateway/backend/migrations/max_migration.txt
@@ -0,0 +1 @@
+0001_initial
diff --git a/products/ai_gateway/backend/models.py b/products/ai_gateway/backend/models.py
new file mode 100644
index 000000000000..bded170b8ad5
--- /dev/null
+++ b/products/ai_gateway/backend/models.py
@@ -0,0 +1,31 @@
+from django.db import models
+
+from posthog.models.scoping.root_mixin import TeamScopedRootMixin
+from posthog.models.utils import uuid7
+
+from .facade.enums import DevTokenClient
+
+
+class DevGatewayToken(TeamScopedRootMixin):
+ """`team` is the team whose wallet pays (DEV_GATEWAY_TEAM_ID), not the user's current
+ project. Only the token's sha256 is stored; the bearer is returned once at mint.
+ A row exists from before the gateway call, so a concurrent mint counts it against
+ the daily cap; a mint the gateway refused deletes it. The foreign keys skip the DB constraint so
+ creating this table takes no lock on posthog_team or posthog_user; Django still cascades.
+ """
+
+ id = models.UUIDField(primary_key=True, default=uuid7, editable=False)
+ team = models.ForeignKey("posthog.Team", on_delete=models.CASCADE, related_name="+", db_constraint=False)
+ user = models.ForeignKey("posthog.User", on_delete=models.CASCADE, related_name="+", db_constraint=False)
+ client = models.CharField(max_length=16, choices=DevTokenClient.choices)
+ cap_usd = models.DecimalField(max_digits=10, decimal_places=2)
+ token_hash = models.CharField(max_length=64, null=True, blank=True, unique=True)
+ created_at = models.DateTimeField(auto_now_add=True)
+ expires_at = models.DateTimeField(null=True, blank=True)
+ revoked_at = models.DateTimeField(null=True, blank=True)
+
+ class Meta:
+ indexes = [models.Index(fields=["team", "user", "created_at"], name="ai_gw_devtoken_user_created")]
+
+ def __str__(self) -> str:
+ return f"DevGatewayToken({self.id})"
diff --git a/products/ai_gateway/backend/presentation/__init__.py b/products/ai_gateway/backend/presentation/__init__.py
new file mode 100644
index 000000000000..e69de29bb2d1
diff --git a/products/ai_gateway/backend/presentation/serializers.py b/products/ai_gateway/backend/presentation/serializers.py
new file mode 100644
index 000000000000..0674b7625bc0
--- /dev/null
+++ b/products/ai_gateway/backend/presentation/serializers.py
@@ -0,0 +1,76 @@
+from rest_framework import serializers
+
+from ..facade.enums import DevTokenClient, DevTokenStatus
+
+PKCE_CHALLENGE_PATTERN = r"^[A-Za-z0-9_-]{43}$"
+PKCE_VERIFIER_PATTERN = r"^[A-Za-z0-9._~-]{43,128}$"
+
+
+class DevTokenSerializer(serializers.Serializer):
+ id = serializers.UUIDField(help_text="Token id, used to revoke it.")
+ client = serializers.ChoiceField(choices=DevTokenClient.choices, help_text="Where the token was minted.")
+ cap_usd = serializers.DecimalField(max_digits=10, decimal_places=2, help_text="Most the token can spend, in USD.")
+ created_at = serializers.DateTimeField(help_text="When the token was minted.")
+ expires_at = serializers.DateTimeField(allow_null=True, help_text="When the gateway stops accepting the token.")
+ revoked_at = serializers.DateTimeField(allow_null=True, help_text="When the token was revoked, if it was.")
+ status = serializers.ChoiceField(
+ choices=DevTokenStatus.choices, help_text="Whether the token still works: live, revoked or expired."
+ )
+
+
+class DevTokenUsageSerializer(serializers.Serializer):
+ daily_cap_usd = serializers.DecimalField(
+ max_digits=10, decimal_places=2, help_text="Most token caps one user can mint in 24 hours, in USD."
+ )
+ used_usd = serializers.DecimalField(
+ max_digits=10, decimal_places=2, help_text="Token caps the user minted in the last 24 hours, in USD."
+ )
+ token_cap_usd = serializers.DecimalField(
+ max_digits=10, decimal_places=2, help_text="Cap of each new token, in USD."
+ )
+ ttl_seconds = serializers.IntegerField(help_text="Lifetime of each new token, in seconds.")
+ tokens = DevTokenSerializer(
+ many=True,
+ help_text="Tokens the user minted in the last 24 hours, newest first, including revoked and expired ones.",
+ )
+
+
+class MintedDevTokenSerializer(serializers.Serializer):
+ id = serializers.UUIDField(help_text="Token id, used to revoke it.")
+ token = serializers.CharField(help_text="The phe_ bearer. Shown once; only its hash is stored.")
+ cap_usd = serializers.DecimalField(max_digits=10, decimal_places=2, help_text="Most the token can spend, in USD.")
+ expires_at = serializers.DateTimeField(help_text="When the gateway stops accepting the token.")
+ gateway_url = serializers.CharField(help_text="Base URL of the AI gateway the token is valid on.")
+ product = serializers.CharField(help_text="Product the token's spend is attributed to.")
+
+
+class DevTokenRevokeSerializer(serializers.Serializer):
+ id = serializers.UUIDField(
+ required=False, allow_null=True, help_text="Token to revoke. Omit to revoke all of the user's live tokens."
+ )
+
+
+class DevTokenRevokedSerializer(serializers.Serializer):
+ revoked_count = serializers.IntegerField(help_text="How many tokens the gateway revoked.")
+
+
+class CliAuthorizeSerializer(serializers.Serializer):
+ code_challenge = serializers.RegexField(
+ PKCE_CHALLENGE_PATTERN, help_text="base64url SHA-256 of the CLI's PKCE code_verifier, without padding."
+ )
+
+
+class CliAuthorizedSerializer(serializers.Serializer):
+ code = serializers.CharField(help_text="One-time code for the CLI to exchange within two minutes.")
+
+
+class CliExchangeSerializer(serializers.Serializer):
+ code = serializers.CharField(max_length=128, help_text="Code from the authorize step.")
+ code_verifier = serializers.RegexField(
+ PKCE_VERIFIER_PATTERN, help_text="PKCE code_verifier whose S256 challenge was authorized."
+ )
+
+
+class DevTokenRefusalSerializer(serializers.Serializer):
+ code = serializers.CharField(help_text="Stable refusal code, for example daily_limit or not_staff.")
+ detail = serializers.CharField(help_text="Human-readable reason.")
diff --git a/products/ai_gateway/backend/presentation/views.py b/products/ai_gateway/backend/presentation/views.py
new file mode 100644
index 000000000000..9c95d8657bf3
--- /dev/null
+++ b/products/ai_gateway/backend/presentation/views.py
@@ -0,0 +1,176 @@
+"""Browser endpoints take the session only, so no long-lived key can mint. The CLI never holds
+a PostHog credential: the browser authorizes a PKCE challenge and hands a one-time code to
+the CLI's loopback listener, which exchanges it for a token.
+"""
+
+from collections.abc import Sequence
+from typing import TYPE_CHECKING, cast
+
+from django.conf import settings
+
+from drf_spectacular.utils import OpenApiResponse
+from rest_framework import permissions, status, viewsets
+from rest_framework.authentication import BaseAuthentication
+from rest_framework.decorators import action
+from rest_framework.request import Request
+from rest_framework.response import Response
+from rest_framework.throttling import BaseThrottle, UserRateThrottle
+
+from posthog.api.mixins import validated_request
+from posthog.auth import SessionAuthentication
+from posthog.helpers.impersonation import is_impersonated
+from posthog.models.user import User
+from posthog.permissions import TimeSensitiveActionPermission
+from posthog.rate_limit import IPThrottle
+
+from ..facade import api
+from ..facade.contracts import DevTokenRefused
+from ..facade.enums import DevTokenClient
+from .serializers import (
+ CliAuthorizedSerializer,
+ CliAuthorizeSerializer,
+ CliExchangeSerializer,
+ DevTokenRefusalSerializer,
+ DevTokenRevokedSerializer,
+ DevTokenRevokeSerializer,
+ DevTokenUsageSerializer,
+ MintedDevTokenSerializer,
+)
+
+if TYPE_CHECKING:
+ from rest_framework.permissions import _SupportsHasPermission
+
+_REFUSAL = OpenApiResponse(response=DevTokenRefusalSerializer)
+
+
+class DevTokenMintThrottle(UserRateThrottle):
+ scope = "ai_gateway_dev_token_mint"
+
+ def get_rate(self) -> str:
+ return f"{int(settings.DEV_GATEWAY_MINTS_PER_HOUR)}/hour"
+
+
+class DevTokenExchangeIPThrottle(IPThrottle):
+ scope = "ai_gateway_dev_token_exchange_ip"
+ rate = "30/minute"
+
+
+def _refusal(refused: DevTokenRefused) -> Response:
+ return Response(
+ DevTokenRefusalSerializer({"code": refused.code, "detail": refused.detail}).data, status=refused.http_status
+ )
+
+
+class DevGatewayTokenViewSet(viewsets.ViewSet):
+ authentication_classes = [SessionAuthentication]
+ permission_classes = [permissions.IsAuthenticated, TimeSensitiveActionPermission]
+ time_sensitive_exclude_actions = ["revoke", "usage"]
+ scope_object = "INTERNAL"
+
+ def get_throttles(self) -> list[BaseThrottle]:
+ if self.action in ("create", "authorize_cli"):
+ return [DevTokenMintThrottle()]
+ if self.action == "exchange_cli":
+ return [DevTokenExchangeIPThrottle()]
+ return super().get_throttles()
+
+ def get_authenticators(self) -> list[BaseAuthentication]:
+ # The exchange is authenticated by the PKCE verifier, not a session; DRF resolves
+ # authenticators before `self.action` is set, so match on the path.
+ if self.request is not None and self.request.path.rstrip("/").endswith("/cli_exchange"):
+ return []
+ return super().get_authenticators()
+
+ def get_permissions(self) -> Sequence["_SupportsHasPermission"]:
+ if self.action == "exchange_cli":
+ return [permissions.AllowAny()]
+ return super().get_permissions()
+
+ def _caller_refusal(self, request: Request) -> DevTokenRefused | None:
+ if is_impersonated(request):
+ return DevTokenRefused("impersonated", "Developer mode isn't available while impersonating a user.", 403)
+ return api.eligibility_refusal(cast(User, request.user))
+
+ @validated_request(
+ responses={200: OpenApiResponse(response=DevTokenUsageSerializer), 403: _REFUSAL, 503: _REFUSAL},
+ summary="List your live developer mode tokens and today's usage",
+ )
+ @action(detail=False, methods=["get"], url_path="usage")
+ def usage(self, request: Request) -> Response:
+ refused = self._caller_refusal(request)
+ if refused is not None:
+ return _refusal(refused)
+ return Response(DevTokenUsageSerializer(api.dev_token_usage(cast(User, request.user))).data)
+
+ @validated_request(
+ responses={
+ 201: OpenApiResponse(response=MintedDevTokenSerializer),
+ 403: _REFUSAL,
+ 429: _REFUSAL,
+ 503: _REFUSAL,
+ },
+ summary="Create a developer mode token attributed to you",
+ )
+ def create(self, request: Request) -> Response:
+ refused = self._caller_refusal(request)
+ if refused is not None:
+ return _refusal(refused)
+ try:
+ minted = api.mint_dev_token(cast(User, request.user), DevTokenClient.WEB)
+ except DevTokenRefused as e:
+ return _refusal(e)
+ return Response(MintedDevTokenSerializer(minted).data, status=status.HTTP_201_CREATED)
+
+ @validated_request(
+ request_serializer=DevTokenRevokeSerializer,
+ responses={
+ 200: OpenApiResponse(response=DevTokenRevokedSerializer),
+ 403: _REFUSAL,
+ 404: _REFUSAL,
+ 503: _REFUSAL,
+ },
+ summary="Revoke one or all of your developer mode tokens",
+ )
+ @action(detail=False, methods=["post"], url_path="revoke")
+ def revoke(self, request: Request) -> Response:
+ if is_impersonated(request):
+ return _refusal(
+ DevTokenRefused("impersonated", "Developer mode isn't available while impersonating a user.", 403)
+ )
+ token_id = request.validated_data.get("id")
+ try:
+ count = api.revoke_dev_tokens(cast(User, request.user), str(token_id) if token_id else None)
+ except DevTokenRefused as e:
+ return _refusal(e)
+ return Response(DevTokenRevokedSerializer({"revoked_count": count}).data)
+
+ @validated_request(
+ request_serializer=CliAuthorizeSerializer,
+ responses={200: OpenApiResponse(response=CliAuthorizedSerializer), 403: _REFUSAL, 503: _REFUSAL},
+ summary="Authorize a CLI login by its PKCE challenge",
+ )
+ @action(detail=False, methods=["post"], url_path="cli_authorize")
+ def authorize_cli(self, request: Request) -> Response:
+ refused = self._caller_refusal(request)
+ if refused is not None:
+ return _refusal(refused)
+ code = api.authorize_cli(cast(User, request.user), request.validated_data["code_challenge"])
+ return Response(CliAuthorizedSerializer({"code": code}).data)
+
+ @validated_request(
+ request_serializer=CliExchangeSerializer,
+ responses={
+ 201: OpenApiResponse(response=MintedDevTokenSerializer),
+ 400: _REFUSAL,
+ 403: _REFUSAL,
+ 429: _REFUSAL,
+ },
+ summary="Exchange a CLI authorization code for a developer mode token",
+ )
+ @action(detail=False, methods=["post"], url_path="cli_exchange")
+ def exchange_cli(self, request: Request) -> Response:
+ try:
+ minted = api.exchange_cli_code(request.validated_data["code"], request.validated_data["code_verifier"])
+ except DevTokenRefused as e:
+ return _refusal(e)
+ return Response(MintedDevTokenSerializer(minted).data, status=status.HTTP_201_CREATED)
diff --git a/products/ai_gateway/backend/routes.py b/products/ai_gateway/backend/routes.py
new file mode 100644
index 000000000000..599c4c543fed
--- /dev/null
+++ b/products/ai_gateway/backend/routes.py
@@ -0,0 +1,7 @@
+from posthog.api.routing import RouterRegistry
+
+from .presentation.views import DevGatewayTokenViewSet
+
+
+def register_routes(routers: RouterRegistry) -> None:
+ routers.root.register(r"ai_gateway/dev_tokens", DevGatewayTokenViewSet, "ai_gateway_dev_tokens")
diff --git a/products/ai_gateway/backend/tasks/schedules.py b/products/ai_gateway/backend/tasks/schedules.py
new file mode 100644
index 000000000000..e909ecd08881
--- /dev/null
+++ b/products/ai_gateway/backend/tasks/schedules.py
@@ -0,0 +1,2 @@
+"""Celery beat schedules for ai_gateway."""
+# Define periodic task schedules here
diff --git a/products/ai_gateway/backend/tasks/tasks.py b/products/ai_gateway/backend/tasks/tasks.py
new file mode 100644
index 000000000000..a17a69e8e458
--- /dev/null
+++ b/products/ai_gateway/backend/tasks/tasks.py
@@ -0,0 +1,8 @@
+"""
+Celery tasks for ai_gateway.
+
+Async entrypoints that call the facade (facade/api.py).
+Keep task functions thin - only call facade methods.
+"""
+# from celery import shared_task
+# from ..facade import api
diff --git a/products/ai_gateway/backend/tests/__init__.py b/products/ai_gateway/backend/tests/__init__.py
new file mode 100644
index 000000000000..e69de29bb2d1
diff --git a/products/ai_gateway/backend/tests/conftest.py b/products/ai_gateway/backend/tests/conftest.py
new file mode 100644
index 000000000000..ae3f00200968
--- /dev/null
+++ b/products/ai_gateway/backend/tests/conftest.py
@@ -0,0 +1,13 @@
+import pytest
+
+from posthog.models.scoping import team_scope
+
+
[email protected](autouse=True)
+def _set_team_scope(request):
+ if request.node.get_closest_marker("django_db") is None:
+ yield
+ return
+ team = request.getfixturevalue("team")
+ with team_scope(team.id):
+ yield
diff --git a/products/ai_gateway/backend/tests/test_dev_tokens.py b/products/ai_gateway/backend/tests/test_dev_tokens.py
new file mode 100644
index 000000000000..e8da02677db1
--- /dev/null
+++ b/products/ai_gateway/backend/tests/test_dev_tokens.py
@@ -0,0 +1,324 @@
+import time
+import base64
+import hashlib
+from datetime import timedelta
+from decimal import Decimal
+
+from posthog.test.base import APIBaseTest
+from unittest.mock import MagicMock, patch
+
+from django.conf import settings
+from django.db import connection
+from django.test import (
+ Client as DjangoClient,
+ override_settings,
+)
+from django.test.utils import CaptureQueriesContext
+from django.urls import reverse
+from django.utils import timezone
+
+import requests
+from parameterized import parameterized
+from urllib3 import HTTPConnectionPool
+from urllib3.exceptions import MaxRetryError, NewConnectionError, ProtocolError
+
+from posthog.models.activity_logging.activity_log import ActivityLog
+from posthog.models.user import User
+
+from products.ai_gateway.backend.models import DevGatewayToken
+
+_POST = "products.ai_gateway.backend.logic.requests.post"
+_VIEW_IMPERSONATED = "products.ai_gateway.backend.presentation.views.is_impersonated"
+URL = "/api/ai_gateway/dev_tokens/"
+VERIFIER = "v" * 43
+CHALLENGE = base64.urlsafe_b64encode(hashlib.sha256(VERIFIER.encode()).digest()).rstrip(b"=").decode()
+
+
+def _response(status_code: int, body: dict | None = None) -> MagicMock:
+ response = MagicMock(status_code=status_code)
+ response.json.return_value = body or {}
+ return response
+
+
+def _minted(token: str = "phe_new") -> MagicMock:
+ return _response(201, {"token": token, "expires_at": "2099-01-01T00:00:00Z", "cap_usd": "25"})
+
+
+class TestDevTokens(APIBaseTest):
+ def setUp(self) -> None:
+ super().setUp()
+ override = override_settings(
+ DEV_GATEWAY_URL="https://ai-gateway.test/v1",
+ DEV_GATEWAY_MINT_KEY="phs_dev",
+ DEV_GATEWAY_TEAM_ID=self.team.id,
+ DEV_GATEWAY_TOKEN_CAP_USD="25",
+ DEV_GATEWAY_DAILY_CAP_USD="50",
+ )
+ override.enable()
+ self.addCleanup(override.disable)
+ self.user.is_staff = True
+ self.user.save()
+
+ def _rows(self, **filters):
+ return DevGatewayToken.objects.unscoped().filter(**filters)
+
+ def test_mint_pins_the_session_user_and_stores_only_the_hash(self):
+ with patch(_POST, return_value=_minted("phe_secret")) as post:
+ response = self.client.post(URL)
+
+ assert response.status_code == 201, response.json()
+ assert response.json()["token"] == "phe_secret"
+ url, kwargs = post.call_args.args[0], post.call_args.kwargs
+ assert url == "https://ai-gateway.test/v1/tokens"
+ assert kwargs["headers"] == {"Authorization": "Bearer phs_dev"}
+ assert kwargs["json"] == {
+ "cap_usd": "25",
+ "ttl_seconds": 28800,
+ "product": "dev",
+ "user": self.user.distinct_id,
+ }
+ row = self._rows(user=self.user).get()
+ assert row.token_hash == hashlib.sha256(b"phe_secret").hexdigest()
+ assert row.client == "web"
+ entry = ActivityLog.objects.get(scope="DevGatewayToken", item_id=str(row.id))
+ assert (entry.activity, entry.user_id, entry.team_id) == ("minted", self.user.id, self.team.id)
+ assert "phe_secret" not in str(entry.detail)
+
+ @parameterized.expand(
+ [
+ ("not_staff", {"is_staff": False}, "not_staff"),
+ ("outside_email", {"email": "[email protected]"}, "not_staff"),
+ ("lookalike_domain", {"email": "[email protected]"}, "not_staff"),
+ ("no_distinct_id", {"distinct_id": ""}, "not_staff"),
+ ("unverified_email", {"is_email_verified": False}, "not_staff"),
+ ]
+ )
+ def test_ineligible_user_never_reaches_the_gateway(self, _name, fields, code):
+ for field, value in fields.items():
+ setattr(self.user, field, value)
+ self.user.save()
+ with patch(_POST) as post:
+ response = self.client.post(URL)
+ assert response.status_code == 403
+ assert response.json()["code"] == code
+ post.assert_not_called()
+
+ def test_unconfigured_instance_never_reaches_the_gateway(self):
+ with override_settings(DEV_GATEWAY_MINT_KEY=""), patch(_POST) as post:
+ response = self.client.post(URL)
+ assert response.status_code == 503
+ assert response.json()["code"] == "unconfigured"
+ post.assert_not_called()
+
+ @parameterized.expand(
+ [
+ ("usage", "get", "usage/", {}),
+ ("create", "post", "", {}),
+ ("revoke", "post", "revoke/", {}),
+ ("cli_authorize", "post", "cli_authorize/", {"code_challenge": CHALLENGE}),
+ ]
+ )
+ def test_every_session_view_refuses_impersonation(self, _name, method, path, body):
+ # The middleware check reads the session only, so this pins each view's own guard.
+ with patch(_VIEW_IMPERSONATED, return_value=True), patch(_POST) as post:
+ response = getattr(self.client, method)(f"{URL}{path}", body)
+ assert response.status_code == 403
+ assert response.json()["code"] == "impersonated"
+ post.assert_not_called()
+ assert not self._rows(user=self.user).exists()
+
+ @parameterized.expand(
+ [
+ # GET passes the middleware, so the view guard is the only refusal.
+ ("usage", "get", "usage/", {}, "impersonated"),
+ ("create", "post", "", {}, "impersonation_path_blocked"),
+ ("revoke", "post", "revoke/", {}, "impersonation_path_blocked"),
+ (
+ "cli_authorize",
+ "post",
+ "cli_authorize/",
+ {"code_challenge": CHALLENGE},
+ "impersonation_path_blocked",
+ ),
+ ]
+ )
+ @override_settings(ADMIN_AUTH_GOOGLE_OAUTH2_SECRET=None)
+ def test_loginas_session_is_refused(self, _name, method, path, body, code):
+ # loginas refuses staff targets, so the impersonated user is a customer.
+ target = User.objects.create_and_join(self.organization, "[email protected]", "123456")
+ client = DjangoClient()
+ client.force_login(self.user)
+ client.post(
+ reverse("loginas-user-login", kwargs={"user_id": target.id}),
+ data={"read_only": "false", "reason": "Test impersonation"},
+ )
+ with patch(_POST) as post:
+ if method == "get":
+ response = client.get(f"{URL}{path}")
+ else:
+ response = client.post(f"{URL}{path}", body, content_type="application/json")
+ assert response.status_code == 403
+ assert response.json()["code"] == code
+ post.assert_not_called()
+ assert not self._rows(user=target).exists()
+
+ def test_daily_cap_is_reserved_before_the_gateway_call(self):
+ with patch(_POST, side_effect=[_minted(f"phe_{i}") for i in range(2)]):
+ for _ in range(2):
+ assert self.client.post(URL).status_code == 201
+
+ with patch(_POST) as post:
+ response = self.client.post(URL)
+ assert response.status_code == 429
+ assert response.json()["code"] == "daily_limit"
+ assert "$50.00 limit" in response.json()["detail"]
+ post.assert_not_called()
+
+ usage = self.client.get(f"{URL}usage/").json()
+ assert Decimal(usage["used_usd"]) == Decimal("50")
+ assert [t["status"] for t in usage["tokens"]] == ["live", "live"]
+
+ def test_reserve_locks_the_user_row_before_summing_the_window(self):
+ with patch(_POST, return_value=_minted()), CaptureQueriesContext(connection) as queries:
+ assert self.client.post(URL).status_code == 201
+ sqls = [q["sql"] for q in queries.captured_queries]
+ lock = next(
+ i for i, sql in enumerate(sqls) if 'FROM "posthog_user"' in sql and sql.rstrip().endswith("FOR UPDATE")
+ )
+ window_sum = next(i for i, sql in enumerate(sqls) if "SUM(" in sql and "ai_gateway_devgatewaytoken" in sql)
+ reserve = next(i for i, sql in enumerate(sqls) if sql.startswith('INSERT INTO "ai_gateway_devgatewaytoken"'))
+ assert lock < window_sum < reserve
+
+ @parameterized.expand(
+ [
+ ("refused", {"return_value": _response(500)}, False),
+ ("body_without_expiry", {"return_value": _response(201, {"token": "phe_x"})}, True),
+ (
+ "non_json_body",
+ {"return_value": MagicMock(status_code=201, json=MagicMock(side_effect=ValueError))},
+ True,
+ ),
+ ("read_timeout", {"side_effect": requests.ReadTimeout()}, True),
+ ("connection_reset", {"side_effect": requests.ConnectionError(ProtocolError("reset"))}, True),
+ ("connect_timeout", {"side_effect": requests.ConnectTimeout()}, False),
+ (
+ "connection_refused",
+ {
+ "side_effect": requests.ConnectionError(
+ MaxRetryError(HTTPConnectionPool("gateway"), "/v1/tokens", NewConnectionError(None, "refused"))
+ )
+ },
+ False,
+ ),
+ ("invalid_url", {"side_effect": requests.exceptions.InvalidURL()}, False),
+ ]
+ )
+ def test_failed_mint_keeps_the_reservation_only_when_ambiguous(self, _name, post_kwargs, kept):
+ with patch(_POST, **post_kwargs):
+ response = self.client.post(URL)
+ assert response.status_code == 503
+ assert response.json()["code"] == "mint_failed"
+ assert self._rows(user=self.user, token_hash__isnull=True).exists() == kept
+
+ @parameterized.expand(
+ [
+ ("create", "", {}),
+ ("cli_authorize", "cli_authorize/", {"code_challenge": CHALLENGE}),
+ ]
+ )
+ def test_stale_session_must_reauthenticate_to_mint(self, _name, path, body):
+ session = self.client.session
+ stale = time.time() - settings.SESSION_SENSITIVE_ACTIONS_AGE - 60
+ session[settings.SESSION_COOKIE_CREATED_AT_KEY] = stale
+ session[settings.SESSION_LAST_REAUTH_AT_KEY] = stale
+ session.save()
+ with patch(_POST) as post:
+ response = self.client.post(f"{URL}{path}", body)
+ assert response.status_code == 403
+ assert response.json()["code"] == "sensitive_action_required_reauth"
+ post.assert_not_called()
+ assert self.client.post(f"{URL}revoke/", {}).status_code == 200
+
+ def test_revoke_sends_only_the_callers_hashes(self):
+ other = User.objects.create_and_join(self.organization, "[email protected]", None, is_staff=True)
+ others = DevGatewayToken.objects.unscoped().create(
+ team=self.team,
+ user=other,
+ client="web",
+ cap_usd=Decimal("25"),
+ token_hash="b" * 64,
+ expires_at=timezone.now() + timedelta(hours=1),
+ )
+ with patch(_POST, return_value=_minted("phe_mine")):
+ self.client.post(URL)
+
+ with patch(_POST) as post:
+ foreign = self.client.post(f"{URL}revoke/", {"id": str(others.id)})
+ assert foreign.status_code == 404
+ post.assert_not_called()
+
+ with patch(_POST, return_value=_response(200, {"revoked": True, "revoked_count": 1})) as post:
+ response = self.client.post(f"{URL}revoke/", {})
+
+ assert response.status_code == 200
+ assert response.json() == {"revoked_count": 1}
+ assert post.call_args.args[0] == "https://ai-gateway.test/v1/tokens/revoke"
+ assert post.call_args.kwargs["json"] == {"token_hashes": [hashlib.sha256(b"phe_mine").hexdigest()]}
+ assert self._rows(user=self.user, revoked_at__isnull=False).count() == 1
+ assert self._rows(user=other, revoked_at__isnull=True).count() == 1
+ mine = self._rows(user=self.user).get()
+ assert (
+ ActivityLog.objects.filter(scope="DevGatewayToken", item_id=str(mine.id), activity="revoked").count() == 1
+ )
+ assert self.client.get(f"{URL}usage/").json()["tokens"][0]["status"] == "revoked"
+
+ def test_cli_code_exchanges_once_for_the_authorizing_user(self):
+ code = self.client.post(f"{URL}cli_authorize/", {"code_challenge": CHALLENGE}).json()["code"]
+ self.client.logout()
+
+ with patch(_POST) as post:
+ wrong = self.client.post(f"{URL}cli_exchange/", {"code": code, "code_verifier": "w" * 43})
+ assert wrong.status_code == 400
+ post.assert_not_called()
+
+ code = self._authorize()
+ with patch(_POST, return_value=_minted("phe_cli")) as post:
+ response = self.client.post(f"{URL}cli_exchange/", {"code": code, "code_verifier": VERIFIER})
+ assert response.status_code == 201, response.json()
+ assert post.call_args.kwargs["json"]["user"] == self.user.distinct_id
+ assert self._rows(user=self.user, client="cli").count() == 1
+
+ with patch(_POST) as post:
+ replay = self.client.post(f"{URL}cli_exchange/", {"code": code, "code_verifier": VERIFIER})
+ assert replay.status_code == 400
+ post.assert_not_called()
+
+ def test_cli_exchange_refuses_a_deactivated_user(self):
+ code = self._authorize()
+ self.user.is_active = False
+ self.user.save()
+ with patch(_POST) as post:
+ response = self.client.post(f"{URL}cli_exchange/", {"code": code, "code_verifier": VERIFIER})
+ assert response.status_code == 403
+ assert response.json()["code"] == "not_staff"
+ post.assert_not_called()
+
+ def test_cli_exchange_rechecks_eligibility(self):
+ code = self._authorize()
+ self.user.is_staff = False
+ self.user.save()
+ with patch(_POST) as post:
+ response = self.client.post(f"{URL}cli_exchange/", {"code": code, "code_verifier": VERIFIER})
+ assert response.status_code == 403
+ post.assert_not_called()
+
+ def test_cli_authorize_needs_a_session(self):
+ self.client.logout()
+ response = self.client.post(f"{URL}cli_authorize/", {"code_challenge": CHALLENGE})
+ assert response.status_code in (401, 403)
+
+ def _authorize(self) -> str:
+ self.client.force_login(self.user)
+ code = self.client.post(f"{URL}cli_authorize/", {"code_challenge": CHALLENGE}).json()["code"]
+ self.client.logout()
+ return code
diff --git a/products/ai_gateway/frontend/components/DevModeSetting.test.tsx b/products/ai_gateway/frontend/components/DevModeSetting.test.tsx
new file mode 100644
index 000000000000..3fa40b5941ad
--- /dev/null
+++ b/products/ai_gateway/frontend/components/DevModeSetting.test.tsx
@@ -0,0 +1,78 @@
+import '@testing-library/jest-dom'
+
+import { cleanup, render, screen, waitFor } from '@testing-library/react'
+import { Provider } from 'kea'
+
+import { initKeaTests } from '~/test/init'
+
+import { aiGatewayDevTokensRevokeCreate, aiGatewayDevTokensUsageRetrieve } from '../generated/api'
+import type { DevTokenUsageApi, MintedDevTokenApi } from '../generated/api.schemas'
+import { devModeLogic } from '../logics/devModeLogic'
+import { DevModeSetting } from './DevModeSetting'
+
+jest.mock('../generated/api', () => ({
+ aiGatewayDevTokensUsageRetrieve: jest.fn(),
+ aiGatewayDevTokensCreate: jest.fn(),
+ aiGatewayDevTokensRevokeCreate: jest.fn(),
+ aiGatewayDevTokensCliAuthorizeCreate: jest.fn(),
+}))
+
+const USAGE: DevTokenUsageApi = {
+ daily_cap_usd: '50.00',
+ used_usd: '25.00',
+ token_cap_usd: '25.00',
+ ttl_seconds: 28800,
+ tokens: [],
+}
+
+const MINTED: MintedDevTokenApi = {
+ id: '00000000-0000-0000-0000-000000000001',
+ token: 'phe_fixture_bearer_value',
+ cap_usd: '25.00',
+ expires_at: '2026-10-21T08:00:00Z',
+ gateway_url: 'https://gateway.example.test',
+ product: 'dev',
+}
+
+describe('<DevModeSetting />', () => {
+ beforeEach(() => {
+ initKeaTests()
+ jest.mocked(aiGatewayDevTokensUsageRetrieve).mockResolvedValue(USAGE)
+ devModeLogic.mount()
+ })
+
+ afterEach(() => {
+ cleanup()
+ devModeLogic.unmount()
+ })
+
+ it('hides the minted token from session replay and autocapture', () => {
+ devModeLogic.actions.mintSuccess(MINTED)
+ render(
+ <Provider>
+ <DevModeSetting />
+ </Provider>
+ )
+
+ const token = screen.getByText(MINTED.token)
+ expect(token.closest('.ph-no-capture')).not.toBeNull()
+ })
+
+ it.each([
+ ['succeeds', true],
+ ['fails', false],
+ ])('holds the revoke pending state until the request %s', async (_name, succeeds) => {
+ let settle: () => void = () => {}
+ jest.mocked(aiGatewayDevTokensRevokeCreate).mockReturnValue(
+ new Promise((resolve, reject) => {
+ settle = () => (succeeds ? resolve({ revoked_count: 2 }) : reject({ detail: 'nope' }))
+ })
+ )
+
+ devModeLogic.actions.revokeToken(null)
+ expect(devModeLogic.values.revoking).toEqual({ id: null })
+
+ settle()
+ await waitFor(() => expect(devModeLogic.values.revoking).toBeNull())
+ })
+})
diff --git a/products/ai_gateway/frontend/components/DevModeSetting.tsx b/products/ai_gateway/frontend/components/DevModeSetting.tsx
new file mode 100644
index 000000000000..bd61569ee55c
--- /dev/null
+++ b/products/ai_gateway/frontend/components/DevModeSetting.tsx
@@ -0,0 +1,163 @@
+import { useActions, useValues } from 'kea'
+
+import { LemonBanner, LemonButton, LemonTable, LemonTag } from '@posthog/lemon-ui'
+
+import { CodeSnippet, Language } from 'lib/components/CodeSnippet'
+import { TZLabel } from 'lib/components/TZLabel'
+
+import type { DevTokenApi } from '../generated/api.schemas'
+import { devModeLogic } from '../logics/devModeLogic'
+
+const STATUS_TAG: Record<DevTokenApi['status'], { label: string; type: 'success' | 'muted' | 'danger' }> = {
+ live: { label: 'Live', type: 'success' },
+ expired: { label: 'Expired', type: 'muted' },
+ revoked: { label: 'Revoked', type: 'danger' },
+}
+
+export function DevModeSetting(): JSX.Element {
+ const {
+ usage,
+ usageLoading,
+ minted,
+ mintedLoading,
+ refusal,
+ remainingUsd,
+ canMint,
+ cliRequest,
+ liveTokenCount,
+ revoking,
+ } = useValues(devModeLogic)
+ const { mint, revokeToken, authorizeCli, dismissCliRequest } = useActions(devModeLogic)
+
+ if (refusal && !usage) {
+ return <LemonBanner type="warning">{refusal}</LemonBanner>
+ }
+
+ return (
+ <div className="flex flex-col gap-4">
+ {cliRequest ? (
+ <LemonBanner type="info" data-attr="ai-gateway-dev-mode-cli-request">
+ <div className="flex flex-col gap-2">
+ <span>
+ <code>hogli gateway:token</code> is asking for a token. Approve only if you just ran that
+ command.
+ </span>
+ <div className="flex flex-wrap gap-2">
+ <LemonButton type="primary" size="small" onClick={authorizeCli}>
+ Approve
+ </LemonButton>
+ <LemonButton type="secondary" size="small" onClick={dismissCliRequest}>
+ Deny
+ </LemonButton>
+ </div>
+ </div>
+ </LemonBanner>
+ ) : null}
+
+ <p className="mb-0">
+ Each token can spend up to ${usage?.token_cap_usd ?? '…'} and expires after{' '}
+ {usage ? Math.round(usage.ttl_seconds / 3600) : '…'} hours.
+ </p>
+
+ <div className="flex flex-wrap items-center gap-2">
+ <LemonButton
+ type="primary"
+ onClick={mint}
+ loading={mintedLoading}
+ disabledReason={
+ usageLoading && !usage
+ ? 'Loading'
+ : !canMint
+ ? `You've reached your $${usage?.daily_cap_usd} limit for the last 24 hours`
+ : undefined
+ }
+ data-attr="ai-gateway-dev-mode-mint"
+ >
+ Create token
+ </LemonButton>
+ {usage ? (
+ <span className="text-secondary">
+ <span translate="no">
+ ${remainingUsd?.toFixed(2)} of ${usage.daily_cap_usd}
+ </span>{' '}
+ left in the last 24 hours
+ </span>
+ ) : null}
+ </div>
+
+ {minted ? (
+ <div className="flex flex-col gap-2">
+ <LemonBanner type="success">Copy this token now. It won't be shown again.</LemonBanner>
+ <CodeSnippet className="ph-no-capture" language={Language.Text} wrap thing="token">
+ {minted.token}
+ </CodeSnippet>
+ </div>
+ ) : null}
+
+ <LemonTable<DevTokenApi>
+ dataSource={usage?.tokens ?? []}
+ loading={usageLoading}
+ emptyState="No tokens created in the last 24 hours"
+ columns={[
+ {
+ title: 'Created',
+ key: 'created_at',
+ render: (_, token) => <TZLabel time={token.created_at} />,
+ },
+ {
+ title: 'Expires',
+ key: 'expires_at',
+ render: (_, token) => (token.expires_at ? <TZLabel time={token.expires_at} /> : null),
+ },
+ {
+ title: 'Status',
+ key: 'status',
+ render: (_, token) => (
+ <LemonTag type={STATUS_TAG[token.status].type}>{STATUS_TAG[token.status].label}</LemonTag>
+ ),
+ },
+ {
+ title: 'Source',
+ key: 'client',
+ render: (_, token) => <LemonTag>{token.client === 'cli' ? 'CLI' : 'Web'}</LemonTag>,
+ },
+ {
+ title: 'Spend limit',
+ key: 'cap_usd',
+ render: (_, token) => <span translate="no">${token.cap_usd}</span>,
+ },
+ {
+ key: 'actions',
+ width: 0,
+ render: (_, token) =>
+ token.status === 'live' ? (
+ <LemonButton
+ status="danger"
+ size="small"
+ onClick={() => revokeToken(token.id)}
+ loading={revoking?.id === token.id}
+ disabledReason={revoking ? 'Revoking' : undefined}
+ >
+ Revoke
+ </LemonButton>
+ ) : null,
+ },
+ ]}
+ />
+ {liveTokenCount > 1 ? (
+ <div>
+ <LemonButton
+ status="danger"
+ type="secondary"
+ size="small"
+ onClick={() => revokeToken(null)}
+ loading={revoking?.id === null}
+ disabledReason={revoking ? 'Revoking' : undefined}
+ >
+ Revoke all
+ </LemonButton>
+ </div>
+ ) : null}
+ </div>
+ )
+}
diff --git a/products/ai_gateway/frontend/generated/api.schemas.ts b/products/ai_gateway/frontend/generated/api.schemas.ts
new file mode 100644
index 000000000000..28bce3a41f9c
--- /dev/null
+++ b/products/ai_gateway/frontend/generated/api.schemas.ts
@@ -0,0 +1,151 @@
+/**
+ * Auto-generated from the Django backend OpenAPI schema.
+ * To modify these types, update the Django serializers or views, then run:
+ * hogli build:openapi
+ * Questions or issues? #team-devex on Slack
+ *
+ * PostHog API - generated
+ * OpenAPI spec version: 1.0.0
+ */
+export interface MintedDevTokenApi {
+ /** Token id, used to revoke it. */
+ id: string
+ /** The phe_ bearer. Shown once; only its hash is stored. */
+ token: string
+ /**
+ * Most the token can spend, in USD.
+ * @pattern ^-?\d{0,8}(?:\.\d{0,2})?$
+ */
+ cap_usd: string
+ /** When the gateway stops accepting the token. */
+ expires_at: string
+ /** Base URL of the AI gateway the token is valid on. */
+ gateway_url: string
+ /** Product the token's spend is attributed to. */
+ product: string
+}
+
+export interface DevTokenRefusalApi {
+ /** Stable refusal code, for example daily_limit or not_staff. */
+ code: string
+ /** Human-readable reason. */
+ detail: string
+}
+
+export interface CliAuthorizeApi {
+ /**
+ * base64url SHA-256 of the CLI's PKCE code_verifier, without padding.
+ * @pattern ^[A-Za-z0-9_-]{43}$
+ */
+ code_challenge: string
+}
+
+export interface CliAuthorizedApi {
+ /** One-time code for the CLI to exchange within two minutes. */
+ code: string
+}
+
+export interface CliExchangeApi {
+ /**
+ * Code from the authorize step.
+ * @maxLength 128
+ */
+ code: string
+ /**
+ * PKCE code_verifier whose S256 challenge was authorized.
+ * @pattern ^[A-Za-z0-9._~-]{43,128}$
+ */
+ code_verifier: string
+}
+
+export interface DevTokenRevokeApi {
+ /**
+ * Token to revoke. Omit to revoke all of the user's live tokens.
+ * @nullable
+ */
+ id?: string | null
+}
+
+export interface DevTokenRevokedApi {
+ /** How many tokens the gateway revoked. */
+ revoked_count: number
+}
+
+/**
+ * * `web` - Web
+ * * `cli` - CLI
+ */
+export type DevTokenClientEnumApi = (typeof DevTokenClientEnumApi)[keyof typeof DevTokenClientEnumApi]
+
+export const DevTokenClientEnumApi = {
+ Web: 'web',
+ Cli: 'cli',
+} as const
+
+/**
+ * * `live` - Live
+ * * `revoked` - Revoked
+ * * `expired` - Expired
+ */
+export type DevTokenStatusEnumApi = (typeof DevTokenStatusEnumApi)[keyof typeof DevTokenStatusEnumApi]
+
+export const DevTokenStatusEnumApi = {
+ Live: 'live',
+ Revoked: 'revoked',
+ Expired: 'expired',
+} as const
+
+export interface DevTokenApi {
+ /** Token id, used to revoke it. */
+ id: string
+ /** Where the token was minted.
+ *
+ * * `web` - Web
+ * * `cli` - CLI */
+ client: DevTokenClientEnumApi
+ /**
+ * Most the token can spend, in USD.
+ * @pattern ^-?\d{0,8}(?:\.\d{0,2})?$
+ */
+ cap_usd: string
+ /** When the token was minted. */
+ created_at: string
+ /**
+ * When the gateway stops accepting the token.
+ * @nullable
+ */
+ expires_at: string | null
+ /**
+ * When the token was revoked, if it was.
+ * @nullable
+ */
+ revoked_at: string | null
+ /** Whether the token still works: live, revoked or expired.
+ *
+ * * `live` - Live
+ * * `revoked` - Revoked
+ * * `expired` - Expired */
+ status: DevTokenStatusEnumApi
+}
+
+export interface DevTokenUsageApi {
+ /**
+ * Most token caps one user can mint in 24 hours, in USD.
+ * @pattern ^-?\d{0,8}(?:\.\d{0,2})?$
+ */
+ daily_cap_usd: string
+ /**
+ * Token caps the user minted in the last 24 hours, in USD.
+ * @pattern ^-?\d{0,8}(?:\.\d{0,2})?$
+ */
+ used_usd: string
+ /**
+ * Cap of each new token, in USD.
+ * @pattern ^-?\d{0,8}(?:\.\d{0,2})?$
+ */
+ token_cap_usd: string
+ /** Lifetime of each new token, in seconds. */
+ ttl_seconds: number
+ /** Tokens the user minted in the last 24 hours, newest first, including revoked and expired ones. */
+ tokens: DevTokenApi[]
+}
diff --git a/products/ai_gateway/frontend/generated/api.ts b/products/ai_gateway/frontend/generated/api.ts
new file mode 100644
index 000000000000..730e8a8893a3
--- /dev/null
+++ b/products/ai_gateway/frontend/generated/api.ts
@@ -0,0 +1,104 @@
+import { apiMutator } from '../../../../frontend/src/lib/api-orval-mutator'
+/**
+ * Auto-generated from the Django backend OpenAPI schema.
+ * To modify these types, update the Django serializers or views, then run:
+ * hogli build:openapi
+ * Questions or issues? #team-devex on Slack
+ *
+ * PostHog API - generated
+ * OpenAPI spec version: 1.0.0
+ */
+import type {
+ CliAuthorizeApi,
+ CliAuthorizedApi,
+ CliExchangeApi,
+ DevTokenRevokeApi,
+ DevTokenRevokedApi,
+ DevTokenUsageApi,
+ MintedDevTokenApi,
+} from './api.schemas'
+
+export const getAiGatewayDevTokensCreateUrl = () => {
+ return `/api/ai_gateway/dev_tokens/`
+}
+
+/**
+ * @summary Create a developer mode token attributed to you
+ */
+export const aiGatewayDevTokensCreate = async (options?: RequestInit): Promise<MintedDevTokenApi> => {
+ return apiMutator<MintedDevTokenApi>(getAiGatewayDevTokensCreateUrl(), {
+ ...options,
+ method: 'POST',
+ })
+}
+
+export const getAiGatewayDevTokensCliAuthorizeCreateUrl = () => {
+ return `/api/ai_gateway/dev_tokens/cli_authorize/`
+}
+
+/**
+ * @summary Authorize a CLI login by its PKCE challenge
+ */
+export const aiGatewayDevTokensCliAuthorizeCreate = async (
+ cliAuthorizeApi: CliAuthorizeApi,
+ options?: RequestInit
+): Promise<CliAuthorizedApi> => {
+ return apiMutator<CliAuthorizedApi>(getAiGatewayDevTokensCliAuthorizeCreateUrl(), {
+ ...options,
+ method: 'POST',
+ headers: { 'Content-Type': 'application/json', ...options?.headers },
+ body: JSON.stringify(cliAuthorizeApi),
+ })
+}
+
+export const getAiGatewayDevTokensCliExchangeCreateUrl = () => {
+ return `/api/ai_gateway/dev_tokens/cli_exchange/`
+}
+
+/**
+ * @summary Exchange a CLI authorization code for a developer mode token
+ */
+export const aiGatewayDevTokensCliExchangeCreate = async (
+ cliExchangeApi: CliExchangeApi,
+ options?: RequestInit
+): Promise<MintedDevTokenApi> => {
+ return apiMutator<MintedDevTokenApi>(getAiGatewayDevTokensCliExchangeCreateUrl(), {
+ ...options,
+ method: 'POST',
+ headers: { 'Content-Type': 'application/json', ...options?.headers },
+ body: JSON.stringify(cliExchangeApi),
+ })
+}
+
+export const getAiGatewayDevTokensRevokeCreateUrl = () => {
+ return `/api/ai_gateway/dev_tokens/revoke/`
+}
+
+/**
+ * @summary Revoke one or all of your developer mode tokens
+ */
+export const aiGatewayDevTokensRevokeCreate = async (
+ devTokenRevokeApi?: DevTokenRevokeApi,
+ options?: RequestInit
+): Promise<DevTokenRevokedApi> => {
+ return apiMutator<DevTokenRevokedApi>(getAiGatewayDevTokensRevokeCreateUrl(), {
+ ...options,
+ method: 'POST',
+ headers: { 'Content-Type': 'application/json', ...options?.headers },
+ body: JSON.stringify(devTokenRevokeApi),
+ })
+}
+
+export const getAiGatewayDevTokensUsageRetrieveUrl = () => {
+ return `/api/ai_gateway/dev_tokens/usage/`
+}
+
+/**
+ * @summary List your live developer mode tokens and today's usage
+ */
+export const aiGatewayDevTokensUsageRetrieve = async (options?: RequestInit): Promise<DevTokenUsageApi> => {
+ return apiMutator<DevTokenUsageApi>(getAiGatewayDevTokensUsageRetrieveUrl(), {
+ ...options,
+ method: 'GET',
+ })
+}
diff --git a/products/ai_gateway/frontend/generated/api.zod.ts b/products/ai_gateway/frontend/generated/api.zod.ts
new file mode 100644
index 000000000000..e630cee4b5d8
--- /dev/null
+++ b/products/ai_gateway/frontend/generated/api.zod.ts
@@ -0,0 +1,44 @@
+/**
+ * Auto-generated Zod validation schemas from the Django backend OpenAPI schema.
+ * To modify these schemas, update the Django serializers or views, then run:
+ * hogli build:openapi
+ * Questions or issues? #team-devex on Slack
+ *
+ * PostHog API - generated
+ * OpenAPI spec version: 1.0.0
+ */
+import * as zod from 'zod'
+
+/**
+ * @summary Authorize a CLI login by its PKCE challenge
+ */
+export const aiGatewayDevTokensCliAuthorizeCreateBodyCodeChallengeRegExp = new RegExp('^[A-Za-z0-9_-]{43}$')
+
+export const AiGatewayDevTokensCliAuthorizeCreateBody = /* @__PURE__ */ zod.object({
+ code_challenge: zod
+ .string()
+ .regex(aiGatewayDevTokensCliAuthorizeCreateBodyCodeChallengeRegExp)
+ .describe("base64url SHA-256 of the CLI's PKCE code_verifier, without padding."),
+})
+
+/**
+ * @summary Exchange a CLI authorization code for a developer mode token
+ */
+export const aiGatewayDevTokensCliExchangeCreateBodyCodeMax = 128
+
+export const aiGatewayDevTokensCliExchangeCreateBodyCodeVerifierRegExp = new RegExp('^[A-Za-z0-9._~-]{43,128}$')
+
+export const AiGatewayDevTokensCliExchangeCreateBody = /* @__PURE__ */ zod.object({
+ code: zod.string().max(aiGatewayDevTokensCliExchangeCreateBodyCodeMax).describe('Code from the authorize step.'),
+ code_verifier: zod
+ .string()
+ .regex(aiGatewayDevTokensCliExchangeCreateBodyCodeVerifierRegExp)
+ .describe('PKCE code_verifier whose S256 challenge was authorized.'),
+})
+
+/**
+ * @summary Revoke one or all of your developer mode tokens
+ */
+export const AiGatewayDevTokensRevokeCreateBody = /* @__PURE__ */ zod.object({
+ id: zod.uuid().nullish().describe("Token to revoke. Omit to revoke all of the user's live tokens."),
+})
diff --git a/products/ai_gateway/frontend/logics/devModeLogic.test.ts b/products/ai_gateway/frontend/logics/devModeLogic.test.ts
new file mode 100644
index 000000000000..6591bf50b437
--- /dev/null
+++ b/products/ai_gateway/frontend/logics/devModeLogic.test.ts
@@ -0,0 +1,26 @@
+import { cliCallbackUrl, parseCliRequest } from './devModeLogic'
+
+const CHALLENGE = 'a'.repeat(43)
+
+describe('devModeLogic CLI handoff', () => {
+ test.each([
+ ['valid', { cli_challenge: CHALLENGE, cli_port: '53682', cli_state: 's1' }, true],
+ ['short challenge', { cli_challenge: 'a'.repeat(42), cli_port: '53682', cli_state: 's1' }, false],
+ ['challenge with a slash', { cli_challenge: 'a'.repeat(42) + '/', cli_port: '53682', cli_state: 's1' }, false],
+ ['privileged port', { cli_challenge: CHALLENGE, cli_port: '80', cli_state: 's1' }, false],
+ ['port out of range', { cli_challenge: CHALLENGE, cli_port: '70000', cli_state: 's1' }, false],
+ ['port with a host', { cli_challenge: CHALLENGE, cli_port: '[email protected]', cli_state: 's1' }, false],
+ ['missing state', { cli_challenge: CHALLENGE, cli_port: '53682' }, false],
+ ])('parses a %s request', (_name, params, valid) => {
+ expect(parseCliRequest(params) !== null).toBe(valid)
+ })
+
+ test('the callback only ever targets the loopback address', () => {
+ const request = parseCliRequest({ cli_challenge: CHALLENGE, cli_port: '53682', cli_state: 'x&y' })!
+ const url = new URL(cliCallbackUrl(request, 'c0de'))
+ expect(url.origin).toBe('http://127.0.0.1:53682')
+ expect(url.pathname).toBe('/callback')
+ expect(url.searchParams.get('code')).toBe('c0de')
+ expect(url.searchParams.get('state')).toBe('x&y')
+ })
+})
diff --git a/products/ai_gateway/frontend/logics/devModeLogic.ts b/products/ai_gateway/frontend/logics/devModeLogic.ts
new file mode 100644
index 000000000000..7bf4d2af0682
--- /dev/null
+++ b/products/ai_gateway/frontend/logics/devModeLogic.ts
@@ -0,0 +1,250 @@
+import { MakeLogicType, actions, afterMount, kea, listeners, path, reducers, selectors } from 'kea'
+import { loaders } from 'kea-loaders'
+import { router, urlToAction } from 'kea-router'
+
+import { lemonToast } from '@posthog/lemon-ui'
+
+import {
+ aiGatewayDevTokensCliAuthorizeCreate,
+ aiGatewayDevTokensCreate,
+ aiGatewayDevTokensRevokeCreate,
+ aiGatewayDevTokensUsageRetrieve,
+} from '../generated/api'
+import type { DevTokenUsageApi, MintedDevTokenApi } from '../generated/api.schemas'
+
+const PKCE_CHALLENGE = /^[A-Za-z0-9_-]{43}$/
+
+export interface CliRequest {
+ challenge: string
+ port: number
+ state: string
+}
+
+export function parseCliRequest(searchParams: Record<string, any>): CliRequest | null {
+ const challenge = String(searchParams.cli_challenge ?? '')
+ const port = Number(searchParams.cli_port)
+ const state = String(searchParams.cli_state ?? '')
+ if (!PKCE_CHALLENGE.test(challenge) || !Number.isInteger(port) || port < 1024 || port > 65535 || !state) {
+ return null
+ }
+ return { challenge, port, state }
+}
+
+export function cliCallbackUrl(request: CliRequest, code: string): string {
+ const params = new URLSearchParams({ code, state: request.state })
+ return `http://127.0.0.1:${request.port}/callback?${params.toString()}`
+}
+
+function errorDetail(error: any): string {
+ return error?.detail || error?.message || "Couldn't reach PostHog. Refresh the page and try again."
+}
+
+// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
+export interface devModeLogicValues {
+ canMint: boolean
+ cliRequest: CliRequest | null
+ liveTokenCount: number
+ minted: MintedDevTokenApi | null
+ mintedLoading: boolean
+ refusal: string | null
+ remainingUsd: number | null
+ revoking: { id: string | null } | null
+ usage: DevTokenUsageApi | null
+ usageLoading: boolean
+}
+
+// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
+export interface devModeLogicActions {
+ authorizeCli: () => {
+ value: true
+ }
+ clearMinted: () => any
+ clearMintedFailure: (
+ error: string,
+ errorObject?: any
+ ) => {
+ error: string
+ errorObject?: any
+ }
+ clearMintedSuccess: (
+ minted: null,
+ payload?: any
+ ) => {
+ minted: null
+ payload?: any
+ }
+ dismissCliRequest: () => {
+ value: true
+ }
+ loadUsage: () => any
+ loadUsageFailure: (
+ error: string,
+ errorObject?: any
+ ) => {
+ error: string
+ errorObject?: any
+ }
+ loadUsageSuccess: (
+ usage: DevTokenUsageApi,
+ payload?: any
+ ) => {
+ usage: DevTokenUsageApi
+ payload?: any
+ }
+ mint: () => any
+ mintFailure: (
+ error: string,
+ errorObject?: any
+ ) => {
+ error: string
+ errorObject?: any
+ }
+ mintSuccess: (
+ minted: MintedDevTokenApi | null,
+ payload?: any
+ ) => {
+ minted: MintedDevTokenApi | null
+ payload?: any
+ }
+ revokeToken: (id: string | null) => {
+ id: string | null
+ }
+ revokeTokenFinished: () => {
+ value: true
+ }
+ setCliRequest: (cliRequest: CliRequest | null) => {
+ cliRequest: CliRequest | null
+ }
+}
+
+// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
+export interface devModeLogicMeta {
+ __keaTypeGenInternalSelectorTypes: {
+ remainingUsd: (usage: DevTokenUsageApi | null) => number | null
+ liveTokenCount: (usage: DevTokenUsageApi | null) => number
+ canMint: (usage: DevTokenUsageApi | null, remainingUsd: number | null) => boolean
+ }
+}
+
+export type devModeLogicType = MakeLogicType<
+ devModeLogicValues,
+ devModeLogicActions,
+ Record<string, any>,
+ devModeLogicMeta
+>
+
+export const devModeLogic = kea<devModeLogicType>([
+ path(['products', 'ai_gateway', 'frontend', 'logics', 'devModeLogic']),
+ actions({
+ setCliRequest: (cliRequest: CliRequest | null) => ({ cliRequest }),
+ authorizeCli: true,
+ dismissCliRequest: true,
+ revokeToken: (id: string | null) => ({ id }),
+ revokeTokenFinished: true,
+ }),
+ reducers({
+ cliRequest: [
+ null as CliRequest | null,
+ { setCliRequest: (_, { cliRequest }) => cliRequest, dismissCliRequest: () => null },
+ ],
+ revoking: [
+ null as { id: string | null } | null,
+ { revokeToken: (_, { id }) => ({ id }), revokeTokenFinished: () => null },
+ ],
+ refusal: [
+ null as string | null,
+ {
+ loadUsageFailure: (_, { error }) => error ?? null,
+ loadUsageSuccess: () => null,
+ },
+ ],
+ }),
+ loaders(({ values }) => ({
+ usage: [
+ null as DevTokenUsageApi | null,
+ {
+ loadUsage: async () => {
+ try {
+ return await aiGatewayDevTokensUsageRetrieve()
+ } catch (error: any) {
+ throw new Error(errorDetail(error))
+ }
+ },
+ },
+ ],
+ minted: [
+ null as MintedDevTokenApi | null,
+ {
+ mint: async () => {
+ try {
+ return await aiGatewayDevTokensCreate()
+ } catch (error: any) {
+ lemonToast.error(errorDetail(error))
+ return values.minted
+ }
+ },
+ clearMinted: () => null,
+ },
+ ],
+ })),
+ selectors({
+ remainingUsd: [
+ (s) => [s.usage],
+ (usage: DevTokenUsageApi | null): number | null =>
+ usage ? Math.max(0, Number(usage.daily_cap_usd) - Number(usage.used_usd)) : null,
+ ],
+ liveTokenCount: [
+ (s) => [s.usage],
+ (usage: DevTokenUsageApi | null): number =>
+ usage?.tokens.filter((token) => token.status === 'live').length ?? 0,
+ ],
+ canMint: [
+ (s) => [s.usage, s.remainingUsd],
+ (usage: DevTokenUsageApi | null, remainingUsd: number | null): boolean =>
+ !!usage && remainingUsd !== null && remainingUsd >= Number(usage.token_cap_usd),
+ ],
+ }),
+ listeners(({ actions, values }) => ({
+ mintSuccess: () => actions.loadUsage(),
+ revokeToken: async ({ id }) => {
+ try {
+ const { revoked_count } = await aiGatewayDevTokensRevokeCreate({ id })
+ lemonToast.success(revoked_count === 1 ? 'Token revoked' : `${revoked_count} tokens revoked`)
+ if (values.minted && (id === null || values.minted.id === id)) {
+ actions.clearMinted()
+ }
+ } catch (error: any) {
+ lemonToast.error(errorDetail(error))
+ }
+ actions.revokeTokenFinished()
+ actions.loadUsage()
+ },
+ authorizeCli: async () => {
+ const request = values.cliRequest
+ if (!request) {
+ return
+ }
+ try {
+ const { code } = await aiGatewayDevTokensCliAuthorizeCreate({ code_challenge: request.challenge })
+ actions.dismissCliRequest()
+ window.location.href = cliCallbackUrl(request, code)
+ } catch (error: any) {
+ lemonToast.error(errorDetail(error))
+ }
+ },
+ dismissCliRequest: () => {
+ const { cli_challenge, cli_port, cli_state, ...rest } = router.values.searchParams
+ router.actions.replace(router.values.location.pathname, rest, router.values.hashParams)
+ },
+ })),
+ urlToAction(({ actions }) => ({
+ '*': (_, searchParams) => {
+ if (searchParams.cli_challenge) {
+ actions.setCliRequest(parseCliRequest(searchParams))
+ }
+ },
+ })),
+ afterMount(({ actions }) => {
+ actions.loadUsage()
+ }),
+])
diff --git a/products/ai_gateway/manifest.tsx b/products/ai_gateway/manifest.tsx
new file mode 100644
index 000000000000..b1b59e0d8bd6
--- /dev/null
+++ b/products/ai_gateway/manifest.tsx
@@ -0,0 +1,23 @@
+/**
+ * Product manifest for ai_gateway.
+ *
+ * Defines scenes, routes, URLs, and navigation for this product.
+ */
+import { ProductManifest } from '../../frontend/src/types'
+
+export const manifest: ProductManifest = {
+ name: 'AiGateway',
+ scenes: {
+ // Define scenes here
+ },
+ routes: {
+ // Define routes here
+ },
+ redirects: {},
+ urls: {
+ // Define URL helpers here
+ },
+ fileSystemTypes: {},
+ treeItemsNew: [],
+ treeItemsProducts: [],
+}
diff --git a/products/ai_gateway/package.json b/products/ai_gateway/package.json
new file mode 100644
index 000000000000..f8667ef1e0e7
--- /dev/null
+++ b/products/ai_gateway/package.json
@@ -0,0 +1,20 @@
+{
+ "name": "@posthog/products-ai-gateway",
+ "scripts": {
+ "backend:test": "pytest -c ../../pytest.ini --rootdir ../.. backend/tests -v --tb=short",
+ "backend:contract-check": "echo 'Contract files unchanged'"
+ },
+ "dependencies": {
+ "kea": "catalog:",
+ "kea-loaders": "catalog:",
+ "kea-router": "catalog:"
+ },
+ "devDependencies": {
+ "@testing-library/react": "*"
+ },
+ "peerDependencies": {
+ "@posthog/lemon-ui": "*",
+ "@types/react": "*",
+ "react": "*"
+ }
+}
diff --git a/products/ai_gateway/product.yaml b/products/ai_gateway/product.yaml
new file mode 100644
index 000000000000..247943ac0851
--- /dev/null
+++ b/products/ai_gateway/product.yaml
@@ -0,0 +1,3 @@
+name: AI gateway
+owners:
+ - team-agent-infrastructure
diff --git a/products/ai_gateway/tsconfig.json b/products/ai_gateway/tsconfig.json
new file mode 100644
index 000000000000..fe425d6fcba8
--- /dev/null
+++ b/products/ai_gateway/tsconfig.json
@@ -0,0 +1,6 @@
+{
+ "extends": "../../tsconfig.json",
+ "compilerOptions": {
+ "composite": true
+ }
+}
diff --git a/products/ai_gateway/turbo.json b/products/ai_gateway/turbo.json
new file mode 100644
index 000000000000..1ccef71df142
--- /dev/null
+++ b/products/ai_gateway/turbo.json
@@ -0,0 +1,18 @@
+{
+ "extends": ["//"],
+ "tasks": {
+ "backend:contract-check": {
+ "inputs": [
+ "backend/facade/**",
+ "backend/presentation/**",
+ "backend/webhook_consumers.py",
+ "backend/routes.py",
+ "backend/tasks/**",
+ "backend/models.py",
+ "backend/migrations/**"
+ ],
+ "outputs": [],
+ "cache": true
+ }
+ }
+}
diff --git a/products/platform_features/frontend/generated/api.schemas.ts b/products/platform_features/frontend/generated/api.schemas.ts
index 2e22c5803beb..c6b9d2ac6ab4 100644
--- a/products/platform_features/frontend/generated/api.schemas.ts
+++ b/products/platform_features/frontend/generated/api.schemas.ts
@@ -1394,6 +1394,7 @@ export type ActivityLogListParams = {
*
* * `Cohort` - Cohort
* * `CrossProjectDashboard` - CrossProjectDashboard
+ * * `DevGatewayToken` - DevGatewayToken
* * `FeatureFlag` - FeatureFlag
* * `Person` - Person
* * `Group` - Group
@@ -1505,6 +1506,7 @@ export type ActivityLogListScope = (typeof ActivityLogListScope)[keyof typeof Ac
export const ActivityLogListScope = {
Cohort: 'Cohort',
CrossProjectDashboard: 'CrossProjectDashboard',
+ DevGatewayToken: 'DevGatewayToken',
FeatureFlag: 'FeatureFlag',
Person: 'Person',
Group: 'Group',
@@ -1603,6 +1605,7 @@ export const ActivityLogListScope = {
/**
* * `Cohort` - Cohort
* * `CrossProjectDashboard` - CrossProjectDashboard
+ * * `DevGatewayToken` - DevGatewayToken
* * `FeatureFlag` - FeatureFlag
* * `Person` - Person
* * `Group` - Group
@@ -1702,6 +1705,7 @@ export type ActivityLogListScopesItem = (typeof ActivityLogListScopesItem)[keyof
export const ActivityLogListScopesItem = {
Cohort: 'Cohort',
CrossProjectDashboard: 'CrossProjectDashboard',
+ DevGatewayToken: 'DevGatewayToken',
FeatureFlag: 'FeatureFlag',
Person: 'Person',
Group: 'Group',
diff --git a/services/mcp/src/api/generated.ts b/services/mcp/src/api/generated.ts
index 414182e2d24e..cdd9fed274b7 100644
--- a/services/mcp/src/api/generated.ts
+++ b/services/mcp/src/api/generated.ts
@@ -23292,6 +23292,32 @@ export namespace Schemas {
initial_permission_mode?: InitialPermissionModeEnum;
}
+ export interface CliAuthorize {
+ /**
+ * base64url SHA-256 of the CLI's PKCE code_verifier, without padding.
+ * @pattern ^[A-Za-z0-9_-]{43}$
+ */
+ code_challenge: string;
+ }
+
+ export interface CliAuthorized {
+ /** One-time code for the CLI to exchange within two minutes. */
+ code: string;
+ }
+
+ export interface CliExchange {
+ /**
+ * Code from the authorize step.
+ * @maxLength 128
+ */
+ code: string;
+ /**
+ * PKCE code_verifier whose S256 challenge was authorized.
+ * @pattern ^[A-Za-z0-9._~-]{43,128}$
+ */
+ code_verifier: string;
+ }
+
export type ClickhouseEventProperties = { [key: string]: unknown };
/**
@@ -36489,6 +36515,107 @@ export namespace Schemas {
steps: DestinationConnectionTestStep[];
}
+ /**
+ * * `web` - Web
+ * * `cli` - CLI
+ */
+ export type DevTokenClientEnum = typeof DevTokenClientEnum[keyof typeof DevTokenClientEnum];
+
+
+ export const DevTokenClientEnum = {
+ Web: 'web',
+ Cli: 'cli',
+ } as const;
+
+ /**
+ * * `live` - Live
+ * * `revoked` - Revoked
+ * * `expired` - Expired
+ */
+ export type DevTokenStatusEnum = typeof DevTokenStatusEnum[keyof typeof DevTokenStatusEnum];
+
+
+ export const DevTokenStatusEnum = {
+ Live: 'live',
+ Revoked: 'revoked',
+ Expired: 'expired',
+ } as const;
+
+ export interface DevToken {
+ /** Token id, used to revoke it. */
+ id: string;
+ /** Where the token was minted.
+ *
+ * * `web` - Web
+ * * `cli` - CLI */
+ client: DevTokenClientEnum;
+ /**
+ * Most the token can spend, in USD.
+ * @pattern ^-?\d{0,8}(?:\.\d{0,2})?$
+ */
+ cap_usd: string;
+ /** When the token was minted. */
+ created_at: string;
+ /**
+ * When the gateway stops accepting the token.
+ * @nullable
+ */
+ expires_at: string | null;
+ /**
+ * When the token was revoked, if it was.
+ * @nullable
+ */
+ revoked_at: string | null;
+ /** Whether the token still works: live, revoked or expired.
+ *
+ * * `live` - Live
+ * * `revoked` - Revoked
+ * * `expired` - Expired */
+ status: DevTokenStatusEnum;
+ }
+
+ export interface DevTokenRefusal {
+ /** Stable refusal code, for example daily_limit or not_staff. */
+ code: string;
+ /** Human-readable reason. */
+ detail: string;
+ }
+
+ export interface DevTokenRevoke {
+ /**
+ * Token to revoke. Omit to revoke all of the user's live tokens.
+ * @nullable
+ */
+ id?: string | null;
+ }
+
+ export interface DevTokenRevoked {
+ /** How many tokens the gateway revoked. */
+ revoked_count: number;
+ }
+
+ export interface DevTokenUsage {
+ /**
+ * Most token caps one user can mint in 24 hours, in USD.
+ * @pattern ^-?\d{0,8}(?:\.\d{0,2})?$
+ */
+ daily_cap_usd: string;
+ /**
+ * Token caps the user minted in the last 24 hours, in USD.
+ * @pattern ^-?\d{0,8}(?:\.\d{0,2})?$
+ */
+ used_usd: string;
+ /**
+ * Cap of each new token, in USD.
+ * @pattern ^-?\d{0,8}(?:\.\d{0,2})?$
+ */
+ token_cap_usd: string;
+ /** Lifetime of each new token, in seconds. */
+ ttl_seconds: number;
+ /** Tokens the user minted in the last 24 hours, newest first, including revoked and expired ones. */
+ tokens: DevToken[];
+ }
+
/**
* * `Desktop` - Desktop
* * `Mobile` - Mobile
@@ -69584,6 +69711,24 @@ export namespace Schemas {
readonly last_seen_at: string | null;
}
+ export interface MintedDevToken {
+ /** Token id, used to revoke it. */
+ id: string;
+ /** The phe_ bearer. Shown once; only its hash is stored. */
+ token: string;
+ /**
+ * Most the token can spend, in USD.
+ * @pattern ^-?\d{0,8}(?:\.\d{0,2})?$
+ */
+ cap_usd: string;
+ /** When the gateway stops accepting the token. */
+ expires_at: string;
+ /** Base URL of the AI gateway the token is valid on. */
+ gateway_url: string;
+ /** Product the token's spend is attributed to. */
+ product: string;
+ }
+
export interface MintlifyFeedbackSignalExtra {
path: string | null;
helpful: string | null;
@@ -123852,6 +123997,7 @@ export namespace Schemas {
*
* * `Cohort` - Cohort
* * `CrossProjectDashboard` - CrossProjectDashboard
+ * * `DevGatewayToken` - DevGatewayToken
* * `FeatureFlag` - FeatureFlag
* * `Person` - Person
* * `Group` - Group
@@ -123964,6 +124110,7 @@ export namespace Schemas {
export const ActivityLogListScope = {
Cohort: 'Cohort',
CrossProjectDashboard: 'CrossProjectDashboard',
+ DevGatewayToken: 'DevGatewayToken',
FeatureFlag: 'FeatureFlag',
Person: 'Person',
Group: 'Group',
@@ -124062,6 +124209,7 @@ export namespace Schemas {
/**
* * `Cohort` - Cohort
* * `CrossProjectDashboard` - CrossProjectDashboard
+ * * `DevGatewayToken` - DevGatewayToken
* * `FeatureFlag` - FeatureFlag
* * `Person` - Person
* * `Group` - Group
@@ -124162,6 +124310,7 @@ export namespace Schemas {
export const ActivityLogListScopesItem = {
Cohort: 'Cohort',
CrossProjectDashboard: 'CrossProjectDashboard',
+ DevGatewayToken: 'DevGatewayToken',
FeatureFlag: 'FeatureFlag',
Person: 'Person',
Group: 'Group',
diff --git a/tach.toml b/tach.toml
index fb1d84be2d16..e1ebedb94fd8 100644
--- a/tach.toml
+++ b/tach.toml
@@ -1813,6 +1813,20 @@ from = [
"products.account_audits",
]
+[[modules]]
+path = "products.ai_gateway"
+depends_on = ["posthog"]
+layer = "modules"
+
+[[interfaces]]
+expose = [
+ "backend\\.facade.*",
+ "backend\\.presentation\\.views.*",
+]
+from = [
+ "products.ai_gateway",
+]
+
[[modules]]
path = "products.uptime_monitoring"
depends_on = ["posthog"]