Subagents in the SDK changedagent-sdk/subagents
Nearest release: v2.1.295, published 4 hours before upstream edited the page. Shown because the two are within 24 hours of each other. Nothing here says the release caused the edit.
Upstream edited this page at 8 Oct 2026 22:59 UTC, give or take a minute or two: the time comes from Anthropic’s own sitemap rather than from a commit. This site recorded the change at 8 Oct 2026 23:07 UTC.
Upstream edited
Recorded here
Lines+9added
Lines−2removed
From line
295
where the diff opens
First seen
14 Aug 2026
this site's first read of the page
Recorded edits19to this page, all time
The whole hunk
from line 295, old and new numbered
/
from line 295
295295
296296## Detect subagent invocation
297297
298Claude invokes subagents through the Agent tool. To detect when a subagent is invoked, check for `tool_use` blocks where `name` is `"Agent"`. Messages from within a subagent's context include a `parent_tool_use_id` field.
298Claude invokes subagents through the Agent tool. To detect when a subagent is invoked, check for `tool_use` blocks where `name` is `"Agent"`.
299299
300Messages from within a subagent's context include a `parent_tool_use_id` field. In TypeScript, each assistant and user message a subagent produces also carries [`agent_id`](/docs/en/agent-sdk/typescript#sdkassistantmessage): the `task_id` of that subagent's [task events](/docs/en/agent-sdk/typescript#sdktaskstartedmessage). `agent_id` requires TypeScript Agent SDK v0.3.292 or later.
301
300302<Note>
301303 The tool appears as `"Agent"` in `tool_use` blocks but as `"Task"` in the `system:init` tools list. Before Claude Code v2.1.63, `tool_use` blocks also named it `"Task"`. To keep detection working across SDK versions, match both values in `block.name`.
302304</Note>
from line 305
303305
304306The message structure differs between SDKs. In Python, you access content blocks directly via `message.content`. In TypeScript, `SDKAssistantMessage` wraps the Claude API message, so you access content via `message.message.content`.
305307
306This example iterates through streamed messages, logging when a subagent is invoked and when subsequent messages originate from within that subagent's execution context.
308This example iterates through streamed messages, logging when a subagent is invoked and when subsequent messages originate from within that subagent's execution context. The TypeScript version also logs the `agent_id` of each subagent message that carries one.
307309
308310<CodeGroup>
309311 ```python Python theme={null}
from line 377
375377 // Check if this message is from within a subagent's context
376378 if (msg.parent_tool_use_id) {
377379 console.log(" (running inside subagent)");
380 // On assistant and user messages, agent_id matches the task_id
381 // on that subagent's task_started and other task events
382 if (msg.agent_id) {
383 console.log(` agent_id: ${msg.agent_id}`);
384 }
378385 }
379386
380387 if ("result" in message) {
No line in this hunk matches that.