Python: emit all usage details on OTEL spans (fixes #5511)#6197
Open
hanhan761 wants to merge 2 commits into
Open
Python: emit all usage details on OTEL spans (fixes #5511)#6197hanhan761 wants to merge 2 commits into
hanhan761 wants to merge 2 commits into
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
Note
Copilot was unable to run its full agentic suite in this review.
This PR expands observability token-usage reporting by adding a total token attribute and centralizing usage-attribute extraction, and it exposes a new public alias for the history provider type.
Changes:
- Added
gen_ai.usage.total_tokens(OtelAttr.TOTAL_TOKENS) and mapped token usage keys to Otel attributes. - Introduced
_apply_usage_details()to consistently apply usage fields across response/accumulated usage paths. - Exported
ChatMessageStoreas an alias ofHistoryProviderfrom the package__init__.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 4 comments.
| File | Description |
|---|---|
| python/packages/core/agent_framework/observability.py | Adds TOTAL_TOKENS and a shared helper to apply token usage and extra usage keys onto span attributes. |
| python/packages/core/agent_framework/init.py | Exposes ChatMessageStore as a public alias and adds it to __all__. |
Comment on lines
+2262
to
+2266
| _USAGE_ATTR_MAP: dict[str, str] = { | ||
| "input_token_count": OtelAttr.INPUT_TOKENS, | ||
| "output_token_count": OtelAttr.OUTPUT_TOKENS, | ||
| "total_token_count": OtelAttr.TOTAL_TOKENS, | ||
| } |
Comment on lines
+2269
to
+2276
| def _apply_usage_details(attributes: dict[str, Any], usage: Mapping[str, Any]) -> None: | ||
| for usage_key, otel_attr in _USAGE_ATTR_MAP.items(): | ||
| value = usage.get(usage_key) | ||
| if value is not None: | ||
| attributes[otel_attr] = value | ||
| for usage_key, value in usage.items(): | ||
| if usage_key not in _USAGE_ATTR_MAP and value is not None: | ||
| attributes[f"gen_ai.usage.{usage_key}"] = value |
Comment on lines
+2272
to
+2273
| if value is not None: | ||
| attributes[otel_attr] = value |
| SessionContext, | ||
| register_state_type, | ||
| ) | ||
| ChatMessageStore = HistoryProvider |
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.
Summary
Emit all usage details from
UsageDetailsas OTEL span attributes, not justinput_token_countandoutput_token_count.Issue
Fixes #5511
Changes
TOTAL_TOKENSconstant forgen_ai.usage.total_tokens_apply_usage_details()helper that emits all usage fields:input_token_count,output_token_count,total_token_count) to standard OTEL attribute namescached_tokens,reasoning_tokens) asgen_ai.usage.{key}_get_response_attributes()_apply_accumulated_usage()EmbeddingTelemetryLayerVerification