Skip to content

fix(claude-agent-sdk): group inference spans by response ID - #301

Open
RichardoMrMu wants to merge 3 commits into
alibaba:mainfrom
RichardoMrMu:fix/claude-agent-response-spans-293
Open

RichardoMrMu wants to merge 3 commits into
alibaba:mainfrom
RichardoMrMu:fix/claude-agent-response-spans-293

Conversation

@RichardoMrMu

Copy link
Copy Markdown
Collaborator

Description

Refs #293.

With SDK versions that expose AssistantMessage.message_id, group fragments of the same model response into one chat span. That includes tool-call-only responses. Record the response ID, capture thinking as reasoning without the signature, and use the response's own token usage. Keep the run-wide total on the agent span. The span and duration metric end at the last response fragment, not when tools finish. Messages without response IDs retain the legacy grouping.

The response-ID path is enabled automatically when IDs are available; there is no new setting. @ralf0131, thanks for reproducing this. Could you review the response grouping and the fallback for older SDKs?

Verification

  • Ran the issue's offline stream reproducer on main and the patch with Python 3.12 and Claude Agent SDK 0.1.80. Main exported 1 chat span with 300 input / 126 output tokens. The patch exported 3 chat spans with 100 / 42 each and kept 300 / 126 on the agent span.
  • Claude Agent SDK 0.1.80: 35 tests passed, including split tool calls, missing usage, early stream closure, and tool wait exclusion from chat duration.
  • Claude Agent SDK 0.1.50 (no response-ID field): 25 tests passed, 5 response-ID-only tests skipped.
  • Shared GenAI handler and metrics: 117 tests passed.

These were offline tests; no live Claude API call was made. No OTel core repository change is required.

@RichardoMrMu
RichardoMrMu marked this pull request as ready for review November 30, 2026 06:16

This branch has not been deployed

No deployments
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.

4 participants