Skip to content

Refactor Anthropic on_response() to use shared get_usage_attributes()#1847

Merged
alexmojaki merged 23 commits intomainfrom
alex/anthropic-streaming-usage
Apr 9, 2026
Merged

Refactor Anthropic on_response() to use shared get_usage_attributes()#1847
alexmojaki merged 23 commits intomainfrom
alex/anthropic-streaming-usage

Conversation

@alexmojaki
Copy link
Copy Markdown
Collaborator

Summary

  • Extracts get_anthropic_usage_attributes() in anthropic.py, parallel to OpenAI's get_openai_usage_attributes()
  • Refactors on_response() to use it, replacing inline token-setting and cost calculation
  • Adds gen_ai.usage.raw to non-streaming Anthropic spans (previously missing)
  • Adds unit tests for the new function (basic, cache tokens, usage=None)

Phase 3a of the GenAI usage tracking work (phase 1: #1843, phase 2: #1846).

Test plan

  • All 15 usage attribute unit tests pass
  • All 24 Anthropic integration tests pass (snapshots updated)
  • Pyright clean on modified files

🤖 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: 4fdfeff
Status: ✅  Deploy successful!
Preview URL: https://62c27601.logfire-docs.pages.dev
Branch Preview URL: https://alex-anthropic-streaming-usa.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 4 additional findings.

Open in Devin Review

@alexmojaki alexmojaki enabled auto-merge (squash) April 9, 2026 09:46
@alexmojaki alexmojaki merged commit 2f7590f into main Apr 9, 2026
17 checks passed
@alexmojaki alexmojaki deleted the alex/anthropic-streaming-usage branch April 9, 2026 09:51
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.

1 participant