Skip to content

fix(anthropic): restore accidentally lost cache tokens attributes#3648

Open
dinmukhamedm wants to merge 1 commit intotraceloop:mainfrom
lmnr-ai:fix/anthropic-cached-tokens
Open

fix(anthropic): restore accidentally lost cache tokens attributes#3648
dinmukhamedm wants to merge 1 commit intotraceloop:mainfrom
lmnr-ai:fix/anthropic-cached-tokens

Conversation

@dinmukhamedm
Copy link
Collaborator

@dinmukhamedm dinmukhamedm commented Jan 31, 2026

This was accidentally removed in #3138 and some subsequent changes. Restoring. More context in #3647.

Note: this PR does NOT fully close #3647, as there will be a new attribute convention soon, see open-telemetry/semantic-conventions#3163

  • I have added tests that cover my changes.
  • If adding a new instrumentation or changing an existing one, I've added screenshots from some observability platform showing the change.
  • PR name follows conventional commits format: feat(instrumentation): ... or fix(instrumentation): ....
  • (If applicable) I have updated the documentation accordingly.

Important

Restores cache token attributes in Anthropic instrumentation and updates tests to verify correct attribute settings.

  • Behavior:
    • Restores cache token attributes in _aset_token_usage() and _set_token_usage() in __init__.py and _set_token_usage() in streaming.py.
    • Updates attribute names from GEN_AI_USAGE_CACHE_READ_INPUT_TOKENS to LLM_USAGE_CACHE_READ_INPUT_TOKENS and GEN_AI_USAGE_CACHE_CREATION_INPUT_TOKENS to LLM_USAGE_CACHE_CREATION_INPUT_TOKENS.
  • Tests:
    • Adds assertions in test_prompt_caching.py to verify cache token attributes are correctly set for both sync and async operations.

This description was created by Ellipsis for daa6ff5. You can customize this summary. It will automatically update as commits are pushed.

Summary by CodeRabbit

  • New Features

    • Enhanced token usage metrics to track cache creation and cache read tokens separately in instrumentation spans.
  • Tests

    • Added comprehensive test coverage validating cache token tracking across streaming and asynchronous operations.

✏️ Tip: You can customize this high-level summary in your review settings.

@dinmukhamedm dinmukhamedm requested a review from nirga January 31, 2026 16:19
Copy link
Contributor

@ellipsis-dev ellipsis-dev bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Important

Looks good to me! 👍

Reviewed everything up to daa6ff5 in 11 seconds. Click for details.
  • Reviewed 459 lines of code in 3 files
  • Skipped 0 files when reviewing.
  • Skipped posting 0 draft comments. View those below.
  • Modify your settings and rules to customize what types of comments Ellipsis leaves. And don't forget to react with 👍 or 👎 to teach Ellipsis.

Workflow ID: wflow_ZIhOqCTqy5KUUHF1

You can customize Ellipsis by changing your verbosity settings, reacting with 👍 or 👎, replying to comments, or adding code review rules.

@coderabbitai
Copy link

coderabbitai bot commented Jan 31, 2026

📝 Walkthrough

Walkthrough

This PR restores cache-related token usage attributes in Anthropic instrumentation by renaming attribute keys from GEN_AI_USAGE_* to LLM_USAGE_* format, adding span attribute assignments for cache metrics in streaming operations, and adding comprehensive test coverage validating cache token separation between creation and read spans.

Changes

Cohort / File(s) Summary
Attribute Key Renaming
packages/opentelemetry-instrumentation-anthropic/opentelemetry/instrumentation/anthropic/__init__.py
Renamed attribute key constants from GEN_AI_USAGE_CACHE_READ_INPUT_TOKENS and GEN_AI_USAGE_CACHE_CREATION_INPUT_TOKENS to LLM_USAGE_CACHE_READ_INPUT_TOKENS and LLM_USAGE_CACHE_CREATION_INPUT_TOKENS across two token usage methods.
Streaming Metrics Addition
packages/opentelemetry-instrumentation-anthropic/opentelemetry/instrumentation/anthropic/streaming.py
Added span attribute assignments in _set_token_usage to record cache_read_input_tokens and cache_creation_input_tokens using the updated attribute keys.
Test Coverage
packages/opentelemetry-instrumentation-anthropic/tests/test_prompt_caching.py
Expanded test assertions to validate cache token separation between creation and read spans across multiple test scenarios (legacy, with-content, with-no-content, stream, and async variants), including exact numeric value verification and read/write behavior assertions.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~15 minutes

Poem

🐰 Tokens cached with care so bright,
Renamed keys in telemetry's light,
Creation reads, reads read again,
Metrics flow through span and den! ✨

🚥 Pre-merge checks | ✅ 4 | ❌ 1
❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately describes the main change: restoring accidentally lost cache token attributes in Anthropic instrumentation.
Linked Issues check ✅ Passed The PR fully addresses issue #3647 by restoring cache_read_input_tokens and cache_creation_input_tokens attributes across Anthropic instrumentation files with corresponding test coverage.
Out of Scope Changes check ✅ Passed All changes are scoped to restoring cache token attributes in Anthropic instrumentation; no unrelated modifications detected.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

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.

🐛 Bug Report: cache read and cache creation input tokens aren't recorded on span attributes

1 participant