`gen_ai.usage.input_tokens` on LLM spans leaves out cached tokens for Anthropic and Bedrock
pipecat version
main (6396f9771), 1.12.1.dev10
Python version
3.12.14
Operating System
macOS 27.0
Issue description
The llm span's gen_ai.usage.input_tokens is set from LLMTokenUsage.prompt_tokens (_add_token_usage_to_span in src/pipecat/utils/tracing/service_decorators.py). AnthropicLLMService and AWSBedrockLLMService report prompt_tokens net of the prompt cache, with the cache counts beside it, as documented on LLMTokenUsage. So for these services the span leaves the cached tokens out of gen_ai.usage.input_tokens, and gen_ai.usage.cache_read.input_tokens can be larger than it.
The OpenTelemetry GenAI conventions define gen_ai.usage.input_tokens as including cached tokens: "This value SHOULD include all types of input tokens, including cached tokens", and both cache attributes "SHOULD be included in gen_ai.usage.input_tokens" (gen-ai attributes). Tracing backends that price spans from these attributes undercount every cached turn.
OpenAI-compatible services are not affected, since their prompt_tokens already includes the cached tokens.
Reproduction steps
- Install
pipecat-ai[anthropic,tracing]. - Run the script below. It builds the usage
AnthropicLLMServicereports for a turn where the API returnedinput_tokens=100,cache_creation_input_tokens=300,cache_read_input_tokens=2000andoutput_tokens=50, and adds it to a span the waytraced_llmdoes. - Compare
gen_ai.usage.input_tokenswith the cache attributes.
Minimal reproducible example
import asyncio
from unittest.mock import AsyncMock
from opentelemetry import trace
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import SimpleSpanProcessor
from opentelemetry.sdk.trace.export.in_memory_span_exporter import InMemorySpanExporter
from pipecat.services.anthropic.llm import AnthropicLLMService
from pipecat.utils.tracing.service_decorators import _add_token_usage_to_span
async def main():
exporter = InMemorySpanExporter()
provider = TracerProvider()
provider.add_span_processor(SimpleSpanProcessor(exporter))
trace.set_tracer_provider(provider)
service = AnthropicLLMService(api_key="not-used")
service.start_llm_usage_metrics = AsyncMock()
await service._report_usage_metrics(
prompt_tokens=100,
completion_tokens=50,
cache_creation_input_tokens=300,
cache_read_input_tokens=2000,
)
usage = service.start_llm_usage_metrics.call_args.args[0]
print("LLMTokenUsage:", usage.model_dump(exclude_none=True))
with trace.get_tracer("pipecat").start_as_current_span("llm") as span:
_add_token_usage_to_span(span, usage)
attributes = exporter.get_finished_spans()[0].attributes
for key in sorted(attributes):
if key.startswith("gen_ai.usage."):
print(f"{key} = {attributes[key]}")
asyncio.run(main())Expected behavior
gen_ai.usage.input_tokens = 2400 (100 uncached, 2000 read from the cache, 300 written to it), so the cache attributes are parts of it and input plus output matches the total_tokens of 2450.
Actual behavior
LLMTokenUsage: {'prompt_tokens': 100, 'completion_tokens': 50, 'total_tokens': 2450, 'cache_read_input_tokens': 2000, 'cache_creation_input_tokens': 300}
gen_ai.usage.cache_creation.input_tokens = 300
gen_ai.usage.cache_read.input_tokens = 2000
gen_ai.usage.input_tokens = 100
gen_ai.usage.output_tokens = 50Before you submit
- I have personally reproduced this issue on the latest Pipecat release (or
main). - I have searched existing issues for this bug.
- If AI tools helped write this report, I have verified the behavior myself and will personally answer follow-up questions.
Source: pipecat-ai/pipecat