Skip to content

Add usage attributes to OpenAI streaming spans#1846

Merged
alexmojaki merged 13 commits intomainfrom
alex/openai-streaming-usage
Apr 8, 2026
Merged

Add usage attributes to OpenAI streaming spans#1846
alexmojaki merged 13 commits intomainfrom
alex/openai-streaming-usage

Conversation

@alexmojaki
Copy link
Copy Markdown
Collaborator

Summary

  • Extracts get_openai_usage_attributes() shared function in openai.py to deduplicate usage extraction logic across streaming and non-streaming code paths
  • Adds gen_ai.usage.input_tokens, gen_ai.usage.output_tokens, gen_ai.usage.raw, and operation.cost to chat completions and Responses API streaming spans
  • Refactors on_response() to use the shared function (no behavior change)

Closes #1845

Test plan

  • Existing OpenAI tests pass (61 tests)
  • New unit tests for get_openai_usage_attributes() (4 tests)
  • Streaming tests with include_usage=True gain usage attributes in snapshots
  • Streaming tests without include_usage unchanged
  • Responses streaming test gains usage attributes

🤖 Generated with Claude Code

@cloudflare-workers-and-pages
Copy link
Copy Markdown

cloudflare-workers-and-pages bot commented Apr 8, 2026

Deploying logfire-docs with  Cloudflare Pages  Cloudflare Pages

Latest commit: 324b4e7
Status: ✅  Deploy successful!
Preview URL: https://a7563477.logfire-docs.pages.dev
Branch Preview URL: https://alex-openai-streaming-usage.logfire-docs.pages.dev

View logs

Copy link
Copy Markdown
Contributor

@devin-ai-integration devin-ai-integration bot left a comment

Choose a reason for hiding this comment

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

✅ Devin Review: No Issues Found

Devin Review analyzed this PR and found no potential bugs to report.

View in Devin Review to see 5 additional findings.

Open in Devin Review

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

DiskRetryer temp directories accumulate across process restarts, eventually exhausting /tmp

1 participant