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.

fix(llm-analytics): price gemini reasoning tokens for models/-prefixed names (#74297)

Radu Raicea247a3420e269
diff --git a/nodejs/src/ingestion/pipelines/ai/costs/output-costs.test.ts b/nodejs/src/ingestion/pipelines/ai/costs/output-costs.test.ts
index 02211e338e9c..44acf73eb381 100644
--- a/nodejs/src/ingestion/pipelines/ai/costs/output-costs.test.ts
+++ b/nodejs/src/ingestion/pipelines/ai/costs/output-costs.test.ts
@@ -180,6 +180,18 @@ describe('calculateOutputCost()', () => {
expectedCost: 0.0036,
description: 'includes reasoning tokens for gemini-3.1-pro-preview',
},
+ {
+ // The Gemini SDK reports models as `models/<name>`; the provider
+ // prefix must be stripped before the anchored reasoning regex,
+ // otherwise reasoning tokens are silently priced at zero.
+ modelName: 'models/gemini-3-flash-preview',
+ modelRow: gemini3FlashModel,
+ outputTokens: 2,
+ reasoningTokens: 77,
+ // (2 + 77) * 3e-6 = 0.000237
+ expectedCost: 0.000237,
+ description: 'includes reasoning tokens for models/-prefixed gemini-3-flash-preview',
+ },
])('$description', ({ modelName, modelRow, outputTokens, reasoningTokens, expectedCost }) => {
const event = createAIEvent({
$ai_provider: 'google',
diff --git a/nodejs/src/ingestion/pipelines/ai/costs/output-costs.ts b/nodejs/src/ingestion/pipelines/ai/costs/output-costs.ts
index ce6628d70872..af81e534b43c 100644
--- a/nodejs/src/ingestion/pipelines/ai/costs/output-costs.ts
+++ b/nodejs/src/ingestion/pipelines/ai/costs/output-costs.ts
@@ -9,7 +9,16 @@ import { ResolvedModelCost } from './providers/types'
const REASONING_COST_MODELS = [/^gemini-2\.5-/, /^gemini-3(\.\d+)?-/]
export const mustAddReasoningCost = (model: string): boolean => {
- return REASONING_COST_MODELS.some((candidate) => candidate.test(model.toLowerCase()))
+ const lowerCaseModel = model.toLowerCase()
+ // Strip any provider/path prefix (e.g. the Gemini API's `models/` resource
+ // prefix, or a `google/` provider prefix) before matching. The patterns are
+ // start-anchored, so `models/gemini-3-flash-preview` would otherwise never
+ // match and its (billed) reasoning tokens would be priced at zero. This
+ // mirrors how findCostFromModel resolves the price for the same string.
+ const bareModel = lowerCaseModel.includes('/')
+ ? (lowerCaseModel.split('/').pop() ?? lowerCaseModel)
+ : lowerCaseModel
+ return REASONING_COST_MODELS.some((candidate) => candidate.test(bareModel))
}
const warnMissingModalityRate = (event: PluginEvent, cost: ResolvedModelCost, modality: 'audio' | 'image'): void => {