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(settings): add a usage page with your agent spend under today-rail-nav (#110225)

Shy Alter24fa7564bfa7
diff --git a/frontend/src/scenes/settings/SettingsMap.tsx b/frontend/src/scenes/settings/SettingsMap.tsx
index d5fd8ecc33f6..cff0c4db50a4 100644
--- a/frontend/src/scenes/settings/SettingsMap.tsx
+++ b/frontend/src/scenes/settings/SettingsMap.tsx
@@ -89,6 +89,7 @@ import {
ActivityLogOrgLevelSettings,
ActivityLogSettings,
} from './environment/ActivityLogSettings'
+import { AgentUsageSettings } from './environment/AgentUsageSettings'
import { AutocaptureSettings, WebVitalsAutocaptureSettings } from './environment/AutocaptureSettings'
import { CorrelationConfig } from './environment/CorrelationConfig'
import { CSPReportingSettings } from './environment/CSPReportingSettings'
@@ -512,6 +513,30 @@ export const SETTINGS_MAP: SettingSection[] = [
},
],
},
+ {
+ level: 'environment',
+ id: 'environment-ai-usage',
+ title: 'Usage',
+ group: 'AI',
+ flag: 'TODAY_RAIL_NAV',
+ keywords: ['plan & usage', 'cost management', 'spend', 'credits', 'billing'],
+ settings: [
+ {
+ id: 'ai-usage-spend',
+ title: (
+ <>
+ Your agent spend
+ <SettingScopeTag scope="user" />
+ </>
+ ),
+ searchTerm: 'Your agent spend',
+ description: 'What your PostHog Desktop and cloud agent runs cost, across all your projects.',
+ component: <AgentUsageSettings />,
+ keywords: ['spend', 'cost', 'usage', 'credits', 'billing', 'models', 'desktop'],
+ hideOn: [Realm.SelfHostedClickHouse, Realm.SelfHostedPostgres],
+ },
+ ],
+ },
{
level: 'environment',
id: 'posthog-mcp',
diff --git a/frontend/src/scenes/settings/environment/AgentUsageSettings.tsx b/frontend/src/scenes/settings/environment/AgentUsageSettings.tsx
new file mode 100644
index 000000000000..13c143f136a3
--- /dev/null
+++ b/frontend/src/scenes/settings/environment/AgentUsageSettings.tsx
@@ -0,0 +1,90 @@
+import { useActions, useValues } from 'kea'
+
+import { LemonBanner, LemonButton, LemonSkeleton, LemonTable } from '@posthog/lemon-ui'
+
+import { humanFriendlyCurrency } from 'lib/utils/numbers'
+import { urls } from 'scenes/urls'
+
+import { AGENT_SPEND_WINDOW_DAYS, agentUsageLogic } from './agentUsageLogic'
+
+function SpendStat({ label, value }: { label: string; value: number }): JSX.Element {
+ return (
+ <div className="flex flex-col gap-1 min-w-32 flex-1">
+ <span className="text-secondary text-xs">{label}</span>
+ <span className="text-xl font-semibold" translate="no">
+ {humanFriendlyCurrency(value)}
+ </span>
+ </div>
+ )
+}
+
+export function AgentUsageSettings(): JSX.Element {
+ const { totals, topModels, spendLoading, spendFailed } = useValues(agentUsageLogic)
+ const { loadSpend } = useActions(agentUsageLogic)
+
+ if (spendFailed) {
+ return (
+ <LemonBanner
+ type="warning"
+ action={{ children: 'Try again', onClick: loadSpend, loading: spendLoading }}
+ className="max-w-200"
+ >
+ Your agent spend did not load.
+ </LemonBanner>
+ )
+ }
+
+ if (!totals) {
+ return (
+ <div className="flex flex-col gap-2 max-w-200">
+ <LemonSkeleton className="h-16" />
+ <LemonSkeleton className="h-24" />
+ </div>
+ )
+ }
+
+ return (
+ <div className="flex flex-col gap-4 max-w-200">
+ <div className="flex flex-wrap gap-4 rounded border p-4 bg-surface-primary">
+ <SpendStat label="Today" value={totals.todayUsd} />
+ <SpendStat label="This month" value={totals.monthUsd} />
+ <SpendStat label={`Last ${AGENT_SPEND_WINDOW_DAYS} days`} value={totals.windowUsd} />
+ </div>
+ {topModels.length > 0 ? (
+ <LemonTable
+ size="small"
+ dataSource={topModels}
+ rowKey={(row) => row.model ?? 'other'}
+ columns={[
+ {
+ title: 'Model',
+ key: 'model',
+ render: (_, row) => <span translate="no">{row.model ?? 'Other models'}</span>,
+ },
+ {
+ title: 'Model calls',
+ key: 'generation_count',
+ align: 'right',
+ render: (_, row) => <span translate="no">{row.generation_count.toLocaleString()}</span>,
+ },
+ {
+ title: 'Spend',
+ key: 'cost_usd',
+ align: 'right',
+ render: (_, row) => <span translate="no">{humanFriendlyCurrency(row.cost_usd)}</span>,
+ },
+ ]}
+ />
+ ) : (
+ <p className="text-secondary mb-0">
+ You have no agent spend in the last {AGENT_SPEND_WINDOW_DAYS} days.
+ </p>
+ )}
+ <div className="flex">
+ <LemonButton type="secondary" size="small" to={urls.organizationBilling()}>
+ Manage billing
+ </LemonButton>
+ </div>
+ </div>
+ )
+}
diff --git a/frontend/src/scenes/settings/environment/agentUsageLogic.ts b/frontend/src/scenes/settings/environment/agentUsageLogic.ts
new file mode 100644
index 000000000000..467142fdc895
--- /dev/null
+++ b/frontend/src/scenes/settings/environment/agentUsageLogic.ts
@@ -0,0 +1,105 @@
+import { MakeLogicType, afterMount, kea, path, reducers, selectors } from 'kea'
+import { loaders } from 'kea-loaders'
+
+import { dayjs } from 'lib/dayjs'
+
+import { llmAnalyticsPersonalSpendList } from 'products/ai_observability/frontend/generated/api'
+import type {
+ PersonalSpendAnalysisResponseApi,
+ _ModelBreakdownRowApi,
+} from 'products/ai_observability/frontend/generated/api.schemas'
+
+import { AgentSpendTotals, agentSpendTotals } from './agentUsageTotals'
+
+export const AGENT_SPEND_PRODUCT = 'posthog_code'
+export const AGENT_SPEND_WINDOW_DAYS = 30
+const TOP_MODEL_COUNT = 5
+
+// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
+export interface agentUsageLogicValues {
+ spend: PersonalSpendAnalysisResponseApi | null
+ spendFailed: boolean
+ spendLoading: boolean
+ topModels: _ModelBreakdownRowApi[]
+ totals: AgentSpendTotals | null
+}
+
+// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
+export interface agentUsageLogicActions {
+ loadSpend: () => any
+ loadSpendFailure: (
+ error: string,
+ errorObject?: any
+ ) => {
+ error: string
+ errorObject?: any
+ }
+ loadSpendSuccess: (
+ spend: PersonalSpendAnalysisResponseApi,
+ payload?: any
+ ) => {
+ spend: PersonalSpendAnalysisResponseApi
+ payload?: any
+ }
+}
+
+// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
+export interface agentUsageLogicMeta {
+ __keaTypeGenInternalSelectorTypes: {
+ totals: (spend: PersonalSpendAnalysisResponseApi | null) => AgentSpendTotals | null
+ topModels: (spend: PersonalSpendAnalysisResponseApi | null) => _ModelBreakdownRowApi[]
+ }
+}
+
+export type agentUsageLogicType = MakeLogicType<
+ agentUsageLogicValues,
+ agentUsageLogicActions,
+ Record<string, any>,
+ agentUsageLogicMeta
+>
+
+export const agentUsageLogic = kea<agentUsageLogicType>([
+ path(['scenes', 'settings', 'environment', 'agentUsageLogic']),
+ loaders({
+ spend: [
+ null as PersonalSpendAnalysisResponseApi | null,
+ {
+ loadSpend: async () => {
+ const response = await llmAnalyticsPersonalSpendList({
+ product: AGENT_SPEND_PRODUCT,
+ date_from: `-${AGENT_SPEND_WINDOW_DAYS}d`,
+ })
+ return response as unknown as PersonalSpendAnalysisResponseApi
+ },
+ },
+ ],
+ }),
+ reducers({
+ spendFailed: [
+ false,
+ {
+ loadSpend: () => false,
+ loadSpendSuccess: () => false,
+ loadSpendFailure: () => true,
+ },
+ ],
+ }),
+ selectors({
+ totals: [
+ (s) => [s.spend],
+ (spend: PersonalSpendAnalysisResponseApi | null): AgentSpendTotals | null =>
+ spend ? agentSpendTotals(spend.by_day.items, dayjs.utc().format('YYYY-MM-DD')) : null,
+ ],
+ topModels: [
+ (s) => [s.spend],
+ (spend: PersonalSpendAnalysisResponseApi | null): _ModelBreakdownRowApi[] =>
+ [...(spend?.by_model.items ?? [])]
+ .filter((row) => row.cost_usd > 0)
+ .sort((a, b) => b.cost_usd - a.cost_usd)
+ .slice(0, TOP_MODEL_COUNT),
+ ],
+ }),
+ afterMount(({ actions }) => {
+ actions.loadSpend()
+ }),
+])
diff --git a/frontend/src/scenes/settings/environment/agentUsageTotals.test.ts b/frontend/src/scenes/settings/environment/agentUsageTotals.test.ts
new file mode 100644
index 000000000000..cbb5b2aa59d3
--- /dev/null
+++ b/frontend/src/scenes/settings/environment/agentUsageTotals.test.ts
@@ -0,0 +1,25 @@
+import { agentSpendTotals } from './agentUsageTotals'
+
+describe('agentSpendTotals', () => {
+ it.each([
+ [
+ 'splits today, this month, and the whole window',
+ [
+ { day: '2026-09-28', cost_usd: 4 },
+ { day: '2026-10-01', cost_usd: 1.5 },
+ { day: '2026-10-02', cost_usd: 2 },
+ ],
+ '2026-10-02',
+ { todayUsd: 2, monthUsd: 3.5, windowUsd: 7.5 },
+ ],
+ [
+ 'reads a day with a time part as that day',
+ [{ day: '2026-10-02T00:00:00Z', cost_usd: 3 }],
+ '2026-10-02',
+ { todayUsd: 3, monthUsd: 3, windowUsd: 3 },
+ ],
+ ['returns zeros for no spend', [], '2026-10-02', { todayUsd: 0, monthUsd: 0, windowUsd: 0 }],
+ ])('%s', (_name, days, todayIso, expected) => {
+ expect(agentSpendTotals(days, todayIso)).toEqual(expected)
+ })
+})
diff --git a/frontend/src/scenes/settings/environment/agentUsageTotals.ts b/frontend/src/scenes/settings/environment/agentUsageTotals.ts
new file mode 100644
index 000000000000..66abc8201b56
--- /dev/null
+++ b/frontend/src/scenes/settings/environment/agentUsageTotals.ts
@@ -0,0 +1,25 @@
+export interface AgentSpendDay {
+ day: string
+ cost_usd: number
+}
+
+export interface AgentSpendTotals {
+ todayUsd: number
+ monthUsd: number
+ windowUsd: number
+}
+
+export function agentSpendTotals(days: AgentSpendDay[], todayIso: string): AgentSpendTotals {
+ const monthPrefix = todayIso.slice(0, 7)
+ return days.reduce<AgentSpendTotals>(
+ (totals, { day, cost_usd }) => {
+ const dayIso = day.slice(0, 10)
+ return {
+ todayUsd: totals.todayUsd + (dayIso === todayIso ? cost_usd : 0),
+ monthUsd: totals.monthUsd + (dayIso.startsWith(monthPrefix) ? cost_usd : 0),
+ windowUsd: totals.windowUsd + cost_usd,
+ }
+ },
+ { todayUsd: 0, monthUsd: 0, windowUsd: 0 }
+ )
+}
diff --git a/frontend/src/scenes/settings/types.ts b/frontend/src/scenes/settings/types.ts
index f342ddce14ed..9b4457edf5a9 100644
--- a/frontend/src/scenes/settings/types.ts
+++ b/frontend/src/scenes/settings/types.ts
@@ -57,6 +57,7 @@ export type SettingSectionId =
| 'environment-task-agent-instructions'
| 'environment-task-agents'
| 'environment-ai-subscriptions'
+ | 'environment-ai-usage'
| 'environment-tracing'
| 'environment-web-analytics'
| 'environment-workflows'
@@ -295,6 +296,7 @@ export type SettingId =
| 'task-agent-my-preference'
| 'ai-subscription-codex'
| 'task-agent-project-instructions'
+ | 'ai-usage-spend'
| 'task-agent-project-default'
| 'theme'
| 'tracing-distinct-id-attribute-keys'