[opentelemetry-instrumentation-genai-anthropic] Record cache token usage via the shared invocation fields - #641
Conversation
7806cc9 to
441736a
Compare
There was a problem hiding this comment.
🟡 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 populateInferenceInvocation.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
GenAIAttributesconstants; 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.
Pull request dashboard statusWaiting on reviewers · refreshed 2026-09-07 01:32 UTC Review the latest changes. Status above doesn't look right?
|
…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
441736a to
be0d362
Compare
10f354d
Description
messages_extractorsdefined its owngen_ai.usage.cache_creation.input_tokensandgen_ai.usage.cache_read.input_tokensstring constants and wrote them straight intoinvocation.attributes, so the attribute names were pinned in this package rather than tracking the semantic conventions.InferenceInvocationalready carriescache_creation_input_tokensandcache_read_input_tokensfields 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
How has this been tested?
The existing cache token tests now assert against the semconv constants rather than the removed local ones.
anthropicsuite: 178 passed (latest), 171 passed with 7 skipped (oldest)anthropicconformance: 5 passedtox -e precommitandtox -e typecheckcleanChecklist