bug: pydantic-ai cache tokens still dropped (and input double-counted) in extractUsageDetails — follow-up to #12245
Describe the bug
The instrumentationScopeName === "pydantic-ai" branch of OtelIngestionProcessor.extractUsageDetails still drops prompt-cache token counts as of v3.155.1, so Langfuse prices every cached token at the full input rate. This is the bug reported in #12245. That issue was closed as stale without a fix, and the proposed fix #12248 has been open since February. #12248 is not a complete fix on its own, for two reasons.
1. pydantic-ai 2.0 emits different attribute names from the ones the branch reads. The branch reads gen_ai.usage.cache_read_tokens / gen_ai.usage.details.cache_read_input_tokens, plus the write equivalents. pydantic-ai 2.0 emits gen_ai.usage.cache_read.input_tokens and gen_ai.usage.details.cache_read_tokens, plus the write equivalents. #12248 adds the details.* names. It does not add the gen_ai.usage.cache_read.input_tokens / cache_write.input_tokens form.
2. pydantic-ai's input_tokens already includes the cached tokens. For the Anthropic and Bedrock mappings, gen_ai.usage.input_tokens is the gross prompt size: uncached + cache read + cache write. The branch stores that value as input unchanged. Once the cache fields are populated, the cached tokens are priced twice, once as input and again as input_cache_read / input_cache_creation. #12248's own test shows it: it expects input: 174 alongside input_cache_read: 100 for a span whose input includes those 100 cached tokens.
To reproduce
- Run a pydantic-ai ≥ 2.0 agent against Anthropic on Bedrock with prompt caching enabled. Send the traces to Langfuse through OTel, using the
pydantic-aiinstrumentation scope. - Make two calls that share a cached prefix.
- On the second generation,
usage_detailscontains onlyinput/output: noinput_cache_readand noinput_cache_creation. Its cost is computed as if every prompt token were uncached.
Expected behavior
input_cache_read/input_cache_creationare populated from whichever attribute form pydantic-ai emits:gen_ai.usage.cache_read.input_tokens,gen_ai.usage.details.cache_read_tokensorgen_ai.usage.cache_read_tokens, plus the write equivalents.inputis the uncached remainder:input_tokens − cache_read − cache_write, floored at 0, and only when the cache fields came from the same span. This keeps cost correct and avoids double counting.
Impact
For a chat workload that is heavily cache-read (~88% of input tokens served from cache), Langfuse reported that workload's model cost at about 3.5× what the provider billed for the same calls.
Additional information
We work around it in our own instrumentation by emitting the attribute names the branch reads, with input set to the uncached remainder. We'd be glad to open a PR covering both points if you'd like one; #12248 could also be extended instead. Please let us know which you prefer.
- Langfuse version: v3.155.1 (self-hosted)
- pydantic-ai version: 2.0
- Provider: Anthropic via AWS Bedrock
Disclosure: this issue was authored by Claude (Anthropic's AI assistant, via Claude Code) on behalf of @HipHoff, who reviewed and takes responsibility for it.
Source: langfuse/langfuse