langfuse icon indicating copy to clipboard operation
langfuse copied to clipboard

bug : Natively calculate cost for reasoning/thought tokens (Gemini/OpenAI) using output token prices

Open SAMurai-16 opened this issue 3 months ago • 5 comments

Describe the bug

With the rise of reasoning models (e.g., Google Gemini 2.0 Flash Thinking, OpenAI o1/o3-mini), providers are now returning internal reasoning tokens separately from standard completion tokens.

When frameworks ingest these traces via OpenTelemetry, these reasoning tokens are being emitted separately (often under gen_ai.usage.reasoning_tokens,gen_ai.usage.reasoning.output_tokens, or provider-specific fields like thoughts_tokens).

The Problem: Currently, Langfuse's default cost calculation engine only applies prices to the standard input_tokens and output_tokens fields. When an OTEL trace includes reasoning tokens separately, Langfuse tracks them as an "Arbitrary Usage Type" (e.g., under "Other usage"), but it fails to assign any monetary cost to them by default.

Because both Google and OpenAI officially bill reasoning/thought tokens at the exact same rate as standard output tokens, Langfuse is systematically under-calculating the cost of API runs for reasoning models out-of-the-box.

Expected Behavior: Langfuse's pricing engine should natively recognize standard reasoning token keys (e.g., reasoning_tokens, thoughts_tokens, gen_ai.usage.reasoning.output_tokens) and automatically apply the model's output token price to them to calculate the total run cost.

Users should not be forced to manually configure custom model definitions and arbitrary usage prices just to get accurate billing for standard reasoning models.

Steps to reproduce

  1. Execute an LLM call using a reasoning model (e.g., gemini-2.5-flash or o1-mini) and export the trace to Langfuse via OTLP.
  2. Ensure the OTLP trace payload separates reasoning tokens from standard output tokens (e.g., gen_ai.usage.output_tokens: 100, gen_ai.usage.reasoning_tokens: 500).
  3. Open the trace in the Langfuse dashboard.
  4. Check the token counts: Notice that Langfuse successfully tracks the reasoning tokens under "Other Usage" or "Arbitrary Usage Types".
  5. Check the USD Cost: Notice that Langfuse only multiplied the output_tokens (100) by the output token price, completely ignoring the cost of the 500 reasoning tokens (billing them at $0.00).

Langfuse Cloud or self-hosted?

Langfuse Cloud

If self-hosted, what version are you running?

No response

SDK and integration versions

No response

Additional information

No response

Are you interested in contributing a fix for this bug?

Yes

SAMurai-16 avatar Aug 24 '26 19:08 SAMurai-16

@SAMurai-16 your analysis is correct — this is a gap in the generic OTel normalization path. Here's what's happening under the hood:

What already works:

  • Vercel AI SDK (ai scope): ai.usage.reasoningTokens and ai.response.providerMetadata.openai.reasoningTokens are mapped to output_reasoning_tokens, and output is decremented to avoid double-counting [1]. The pricing config has output_reasoning_tokens priced at the output rate [2].

  • Genkit (genkit-tracer scope): thoughtsTokens → output_reasoning, with output adjusted by subtracting thoughts [3].

What breaks for generic OTEL (your scenario):

The extractGenericGenAiUsageDetails() fallback — used by OpenInference, TraceLoop, and other instrumentation — strips the gen_ai.usage. prefix but does not normalize reasoning-specific keys. So:

  • gen_ai.usage.reasoning_tokens → stored as reasoning_tokens
  • gen_ai.usage.reasoning.output_tokens → stored as reasoning.output_tokens
  • gen_ai.usage.thoughts_tokens → stored as thoughts_tokens

None of those match the pricing entries (output_reasoning_tokens, output_reasoning), so the tokens appear in usage but are billed at $0. This is the same class of issue reported in #16321 [4].

What a fix needs to handle:

  1. Add normalization rules in extractGenericGenAiUsageDetails() to map known reasoning key variants (reasoning_tokens, reasoning.output_tokens, thoughts_tokens, etc.) to output_reasoning_tokens [5], mirroring how cache tokens are already normalized.

  2. Provider-aware output adjustment: OpenAI's output_tokens typically includes reasoning tokens (so output must be decremented), while Gemini's candidatesTokenCount excludes thoughts (so no decrement needed). This provider ambiguity is the tricky part [4].

Workaround until fixed: Set langfuse.observation.usage_details as a span attribute with already-normalized keys (e.g., output_reasoning_tokens for OpenAI, output_reasoning for Gemini) — this takes highest priority and skips the generic path entirely [6]. Alternatively, pass provided_cost_details via the SDK to bypass automatic cost calculation.

If you're contributing a fix, the key file is OtelIngestionProcessor.ts — specifically the normalization block in extractGenericGenAiUsageDetails() around lines 2534–2592 [5].

To reply, just mention @dosu.


Share context across your team and agents. Try Dosu.

Leave Feedback Learn about langfuse with Dosu Add Dosu to your team

dosubot[bot] avatar Aug 24 '26 19:08 dosubot[bot]

Thanks for raising this, we will consider this

hassiebp avatar Aug 31 '26 13:08 hassiebp

This issue is stale because it has been open for 30 days with no activity.

github-actions[bot] avatar Oct 01 '26 01:10 github-actions[bot]

This issue was closed because it has been inactive for 14 days since being marked as stale. Please reopen if the issue persists.

github-actions[bot] avatar Oct 18 '26 01:10 github-actions[bot]

This also affects Claude models, not only Gemini and OpenAI.

Setup: Langfuse Cloud (EU), Langfuse Python SDK 5.2.0, Anthropic Python SDK 2.8.0. The Anthropic SDK emits its own OpenTelemetry span per request (anthropic.messages.create), which Langfuse ingests as a generation. The span reports thinking tokens as gen_ai.usage.reasoning.output_tokens, and Langfuse shows them under the usage key output_reasoning_tokens, separate from output.

Problem: The managed price definition for claude-haiku-5-5 has no entry for output_reasoning_tokens, so thinking tokens are counted but priced at $0. Anthropic bills thinking tokens at the output rate.

Example (one call, adaptive thinking, effort high):

Usage type Tokens
input 6,223
output 2,056
output_reasoning_tokens 12,899

At $0.10 / $0.50 per million tokens this call costs about $0.0081. With only input and output priced it comes to about $0.0017, so the cost is understated by roughly a factor of five. With adaptive thinking the reasoning share of the output is often this large.

Workaround: cloning the model definition in the project and adding a price for output_reasoning_tokens at the output rate gives correct costs.

The fallback in #16509 (price reasoning tokens at the output rate when no explicit price exists) would fix this, provided output_reasoning_tokens is among the aliases it covers. Alternatively, the managed Claude definitions could get an explicit output_reasoning_tokens price.

jack-kerouac avatar Nov 12 '26 23:11 jack-kerouac