Skip to content

fix(weave): trace Claude Agent SDK async-iterable queries per turn - #7665

Draft
rgao-coreweave wants to merge 1 commit into
masterfrom
rgao/claude-agent-sdk-python-multi-turn
Draft

fix(weave): trace Claude Agent SDK async-iterable queries per turn#7665
rgao-coreweave wants to merge 1 commit into
masterfrom
rgao/claude-agent-sdk-python-multi-turn

Conversation

@rgao-coreweave

@rgao-coreweave rgao-coreweave commented Jul 29, 2026

Copy link
Copy Markdown
Contributor

Problem

The Claude Agent SDK accepts either a string or an AsyncIterable as the prompt to query(). An async-iterable prompt can submit multiple inputs and produce a separate ResultMessage for each one.

The OTel integration previously treated the entire output iterator as one turn. On current master, a replay containing two submitted prompts and two results produces only one invoke_agent span, collapsing both interactions together.

Approach

Keep the ordinary one-result APIs simple and isolate multi-result coordination to the only path that needs it:

SDK API Tracing path Turn boundary
query(prompt=str) Linear single-turn tracer The first ResultMessage
query(prompt=AsyncIterable) Async-iterable FIFO tracer Each ResultMessage
ClaudeSDKClient.receive_response() Linear single-turn tracer The method stops after its first ResultMessage

Both paths share message processing and turn finalization. The async-iterable path additionally:

  • Captures user inputs as the SDK consumes them without pre-consuming or cloning the iterable.
  • Buffers messages marked shouldQuery: false into the next query-triggering input.
  • Pairs submitted inputs with results in FIFO order.
  • Reuses the SDK session ID as the conversation ID while keeping each turn in its own trace.
  • Records a pending input as an errored turn when transport or parsing fails before its result.
  • Leaves a completed turn successful if teardown fails after its terminal result.

String prompts and the public ClaudeSDKClient.receive_response() contract remain on the straightforward single-turn implementation.

Example

Two-turn async-iterable query in Weave records remembered. and CERULEAN-42 as two turns in one conversation.

Testing

Case Expected tracing contract
String prompt, one result One successful turn
Async-iterable prompt, one input/result Same span payload as the string-prompt path
Async-iterable prompt, two inputs/results Two FIFO-paired turns in one conversation
Async-iterable input with shouldQuery: false Buffered into the next query-triggering turn
String or async-iterable failure before the first response One errored turn containing the submitted input
Async-iterable failure between results Completed turn stays successful; pending turn is errored
String or async-iterable failure after the final result Completed turn stays successful; no phantom turn
ClaudeSDKClient with string or async-iterable input One turn per receive_response()
  • Claude Agent SDK integration suite: 34 passed on Python 3.10.
  • Claude Agent SDK integration suite: 34 passed on Python 3.13.
  • Ruff 0.15.5 check and format verification passed.
  • GitHub CI: 105 passed checks and 3 intentionally skipped checks.

Breaking changes

None.

rgao-coreweave commented Jul 29, 2026

Copy link
Copy Markdown
Contributor Author

Base automatically changed from rgao/claude-agent-sdk-python-weave-genai to master July 29, 2026 22:18
@rgao-coreweave
rgao-coreweave force-pushed the rgao/claude-agent-sdk-python-multi-turn branch from ce65f6a to 041645e Compare July 30, 2026 03:27
@rgao-coreweave rgao-coreweave changed the title fix(weave): trace Claude Agent SDK multi-turn queries fix(weave): trace each Claude Agent SDK streaming turn Jul 30, 2026
@w-b-hivemind

w-b-hivemind Bot commented Jul 30, 2026

Copy link
Copy Markdown

HiveMind Sessions

1 session · 11m · $6.15

Session Agent Duration Tokens Cost Lines
Update PR Descriptions and Sync Commits
e4ab1870-5f94-4b10-8abf-1d259fd41c55
claude 11m 32.6K $6.15 +169 -88
Total 11m 32.6K $6.15 +169 -88

View all sessions in HiveMind →

Run claude --resume e4ab1870-5f94-4b10-8abf-1d259fd41c55 to pickup where you left off.

@codecov

codecov Bot commented Jul 30, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 85.71429% with 15 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
.../integrations/claude_agent_sdk/otel_integration.py 85.71% 12 Missing and 3 partials ⚠️

📢 Thoughts on this report? Let us know!

@rgao-coreweave
rgao-coreweave marked this pull request as ready for review July 30, 2026 05:33
@rgao-coreweave
rgao-coreweave requested a review from a team as a code owner July 30, 2026 05:33
@rgao-coreweave rgao-coreweave changed the title fix(weave): trace each Claude Agent SDK streaming turn fix(weave): trace Claude Agent SDK multi-turn Jul 30, 2026
@rgao-coreweave
rgao-coreweave force-pushed the rgao/claude-agent-sdk-python-multi-turn branch from 041645e to 9fef0da Compare July 31, 2026 18:50
@rgao-coreweave
rgao-coreweave marked this pull request as draft July 31, 2026 22:43
@rgao-coreweave
rgao-coreweave force-pushed the rgao/claude-agent-sdk-python-multi-turn branch from 9fef0da to 5e11bc2 Compare July 31, 2026 23:00
@rgao-coreweave rgao-coreweave changed the title fix(weave): trace Claude Agent SDK multi-turn fix(weave): trace Claude Agent SDK async-iterable queries per turn Jul 31, 2026
@rgao-coreweave
rgao-coreweave force-pushed the rgao/claude-agent-sdk-python-multi-turn branch from 5e11bc2 to 3548601 Compare July 31, 2026 23:45
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