Skip to content

Python: include all usage detail fields in OTEL span attributes (fixes #5511)#6194

Open
hanhan761 wants to merge 1 commit into
microsoft:mainfrom
hanhan761:fix-5511-usage-details-otel
Open

Python: include all usage detail fields in OTEL span attributes (fixes #5511)#6194
hanhan761 wants to merge 1 commit into
microsoft:mainfrom
hanhan761:fix-5511-usage-details-otel

Conversation

@hanhan761
Copy link
Copy Markdown

Summary

_build_response_attributes()\ in \observability.py\ only extracts \input_token_count\ and \output_token_count\ from
esponse.usage_details. Any additional token counts set by providers (e.g. \cached_input_tokens,
easoning_tokens) are silently dropped from OTEL span attributes.

Changes

Replaced the hardcoded extraction of \input_token_count/\output_token_count\ with a loop over all integer-valued keys in \usage_details:

  • Standard fields (\input_token_count, \output_token_count) use the existing gen_ai semantic convention attribute names
  • Any other integer fields (e.g. \cached_input_tokens,
    easoning_tokens) are set as \gen_ai.usage.\

Related

Fixes #5511

Copilot AI review requested due to automatic review settings May 30, 2026 07:11
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Note

Copilot was unable to run its full agentic suite in this review.

Extends response usage attribute capture to include all integer usage metrics beyond just input/output tokens, emitting them as gen_ai.usage.* OpenTelemetry attributes.

Changes:

  • Iterates over all usage_details items instead of only reading input_token_count and output_token_count.
  • Maps known keys to existing OtelAttr.INPUT_TOKENS / OtelAttr.OUTPUT_TOKENS attributes.
  • Emits any additional integer usage fields as gen_ai.usage.<key> attributes.

Comment on lines +2297 to +2305
for key, value in usage.items():
if not isinstance(value, int):
continue
if key == "input_token_count":
attributes[OtelAttr.INPUT_TOKENS] = value
elif key == "output_token_count":
attributes[OtelAttr.OUTPUT_TOKENS] = value
else:
attributes[f"gen_ai.usage.{key}"] = value
Comment on lines +2297 to +2299
for key, value in usage.items():
if not isinstance(value, int):
continue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Python: [Bug]: missing cached tokens on OTEL spans

3 participants