:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
fix(desktop): read model prices from the generated catalog (#105098)
Vojta Bartoše8ec87a3dd94
diff --git a/products/desktop/packages/core/src/billing/modelPricing.test.ts b/products/desktop/packages/core/src/billing/modelPricing.test.tsindex 4cb5973d3ed6..d523fa094776 100644--- a/products/desktop/packages/core/src/billing/modelPricing.test.ts+++ b/products/desktop/packages/core/src/billing/modelPricing.test.ts@@ -2,8 +2,8 @@ import { customModelMeta } from "@posthog/shared";import { describe, expect, it } from "vitest";import {estimateUncachedInputCost,- formatModelRates,modelCostInfo,+ modelCostMultiplier,modelListPrice,toModelPickerOption,} from "./modelPricing";@@ -15,17 +15,23 @@ describe("modelPricing", () => {["claude-sonnet-5", "1×"],["claude-opus-5", "2.5×"],["gpt-5.5", "≈2.8×"],- ["gpt-5.4", "≈1.4×"],["gpt-6-astra", "5×"],- ["deepseek-v4", "≈0.05×"],+ ["deepseek-ai/deepseek-v4-flash-0731", "≈0.05×"],["zai-org/glm-5.3-flash", "≈0.06×"],+ // A picker hands back whichever form the gateway served.+ ["anthropic/claude-opus-5", "2.5×"],+ ["openai/gpt-6-sol", "1×"],] as const)("%s -> %s", (modelId, expected) => {expect(modelCostInfo(modelId)?.multiplierLabel).toBe(expected);});- it("returns null for unknown models so no wrong chip ever renders", () => {- expect(modelCostInfo("totally-unknown-model")).toBeNull();- expect(modelListPrice("totally-unknown-model")).toBeNull();+ it.each([+ // Absent from the catalog, then in it with no published price.+ "totally-unknown-model",+ "gpt-5",+ ] as const)("%s carries no price, so no wrong chip renders", (modelId) => {+ expect(modelCostInfo(modelId)).toBeNull();+ expect(modelListPrice(modelId)).toBeNull();});it("keeps an unpriced gateway model in the picker instead of throwing", () => {@@ -45,30 +51,24 @@ describe("modelPricing", () => {).toMatchObject({ kind: "custom" });});- it("matches specific families before the broader ones they contain", () => {- expect(modelListPrice("gpt-6-astra")).toEqual({- inputPerMtok: 10,- outputPerMtok: 50,- });- expect(modelListPrice("gpt-5.6-luna")?.inputPerMtok).toBe(1);- expect(modelListPrice("gpt-5.5")?.inputPerMtok).toBe(5);- expect(modelListPrice("zai-org/glm-5.3-flash")?.inputPerMtok).toBe(0.15);- expect(modelListPrice("zai-org/glm-5.3")?.inputPerMtok).toBe(1.4);- expect(modelListPrice("claude-sonnet-4-5")?.inputPerMtok).toBe(3);- expect(modelListPrice("claude-sonnet-5")?.inputPerMtok).toBe(2);+ it("ranks models by the numeric multiplier the chips state", () => {+ const opus = modelCostMultiplier("claude-opus-5");+ const sonnet = modelCostMultiplier("claude-sonnet-5");+ expect(sonnet).toBe(1);+ expect(opus).toBe(2.5);+ expect(modelCostMultiplier("totally-unknown-model")).toBeNull();});- it("formats exact rates for tooltips", () => {- // A sub-dollar rate exercises the two-decimal formatting branch.- const price = modelListPrice("deepseek-v4");- expect(price && formatModelRates(price)).toBe(+ it("carries the exact rates for tooltips", () => {+ // A sub-dollar rate exercises the two-decimal formatting the catalog does.+ expect(modelCostInfo("deepseek-ai/deepseek-v4-flash-0731")?.summary).toBe("Input $0.13 · Output $0.26 per 1M tokens",);});it.each([["gpt-5.6-terra", 100_000, 0.25],- ["claude-haiku-4-5", 50_000, 0.05],+ ["claude-sonnet-5", 50_000, 0.1],["unknown-model", 100_000, null],["claude-opus-5", 0, null],] as const)(@@ -79,9 +79,9 @@ describe("modelPricing", () => {);});-// The gateway pins the contract rates these three families bill at, and this-// table claims to mirror them. In the monorepo the gateway file is six-// directories up; a standalone desktop checkout skips the comparison.+// The gateway pins the contract rates these models bill at, and the catalog+// this file reads claims to mirror them. In the monorepo the gateway file is+// six directories up; a standalone desktop checkout skips the comparison.const MONOREPO_ROOT_RELATIVE = "../../../../../..";const MONOREPO_SENTINEL_RELATIVE = `${MONOREPO_ROOT_RELATIVE}/pyproject.toml`;const GATEWAY_OVERRIDES_RELATIVE = `${MONOREPO_ROOT_RELATIVE}/services/llm-gateway/src/llm_gateway/rate_limiting/model_cost_overrides.py`;@@ -111,16 +111,16 @@ function gatewayRate(describe("contract rates match the gateway's pinned table", () => {it.each([- ["kimi", "KIMI_K3_COST"],- ["glm", "BASETEN_GLM_COST"],- ["glm-5.3-flash", "BASETEN_GLM53_FLASH_COST"],- ["deepseek", "BASETEN_DEEPSEEK_COST"],- ["fable", "claude-fable-5"],+ ["moonshotai/kimi-k3", "KIMI_K3_COST"],+ ["zai-org/glm-5.3", "BASETEN_GLM_COST"],+ ["zai-org/glm-5.3-flash", "BASETEN_GLM53_FLASH_COST"],+ ["deepseek-ai/deepseek-v4-flash-0731", "BASETEN_DEEPSEEK_COST"],+ ["claude-fable-5", "claude-fable-5"],["gpt-5.6-sol", "gpt-5.6-sol"],["gpt-5.6-terra", "gpt-5.6-terra"],["gpt-5.6-luna", "gpt-5.6-luna"],["gpt-6-astra", "gpt-6-astra"],- ] as const)("%s", async (family, block) => {+ ] as const)("%s", async (modelId, block) => {// A dynamic import keeps the pure-layer lint honest: only this test// touches the filesystem, and only to read the gateway's table.const { access, readFile } = await import("node:fs/promises");@@ -136,7 +136,7 @@ describe("contract rates match the gateway's pinned table", () => {new URL(GATEWAY_OVERRIDES_RELATIVE, import.meta.url),"utf-8",);- const price = modelListPrice(family);+ const price = modelListPrice(modelId);const input = gatewayRate(gateway, block, "input_cost_per_token");const output = gatewayRate(gateway, block, "output_cost_per_token");expect(input).not.toBeNull();diff --git a/products/desktop/packages/core/src/billing/modelPricing.ts b/products/desktop/packages/core/src/billing/modelPricing.tsindex 7fa6ada5d8e8..5e27b494ddc6 100644--- a/products/desktop/packages/core/src/billing/modelPricing.ts+++ b/products/desktop/packages/core/src/billing/modelPricing.ts@@ -1,30 +1,26 @@import { isCustomModelOption } from "@posthog/shared";+import {+ COST_BASELINE_LABEL,+ COST_BASELINE_MODEL,+ catalogModelFor,+ type ModelCost,+} from "@posthog/shared/model-catalog";/**- * List prices per 1M tokens for every model the pickers can show, and the- * relative per-token cost derived from them. One place on the client; keep in- * sync with the gateway's billing source- * (services/llm-gateway/src/llm_gateway/rate_limiting/model_cost_overrides.py).- *- * Sources, checked 2026-09-07:- * - Anthropic (Opus, Sonnet, Haiku): platform.claude.com/docs/en/about-claude/pricing- * - GPT-5.4, GPT-5.5, GPT-6 Sol, GPT-6 Luna: developers.openai.com/api/docs/pricing- * - Fable, GPT-5.6, GPT-6 Astra, Kimi K3, GLM, DeepSeek: the gateway's billing rates, what- * the user is actually charged (pinned in the file above). The drift test- * binds these rows to it.+ * Rates and rendered strings both come from the generated catalog, which takes+ * them from products/tasks/backend/model_catalog.py, which pins them against+ * services/llm-gateway/src/llm_gateway/rate_limiting/model_cost_overrides.py.+ * That chain is what makes a price on a chip the price the user is charged.*/-export interface ModelListPrice {- inputPerMtok: number;- outputPerMtok: number;-}+export type ModelListPrice = ModelCost;export interface ModelCostInfo {price: ModelListPrice;/** Per-token cost relative to the baseline, e.g. "0.5×" or "≈1.1×". */multiplierLabel: string;- /** True when input and output ratios diverge and the label is approximate. */- approximate: boolean;+ /** "Input $2 · Output $10 per 1M tokens": the exact rates behind a chip. */+ summary: string;}export interface ModelPickerOptionBase {@@ -52,105 +48,38 @@ export type ModelPickerOption =| UnpricedModelPickerOption;/** The 1× anchor every multiplier is stated against. */-export const MODEL_COST_BASELINE_NAME = "Claude Sonnet 5";-const BASELINE: ModelListPrice = { inputPerMtok: 2, outputPerMtok: 10 };--// Matched by lowercase substring, first hit wins, so keep specific ids-// (gpt-5.6-*, glm-5.3-flash, sonnet-4) ahead of their broader families.-const LIST_PRICES: [family: string, price: ModelListPrice][] = [- ["fable", { inputPerMtok: 10, outputPerMtok: 50 }],- ["mythos", { inputPerMtok: 10, outputPerMtok: 50 }],- ["opus-5-5", { inputPerMtok: 4, outputPerMtok: 20 }],- ["opus", { inputPerMtok: 5, outputPerMtok: 25 }],- ["sonnet-4", { inputPerMtok: 3, outputPerMtok: 15 }],- ["sonnet", { inputPerMtok: 2, outputPerMtok: 10 }],- ["haiku", { inputPerMtok: 1, outputPerMtok: 5 }],- ["gpt-6-astra", { inputPerMtok: 10, outputPerMtok: 50 }],- ["gpt-6-sol", { inputPerMtok: 2, outputPerMtok: 10 }],- ["gpt-6-luna", { inputPerMtok: 0.1, outputPerMtok: 0.5 }],- ["gpt-5.6-sol", { inputPerMtok: 5, outputPerMtok: 30 }],- ["gpt-5.6-terra", { inputPerMtok: 2.5, outputPerMtok: 15 }],- ["gpt-5.6-luna", { inputPerMtok: 1, outputPerMtok: 6 }],- ["gpt-5.5", { inputPerMtok: 5, outputPerMtok: 30 }],- ["gpt-5.4", { inputPerMtok: 2.5, outputPerMtok: 15 }],- ["kimi", { inputPerMtok: 3, outputPerMtok: 15 }],- ["glm-5.3-flash", { inputPerMtok: 0.15, outputPerMtok: 0.5 }],- ["glm", { inputPerMtok: 1.4, outputPerMtok: 4.4 }],- ["deepseek", { inputPerMtok: 0.13, outputPerMtok: 0.26 }],-];+export const MODEL_COST_BASELINE_NAME = COST_BASELINE_LABEL;export function modelListPrice(modelId: string): ModelListPrice | null {- const id = modelId.toLowerCase();- for (const [family, price] of LIST_PRICES) {- if (id.includes(family)) return price;- }- return null;-}--/** "$4" for whole dollars, "$0.20" otherwise. */-function formatPerMtok(amount: number): string {- if (Number.isInteger(amount)) return `$${amount}`;- return `$${amount.toFixed(2)}`;-}--/** "Input $2 · Output $10 per 1M tokens": the exact rates behind a chip. */-export function formatModelRates(price: ModelListPrice): string {- return `Input ${formatPerMtok(price.inputPerMtok)} · Output ${formatPerMtok(price.outputPerMtok)} per 1M tokens`;-}--function formatMultiplier(value: number, approximate: boolean): string {- let rounded: string;- if (value >= 10) {- rounded = String(Math.round(value));- } else if (value >= 0.95) {- const oneDecimal = Math.round(value * 10) / 10;- rounded = Number.isInteger(oneDecimal)- ? String(oneDecimal)- : oneDecimal.toFixed(1);- } else {- const twoDecimals = Math.round(value * 100) / 100;- rounded = String(twoDecimals);- }- return `${approximate ? "≈" : ""}${rounded}×`;+ return catalogModelFor(modelId)?.cost ?? null;}/**- * Mean of the input and output rate ratios between two prices, flagged- * approximate when the two ratios diverge by more than 10%.+ * The quantity the catalog renders into `costMultiplier`, as a number, for the+ * callers that rank models by cost instead of naming one.*/-function blendedRatio(- numerator: ModelListPrice,- denominator: ModelListPrice,-): { blended: number; approximate: boolean } {- const inputRatio = numerator.inputPerMtok / denominator.inputPerMtok;- const outputRatio = numerator.outputPerMtok / denominator.outputPerMtok;- const approximate =- Math.abs(inputRatio - outputRatio) / Math.max(inputRatio, outputRatio) >- 0.1;- return { blended: (inputRatio + outputRatio) / 2, approximate };-}--/** Blended per-token multiplier vs the baseline, e.g. 2.5. Null when unpriced. */export function modelCostMultiplier(modelId: string): number | null {const price = modelListPrice(modelId);- if (!price) return null;- return blendedRatio(price, BASELINE).blended;+ const baseline = modelListPrice(COST_BASELINE_MODEL);+ if (!price || !baseline) return null;+ const inputRatio = price.inputPerMtok / baseline.inputPerMtok;+ const outputRatio = price.outputPerMtok / baseline.outputPerMtok;+ return (inputRatio + outputRatio) / 2;}export function modelCostInfo(modelId: string): ModelCostInfo | null {- const price = modelListPrice(modelId);- if (!price) return null;- const { blended, approximate } = blendedRatio(price, BASELINE);+ const model = catalogModelFor(modelId);+ if (!model?.cost || !model.costMultiplier || !model.costSummary) return null;return {- price,- multiplierLabel: formatMultiplier(blended, approximate),- approximate,+ price: model.cost,+ multiplierLabel: model.costMultiplier,+ summary: model.costSummary,};}/*** Converts an ACP model into the picker contract. The pickers call this during- * render, and the harness names model ids this table may not hold, so an+ * render, and the harness names model ids the catalog may not carry, so an* unknown id costs the row its cost chip and nothing more.*/export function toModelPickerOption(diff --git a/products/desktop/packages/shared/src/model-catalog.ts b/products/desktop/packages/shared/src/model-catalog.tsindex 139a60307457..d0418db1b527 100644--- a/products/desktop/packages/shared/src/model-catalog.ts+++ b/products/desktop/packages/shared/src/model-catalog.ts@@ -1,5 +1,6 @@import type { Adapter } from "./adapter";import {+ type CatalogModel,FALLBACK_REASONING_EFFORTS,FAMILY_REASONING_EFFORTS,MODELS,@@ -9,8 +10,11 @@ import {export {type CatalogModel,+ COST_BASELINE_LABEL,+ COST_BASELINE_MODEL,DEFAULT_MODEL_BY_RUNTIME_ADAPTER,MODELS,+ type ModelCost,PROVIDER_BY_RUNTIME_ADAPTER,REASONING_EFFORTS,type ReasoningEffort,@@ -68,12 +72,14 @@ export function reasoningEffortsForModel(}/**- * The name the catalog pins for a model, or `undefined` to let the caller format the id.- *- * Only the ids whose derived name reads wrong carry one, so a caller keeps its formatter- * for everything else. Mirrors `label_for_model` in- * products/tasks/backend/model_catalog.py, so both surfaces name a model identically.+ * The catalog's entry for a model id in any form a picker hands back. The gateway serves+ * ids the catalog omits, so a miss is the ordinary case and never an error.*/+export function catalogModelFor(modelId: string): CatalogModel | undefined {+ const normalized = normalizeModelId(modelId);+ return MODELS.find((candidate) => candidate.id === normalized);+}+/*** Every access flag the catalog gates a model behind, without duplicates.*@@ -97,11 +103,16 @@ export const MODEL_ACCESS_FLAGS: readonly string[] = [* projection can hide a model a person may use, never the other way round.*/export function accessFlagForModel(modelId: string): string | undefined {- const normalized = normalizeModelId(modelId);- return MODELS.find((candidate) => candidate.id === normalized)?.accessFlag;+ return catalogModelFor(modelId)?.accessFlag;}+/**+ * The name the catalog pins for a model, or `undefined` to let the caller format the id.+ *+ * Only the ids whose derived name reads wrong carry one, so a caller keeps its formatter+ * for everything else. Mirrors `label_for_model` in+ * products/tasks/backend/model_catalog.py, so both surfaces name a model identically.+ */export function labelForModel(modelId: string): string | undefined {- const normalized = normalizeModelId(modelId);- return MODELS.find((candidate) => candidate.id === normalized)?.label;+ return catalogModelFor(modelId)?.label;}diff --git a/products/desktop/packages/ui/src/features/sessions/components/ModelCostChip.stories.tsx b/products/desktop/packages/ui/src/features/sessions/components/ModelCostChip.stories.tsxindex 8ee6990ddb4d..9e7b9f076630 100644--- a/products/desktop/packages/ui/src/features/sessions/components/ModelCostChip.stories.tsx+++ b/products/desktop/packages/ui/src/features/sessions/components/ModelCostChip.stories.tsx@@ -12,6 +12,8 @@ import { ModelCostFooter } from "./ModelCostChip";import { ModelRadioItem } from "./ModelRadioItem";const MODELS = [+ // Not a catalog id, so this row shows how an unpriced model sits next to+ // the priced ones.{ value: "claude-haiku-4-5", name: "Claude Haiku 4.5" },{ value: "claude-sonnet-5", name: "Claude Sonnet 5" },{ value: "claude-opus-5-5", name: "Claude Opus 5.5" },@@ -24,7 +26,7 @@ const MODELS = [name: "GPT-6 Astra",_meta: { "posthog.code/restrictedModel": true },},- { value: "glm-5.3", name: "GLM 5.3" },+ { value: "zai-org/glm-5.3", name: "GLM 5.3" },{value: "some-custom-model",name: "Custom model",diff --git a/products/desktop/packages/ui/src/features/sessions/components/ModelCostChip.tsx b/products/desktop/packages/ui/src/features/sessions/components/ModelCostChip.tsxindex d7900533b003..a9dc3bb301e0 100644--- a/products/desktop/packages/ui/src/features/sessions/components/ModelCostChip.tsx+++ b/products/desktop/packages/ui/src/features/sessions/components/ModelCostChip.tsx@@ -1,12 +1,11 @@import {- formatModelRates,MODEL_COST_BASELINE_NAME,type ModelCostInfo,} from "@posthog/core/billing/modelPricing";/** The exact rates behind a multiplier, for the chip's title. */export function modelCostTitle(cost: ModelCostInfo): string {- return `Cost per token vs ${MODEL_COST_BASELINE_NAME} · ${formatModelRates(cost.price)}`;+ return `Cost per token vs ${MODEL_COST_BASELINE_NAME} · ${cost.summary}`;}/**