Skip to content

[opentelemetry-instrumentation-genai-anthropic] Record cache token usage via the shared invocation fields - #641

Merged
lmolkova merged 2 commits into
open-telemetry:mainfrom
Krishnachaitanyakc:anthropic-cache-tokens-via-invocation-fields
Sep 9, 2026
Merged

[opentelemetry-instrumentation-genai-anthropic] Record cache token usage via the shared invocation fields#641
lmolkova merged 2 commits into
open-telemetry:mainfrom
Krishnachaitanyakc:anthropic-cache-tokens-via-invocation-fields

Conversation

@Krishnachaitanyakc

Copy link
Copy Markdown
Contributor

Description

messages_extractors defined its own gen_ai.usage.cache_creation.input_tokens and gen_ai.usage.cache_read.input_tokens string constants and wrote them straight into invocation.attributes, so the attribute names were pinned in this package rather than tracking the semantic conventions. InferenceInvocation already carries cache_creation_input_tokens and cache_read_input_tokens fields for exactly this, and openai uses them (response_extractors.py:661).

This sets those fields instead and drops the two local constants. The emitted attributes are unchanged.

Type of change

  • Bug fix (non-breaking change which fixes an issue)

How has this been tested?

The existing cache token tests now assert against the semconv constants rather than the removed local ones.

  • anthropic suite: 178 passed (latest), 171 passed with 7 skipped (oldest)
  • anthropic conformance: 5 passed
  • tox -e precommit and tox -e typecheck clean

Checklist

  • Followed the style guidelines of this project
  • Changelog updated if the change requires an entry
  • Unit tests updated

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

It removes previously importable module-level constants in messages_extractors.py, which is potentially a breaking change for downstream imports unless kept as backward-compatible aliases.

Once you've addressed the issues Copilot identified, you can request another Copilot review.

Pull request overview

Updates the Anthropic GenAI instrumentation to record cache token usage via the shared InferenceInvocation fields (instead of package-local attribute-name string constants), aligning emission with semantic conventions while keeping emitted attribute names the same.

Changes:

  • Switch messages_extractors.set_invocation_response_attributes() to populate InferenceInvocation.cache_creation_input_tokens / cache_read_input_tokens.
  • Remove the locally defined cache-usage attribute name constants from messages_extractors.py.
  • Update sync/async messages tests to assert using semconv GenAIAttributes constants; add a changelog fragment.
File summaries
File Description
instrumentation/opentelemetry-instrumentation-genai-anthropic/tests/test_sync_messages.py Updates cache token assertions to use semconv GenAIAttributes constants.
instrumentation/opentelemetry-instrumentation-genai-anthropic/tests/test_async_messages.py Updates cache token assertions to use semconv GenAIAttributes constants and removes old constant imports.
instrumentation/opentelemetry-instrumentation-genai-anthropic/src/opentelemetry/instrumentation/genai/anthropic/messages_extractors.py Sets cache token usage via shared invocation fields; removes local attribute-name constants.
instrumentation/opentelemetry-instrumentation-genai-anthropic/.changelog/641.fixed Documents the behavior/implementation change.
Review details
  • Files reviewed: 4/4 changed files
  • Comments generated: 1
  • Review effort level: Lite

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

@opentelemetry-pr-dashboard

opentelemetry-pr-dashboard Bot commented Sep 7, 2026

Copy link
Copy Markdown

Pull request dashboard status

Waiting on reviewers · refreshed 2026-09-07 01:32 UTC

Review the latest changes.

Status above doesn't look right?
  • Just replied or pushed? Anything around or after the refresh time above may not be picked up yet — give it a few minutes.
  • Anything look wrong? Report it with what you expected; it helps us improve the dashboard.

…age via the shared invocation fields

Set cache_creation_input_tokens / cache_read_input_tokens on the invocation
instead of writing locally defined attribute-name constants into
invocation.attributes, matching how the openai package reports the same
values. The emitted attributes are unchanged.

Assisted-by: Claude Opus 5
@Krishnachaitanyakc
Krishnachaitanyakc force-pushed the anthropic-cache-tokens-via-invocation-fields branch from 441736a to be0d362 Compare September 8, 2026 21:24
@lmolkova lmolkova added the Skip Changelog PR does not require a changelog entry label Sep 8, 2026
@lmolkova
lmolkova added this pull request to the merge queue Sep 9, 2026
Merged via the queue into open-telemetry:main with commit 10f354d Sep 9, 2026
74 of 75 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Skip Changelog PR does not require a changelog entry

Development

Successfully merging this pull request may close these issues.

3 participants