bug: *gen_ai.usage.details.* metrics from PydanticAI cause "Total Usage" double-counting in dashboard
Describe the bug
When using OpenTelemetry to ingest traces from PydanticAI using the Gemini models (gemini-2.5-flash,google:gemma-4-31b-it), the Langfuse dashboard artificially inflates and double-counts the token usage in the "Total usage" calculation.
The Problem: PydanticAI correctly emits the standard OpenTelemetry metric gen_ai.usage.input_tokens. However, as per OpenTelemetry conventions, it also emits provider-specific token breakdown metadata under the details prefix (e.g., gen_ai.usage.details.text_prompt_tokens and gen_ai.usage.details.thoughts_tokens).
Langfuse groups these detailed metrics under "Other usage" in the UI (stripping the prefix to show text_prompt_tokens), but then blindly sums them into the Total usage calculation. Because text_prompt_tokens is essentially identical to input_tokens, the Total Usage metric is exactly double what it should be.
Steps to reproduce
- Run a PydanticAI Agent with
google:gemini-2.5-flash. - Configure the OTLP exporter to send traces to Langfuse
- Open the Langfuse dashboard and inspect the Trace.
- Open the "Usage breakdown" popover.
Langfuse Cloud or self-hosted?
Langfuse Cloud
If self-hosted, what version are you running?
No response
SDK and integration versions
pydantic-ai 2.32.0 opentelemetry-api 1.44.0 opentelemetry-sdk 1.44.0 opentelemetry-exporter-otlp 1.44.0
Additional information
Expected Behavior: Langfuse should display text_prompt_tokens as informational metadata in the breakdown, but it should not sum them into the Total Usage cost calculation, as they overlap with gen_ai.usage.input_tokens.
Actual Behavior: Total Usage = Input usage + Output usage + Other usage (text_prompt_tokens + thoughts_tokens), resulting in double the actual API usage.
Are you interested in contributing a fix for this bug?
Yes
Thanks for the detailed report, @SAMurai-16! This is a real issue, and the root cause is clear from the code.
In extractGenericGenAiUsageDetails(), all gen_ai.usage.* attributes are collected and the gen_ai.usage. prefix is stripped. Your attributes like gen_ai.usage.details.text_prompt_tokens become details.text_prompt_tokens, and then the details. prefix is stripped again to produce text_prompt_tokens [1].
The problem is that details.text_prompt_tokens is not in the known-and-excluded list of cache/standard keys [2], so it falls through into normalizedUsageDetails as a raw "other" key. The UI then sums all keys whose name starts with input or output via reduceUsageOrCostDetails [3] — and since text_prompt_tokens is essentially identical to input_tokens, you get the double-count you observed.
The existing logic already handles specific details.* cache aliases like details.cache_read_tokens and details.cache_read_input_tokens [4], but details.text_prompt_tokens and details.thoughts_tokens are not handled.
For a contributor, the fix lives in extractGenericGenAiUsageDetails() in OtelIngestionProcessor.ts. There are a few possible approaches:
- Exclude all
details.*keys from the normalized usage map entirely, treating them as informational breakdowns rather than additive buckets. Per OTel convention,gen_ai.usage.details.*attributes are decompositions of the top-level metrics, not independent token buckets. - Map
details.text_prompt_tokensexplicitly to an alias ofinputso it gets merged rather than added on top. - Add
details.thoughts_tokensto a subtraction path similar to how cache tokens are handled (since thinking tokens are already included inoutput).
Option 1 is likely the cleanest and most forward-compatible.
This is the same class of problem previously tracked for cache tokens in #10592 and #12306, which were resolved by adding explicit cache key handling [5] — but details.text_prompt_tokens and details.thoughts_tokens (as emitted by PydanticAI + Gemini) were not covered by those fixes.
To reply, just mention @dosu.
Docs are dead. Just use Dosu.
This issue is stale because it has been open for 30 days with no activity.
This issue was closed because it has been inactive for 14 days since being marked as stale. Please reopen if the issue persists.
This still reproduces with Pydantic AI 2.54.0 and Langfuse Python SDK 5.0.4 on Langfuse Cloud, using OpenAI models. gen_ai.usage.output_tokens already includes reasoning tokens, but gen_ai.usage.details.reasoning_tokens is counted again. For example, output=249 and reasoning=73 produces 322 output tokens instead of 249, inflating usage and cost. Could you reopen this issue?