Skip to content

Add usage attributes to Anthropic streaming spans - #1850

Merged
alexmojaki merged 49 commits into
mainfrom
alex/anthropic-streaming-usage-streaming
Apr 9, 2026
Merged

alexmojaki merged 49 commits into
mainfrom
alex/anthropic-streaming-usage-streaming

Conversation

@alexmojaki

Copy link
Copy Markdown
Contributor

Summary

  • Streaming Anthropic spans now emit gen_ai.usage.input_tokens, gen_ai.usage.output_tokens, gen_ai.usage.raw, and operation.cost — matching the non-streaming path
  • Uses the Anthropic SDK's accumulate_event() to build a complete Message from raw stream events, ensuring final accurate usage (including server-side tool use)
  • Replaces manual text delta accumulation (content_from_messages()) with reading from the accumulated message's content blocks
  • Reuses get_anthropic_usage_attributes() for both streaming and non-streaming paths — no separate streaming-specific usage extraction

Depends on #1847 (alex/anthropic-streaming-usage).

Test plan

  • All 5 existing streaming tests updated with new usage attribute snapshots
  • All 22 Anthropic tests pass
  • Pyright passes with 0 errors
  • CI passes

🤖 Generated with Claude Code

…ing-usage

# Conflicts:
#	tests/otel_integrations/test_llm_usage_attributes.py
The original approach (manually capturing message_start and message_delta
separately) produced stale usage data for cost calculation — output_tokens
and server_tool_use could change during streaming. The Anthropic SDK's
accumulate_event() builds a complete Message with final accurate usage,
enabling reuse of get_anthropic_usage_attributes() for both paths.
… spans

Replace manual chunk text extraction with Anthropic's accumulate_event(),
which builds a complete message snapshot. This gives streaming spans access
to usage data (input_tokens, output_tokens, cost) via get_anthropic_usage_attributes().

- Replace _content list with _message (ParsedMessage) in AnthropicMessageStreamState
- Use accumulate_event() in record_chunk() instead of content_from_messages()
- Read text from accumulated message content blocks in get_response_data()
- Add usage attributes to streaming get_attributes()
- Remove unused content_from_messages() and TextDelta/BetaTextDelta imports
@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented Apr 9, 2026 •

Copy link
Copy Markdown

Deploying logfire-docs with  Cloudflare Pages  Cloudflare Pages

Latest commit: 847fcbf
Status: ✅  Deploy successful!
Preview URL: https://b1c9f952.logfire-docs.pages.dev
Branch Preview URL: https://alex-anthropic-streaming-usa-t4s0.logfire-docs.pages.dev

View logs

devin-ai-integration[bot]

This comment was marked as resolved.

Comment thread specs/anthropic-streaming-usage/plan.md Resolved
devin-ai-integration[bot]

This comment was marked as resolved.

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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Devin Review found 1 new potential issue.

View 7 additional findings in Devin Review.

Open in Devin Review

@alexmojaki
alexmojaki merged commit 73678d7 into main Apr 9, 2026
20 checks passed
@alexmojaki
alexmojaki deleted the alex/anthropic-streaming-usage-streaming branch April 9, 2026 13:20
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