Repository navigation
Add usage attributes to Anthropic streaming spans - #1850
Merged
Merged
Conversation
… scope, model_dump issue
…ve broad try/except
… move, no behavior change
…ing-usage # Conflicts: # tests/otel_integrations/test_llm_usage_attributes.py
…c-streaming-usage-streaming
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
Deploying logfire-docs with
|
| 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 |
alexmojaki
commented
Apr 9, 2026
alexmojaki
commented
Apr 9, 2026
alexmojaki
commented
Apr 9, 2026
alexmojaki
commented
Apr 9, 2026
…tics, beta handling, convert_response_to_semconv
…re caught by try/except
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
gen_ai.usage.input_tokens,gen_ai.usage.output_tokens,gen_ai.usage.raw, andoperation.cost— matching the non-streaming pathaccumulate_event()to build a completeMessagefrom raw stream events, ensuring final accurate usage (including server-side tool use)content_from_messages()) with reading from the accumulated message's content blocksget_anthropic_usage_attributes()for both streaming and non-streaming paths — no separate streaming-specific usage extractionDepends on #1847 (
alex/anthropic-streaming-usage).Test plan
🤖 Generated with Claude Code