Skip to content

fix: include cached tokens in gen_ai.usage.input_tokens for Anthropic#1812

Merged
alexmojaki merged 2 commits intomainfrom
fix-anthropic-input-tokens-total
Mar 27, 2026
Merged

fix: include cached tokens in gen_ai.usage.input_tokens for Anthropic#1812
alexmojaki merged 2 commits intomainfrom
fix-anthropic-input-tokens-total

Conversation

@alexmojaki
Copy link
Copy Markdown
Collaborator

Summary

  • gen_ai.usage.input_tokens now includes cache_read_input_tokens and cache_creation_input_tokens in the Anthropic integration
  • The Anthropic API's input_tokens field only counts uncached tokens, but the OTel GenAI semconv defines this attribute as the total input token count
  • When prompt caching is enabled, the previous behavior significantly undercounted input tokens

Test plan

  • make typecheck passes (0 errors)
  • All 22 tests in tests/otel_integrations/test_anthropic.py pass

🤖 Generated with Claude Code

Anthropic's usage.input_tokens only counts uncached tokens, but the
OTel GenAI semconv defines gen_ai.usage.input_tokens as the total input
token count. Sum in cache_read_input_tokens and cache_creation_input_tokens
when present so the reported total is accurate with prompt caching.
devin-ai-integration[bot]

This comment was marked as resolved.

@cloudflare-workers-and-pages
Copy link
Copy Markdown

cloudflare-workers-and-pages bot commented Mar 27, 2026

Deploying logfire-docs with  Cloudflare Pages  Cloudflare Pages

Latest commit: 4ee9700
Status: ✅  Deploy successful!
Preview URL: https://ee3c1d55.logfire-docs.pages.dev
Branch Preview URL: https://fix-anthropic-input-tokens-t.logfire-docs.pages.dev

View logs

@alexmojaki alexmojaki merged commit 9272a06 into main Mar 27, 2026
15 checks passed
@alexmojaki alexmojaki deleted the fix-anthropic-input-tokens-total branch March 27, 2026 12:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant