Python: Fix Python OTel usage detail attributes#6493
Open
eavanvalkenburg wants to merge 2 commits into
Open
Conversation
Map cached/read/reasoning usage detail fields to standard OTel GenAI attributes while preserving provider-specific legacy keys. Add focused coverage for direct response spans, aggregated agent spans, and provider usage parsing. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Contributor
Contributor
There was a problem hiding this comment.
Pull request overview
This PR fixes Python OTel GenAI span usage attributes by mapping additional provider-reported token usage details (cache create/read and reasoning tokens) onto the corresponding OTel semantic-convention attributes, while preserving legacy/provider-specific usage keys for compatibility.
Changes:
- Add OTel GenAI usage attributes for cache creation/read input tokens and reasoning output tokens, and map known
UsageDetailskeys (including legacy OpenAI/Anthropic keys) onto them. - Extend OpenAI and Anthropic usage parsing to emit standard generic usage keys in addition to existing provider-specific keys, preserving zero values.
- Add/extend unit tests to validate the new mapping behavior for both direct chat spans and aggregated
invoke_agentspans.
Reviewed changes
Copilot reviewed 9 out of 9 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| python/packages/core/agent_framework/observability.py | Adds new OTel usage attributes and central mapping logic from UsageDetails keys to OTel span attributes (including legacy/provider keys). |
| python/packages/core/agent_framework/_types.py | Extends UsageDetails with standard fields for cache and reasoning token counts. |
| python/packages/core/tests/core/test_observability.py | Adds coverage for additional usage mapping, legacy key mapping, capture flags, and invoke_agent aggregation. |
| python/packages/openai/agent_framework_openai/_chat_completion_client.py | Emits standard mapped keys for cached and reasoning tokens (preserving zeros) for Chat Completions usage parsing. |
| python/packages/openai/agent_framework_openai/_chat_client.py | Emits standard mapped keys for cached and reasoning tokens for Responses usage parsing. |
| python/packages/openai/tests/openai/test_openai_chat_completion_client.py | Adds test ensuring OpenAI Chat Completions parsing includes both legacy and standard mapped usage detail keys (including zeros). |
| python/packages/openai/tests/openai/test_openai_chat_client.py | Extends tests to assert new standard mapped keys and that zero values are preserved. |
| python/packages/anthropic/agent_framework_anthropic/_chat_client.py | Emits standard mapped cache token keys alongside existing Anthropic-specific keys, preserving zeros. |
| python/packages/anthropic/tests/test_anthropic_client.py | Adds tests for Anthropic parsing including standard mapped keys and preservation of zero cache tokens. |
Omit missing OpenAI Responses usage detail counts while preserving zero-valued counts. Record zero-valued token usage in OTel histograms and add regression coverage. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation and Context
Closes #5511.
This replaces and supersedes #6194 with a smaller, focused change. Python observability currently emits only input/output token counts on OTel spans and drops cache/read/reasoning usage details that providers already expose.
Description
This change maps known
UsageDetailstoken fields to the exact OTel GenAI semantic-convention attributes:gen_ai.usage.cache_creation.input_tokensgen_ai.usage.cache_read.input_tokensgen_ai.usage.reasoning.output_tokensIt adds standard generic usage keys for OpenAI and Anthropic parsing while preserving the existing provider-specific keys for compatibility. The same mapping is used for direct chat spans and aggregated
invoke_agentspans, and zero-valued mapped token counts are preserved.Thanks to @hanhan761 for the original investigation and implementation work in #6194.
Contribution Checklist