Skip to content

Conversation

@clutchski
Copy link
Collaborator

@clutchski clutchski commented Feb 9, 2026

Summary

  • Implement sub-agent span nesting in the Claude Agent SDK wrapper using parent_tool_use_id from SDK messages to detect sub-agent boundaries
  • Sub-agent LLM calls and tool calls now appear nested under their agent's TASK span instead of flat under the root
  • Support concurrent sub-agents (each gets its own span keyed by parent_tool_use_id)
  • Fix race condition where createLLMSpan could lose parent reference after PostToolUse hook fired

Test plan

  • Integration test: real SDK sub-agent with calculator tool verifies nested trace hierarchy (LLM spans, tool spans parented correctly)
  • All 17 tests pass (npx vitest run --config src/wrappers/claude-agent-sdk/vitest.config.js)
  • Build passes (pnpm build)

🤖 Generated with Claude Code

clutchski and others added 2 commits February 9, 2026 12:19
Keep sub-agent span references in subAgentSpans map after PostToolUse
ends them, so createLLMSpan can still resolve the correct parent when
flushing the sub-agent's message batch. Add real SDK integration test
that verifies sub-agent LLM and tool spans are nested correctly.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@clutchski clutchski marked this pull request as ready for review February 9, 2026 18:42
@clutchski clutchski changed the title handle sub agents in claude agent sdk Add sub-agent nesting for Claude Agent SDK wrapper Feb 9, 2026
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@clutchski clutchski requested review from cpinn and knjiang February 9, 2026 21:17
Track ended sub-agent spans in a Set so the finally block skips
already-closed spans. Remove lenient guard on tool span assertion
so the test fails if the sub-agent doesn't use the calculator.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
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.

2 participants