langfuse icon indicating copy to clipboard operation
langfuse copied to clipboard

bug: *gen_ai.usage.details.* metrics from PydanticAI cause "Total Usage" double-counting in dashboard

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

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

  1. Run a PydanticAI Agent with google:gemini-2.5-flash.
  2. Configure the OTLP exporter to send traces to Langfuse
  3. Open the Langfuse dashboard and inspect the Trace.
  4. 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.

Image

Are you interested in contributing a fix for this bug?

Yes

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

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:

  1. 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.
  2. Map details.text_prompt_tokens explicitly to an alias of input so it gets merged rather than added on top.
  3. Add details.thoughts_tokens to a subtraction path similar to how cache tokens are handled (since thinking tokens are already included in output).

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.

Leave Feedback Ask Dosu about langfuse Share Dosu with your team

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

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

github-actions[bot] avatar Sep 25 '26 01:09 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 09 '26 01:10 github-actions[bot]

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?

sipa-echo-zaoa avatar Oct 26 '26 13:10 sipa-echo-zaoa