modified
--- a/subagents.md +++ b/subagents.md @@ -129,12 +129,12 @@ ---|---|---|--- `description`| `string`| Yes| Natural language description of when to use this agent `prompt`| `string`| Yes| The agent’s system prompt defining its role and behavior -`tools`| `string[]`| No| Array of allowed tool names; listing one doesn’t [pre-approve](</docs/en/agent-sdk/permissions#allow-and-deny-rules>) it. If omitted, inherits every [tool available to subagents](</docs/en/sub-agents#available-tools>) +`tools`| `string[]`| No| Array of tool names available to this agent; listing one doesn’t [pre-approve](</docs/en/agent-sdk/permissions#allow-and-deny-rules>) it. If omitted, inherits every [tool available to subagents](</docs/en/sub-agents#available-tools>) `disallowedTools`| `string[]`| No| Array of tool names to remove from the agent’s tool set. MCP server-level patterns are also accepted: `mcp__server` or `mcp__server__*` removes every tool from that server, and `mcp__*` removes every MCP tool from any server `model`| `string`| No| Model override for this agent. Accepts an alias such as `'fable'`, `'opus'`, `'sonnet'`, `'haiku'`, `'inherit'`, or a full model ID. `'inherit'` uses the model of the agent that spawned this one. When you omit `model`, Claude Code picks the model in the [subagent model order](</docs/en/sub-agents#choose-a-model>) `skills`| `string[]`| No| List of skill names to preload into the agent’s context at startup. At most the [first 32 distinct names](</docs/en/sub-agents#preload-skills-into-subagents>) in the list are preloaded. Unlisted skills remain invocable through the Skill tool `memory`| `'user' | 'project' | 'local'`| No| Memory source for this agent -`mcpServers`| `(string | object)[]`| No| MCP servers available to this agent, by name or inline config +`mcpServers`| `(string | object)[]`| No| MCP servers to add to any that this agent inherits, by name or inline config `initialPrompt`| `string`| No| Auto-submitted as the first user turn when this agent runs as the main thread agent. Ignored when the agent is invoked as a subagent `maxTurns`| `number`| No| Maximum number of agentic turns before the agent stops. When the agent reaches the limit, Claude Code returns its output marked as partial, and you can resume the agent to continue. The partial marking requires Claude Code v2.1.246 or later `autoCompactWindow`| `number`| No| Lower the [auto-compact window](</docs/en/sub-agents#subagent-auto-compact-window>) for this agent when it runs as a subagent, to a token count of `100000` or more. Requires TypeScript Agent SDK v0.3.296 or later. The Python SDK’s [`AgentDefinition`](</docs/en/agent-sdk/python#agentdefinition>) doesn’t have this field @@ -507,7 +507,7 @@ Use the `tools` field to limit what a subagent can do: * **Omit`tools`**: the subagent gets every [tool available to subagents](</docs/en/sub-agents#available-tools>) - * **List tools** : the subagent gets those. With [`memory`](</docs/en/sub-agents#enable-persistent-memory>) enabled, it also gets Read, Write, and Edit. A code reviewer that shouldn’t edit files, for example, gets `["Read", "Grep", "Glob"]` + * **List tools** : the subagent gets those. With [`memory`](</docs/en/sub-agents#enable-persistent-memory>) enabled, it also gets Read, Write, and Edit. For a code reviewer that shouldn’t edit files, list `["Read", "Grep", "Glob"]` This example creates a read-only analysis agent that can examine code but can’t modify files or run commands. @@ -577,7 +577,7 @@ Limit| Set it with| Default| What Claude Code does at the limit ---|---|---|--- Depth| [`CLAUDE_CODE_MAX_SUBAGENT_SPAWN_DEPTH`](</docs/en/env-vars>)| `3` layers of subagents below your main agent. `1` stops your subagents from spawning any of their own| Leaves a subagent at the bottom layer unable to spawn, so it does its delegated work itself. See [nested subagents](</docs/en/sub-agents#let-subagents-spawn-their-own-subagents>) -Concurrency| [`CLAUDE_CODE_MAX_CONCURRENT_SUBAGENTS`](</docs/en/env-vars>)| `20` subagents running at once, counting every subagent Claude spawns with the Agent tool| Refuses to spawn another subagent, returning `Concurrent subagent limit reached`, until the running count drops below the limit. Sessions with [ultracode](</docs/en/model-config#adjust-effort-level>) active are never refused. See the [concurrent subagent limit](</docs/en/sub-agents#concurrent-subagent-limit>) +Concurrency| [`CLAUDE_CODE_MAX_CONCURRENT_SUBAGENTS`](</docs/en/env-vars>)| At least `20` subagents running at once, counting every subagent Claude spawns with the Agent tool| Refuses to spawn another subagent, returning `Concurrent subagent limit reached`, until the running count drops below the limit. Sessions with [ultracode](</docs/en/model-config#adjust-effort-level>) active are exempt. See the [concurrent subagent limit](</docs/en/sub-agents#concurrent-subagent-limit>) Spend| `maxBudgetUsd` in TypeScript, `max_budget_usd` in Python| No limit. Counts the call’s own spend, subagent requests included| Enforces the cap in three ways: refuses to spawn more subagents, returning `Budget limit reached`, stops background subagents that are still running, and ends the query with the `error_max_budget_usd` result subtype. For how the caps behave across a session, see [turns and budget](</docs/en/agent-sdk/agent-loop#turns-and-budget>) The two SDKs treat the `env` option differently: the TypeScript SDK replaces the subprocess environment with it, so spread `process.env` into it to keep variables like `PATH`, while the Python SDK merges it into the inherited environment. This example turns nesting off, allows at most five subagents at a time, and stops the query once the estimated spend reaches $5:
modified
--- a/subagents.md +++ b/subagents.md @@ -37,7 +37,7 @@ prompt="Review the authentication module for security issues", options=ClaudeAgentOptions( # Auto-approve these tools - allowed_tools=["Read", "Grep", "Glob", "Agent"], + allowed_tools=["Read", "Grep", "Glob", "Agent", "Bash(npm test *)"], agents={ "code-reviewer": AgentDefinition( # description tells Claude when to use this subagent @@ -83,7 +83,7 @@ prompt: "Review the authentication module for security issues", options: { // Auto-approve these tools - allowedTools: ["Read", "Grep", "Glob", "Agent"], + allowedTools: ["Read", "Grep", "Glob", "Agent", "Bash(npm test *)"], agents: { "code-reviewer": { // description tells Claude when to use this subagent @@ -129,7 +129,7 @@ ---|---|---|--- `description`| `string`| Yes| Natural language description of when to use this agent `prompt`| `string`| Yes| The agent’s system prompt defining its role and behavior -`tools`| `string[]`| No| Array of allowed tool names. If omitted, inherits every [tool available to subagents](</docs/en/sub-agents#available-tools>) +`tools`| `string[]`| No| Array of allowed tool names; listing one doesn’t [pre-approve](</docs/en/agent-sdk/permissions#allow-and-deny-rules>) it. If omitted, inherits every [tool available to subagents](</docs/en/sub-agents#available-tools>) `disallowedTools`| `string[]`| No| Array of tool names to remove from the agent’s tool set. MCP server-level patterns are also accepted: `mcp__server` or `mcp__server__*` removes every tool from that server, and `mcp__*` removes every MCP tool from any server `model`| `string`| No| Model override for this agent. Accepts an alias such as `'fable'`, `'opus'`, `'sonnet'`, `'haiku'`, `'inherit'`, or a full model ID. `'inherit'` uses the model of the agent that spawned this one. When you omit `model`, Claude Code picks the model in the [subagent model order](</docs/en/sub-agents#choose-a-model>) `skills`| `string[]`| No| List of skill names to preload into the agent’s context at startup. At most the [first 32 distinct names](</docs/en/sub-agents#preload-skills-into-subagents>) in the list are preloaded. Unlisted skills remain invocable through the Skill tool @@ -369,7 +369,11 @@ ## Resume subagents -You can resume a subagent to continue where it left off rather than starting fresh. A resumed subagent retains its full conversation history, including all previous tool calls, results, and reasoning. When a subagent stops at its `maxTurns` limit, Claude Code marks the output in the Agent tool result as partial, so Claude knows the run is unfinished. When a subagent completes, the Agent tool result includes a text block containing `agentId: <id>`. The built-in [`Explore` and `Plan` agents](</docs/en/sub-agents#built-in-subagents>) are one-shot and don’t return an `agentId`, so use a custom agent or `general-purpose` when you need to resume. To resume a subagent programmatically: +You can resume a subagent to continue where it left off rather than starting fresh. A resumed subagent retains its full conversation history, including all previous tool calls, results, and reasoning. When a subagent stops at its `maxTurns` limit, Claude Code marks the output in the Agent tool result as partial, so Claude knows the run is unfinished. + +### Resume a subagent from your code + +When a subagent completes, the Agent tool result includes a text block containing `agentId: <id>`. The built-in [`Explore` and `Plan` agents](</docs/en/sub-agents#built-in-subagents>) can leave the ID out of that text, so use a custom agent or `general-purpose` when you need to resume. To resume a subagent programmatically: 1. **Capture the session ID** : extract `session_id` from messages during the first query 2. **Extract the agent ID** : parse `agentId` from the Agent tool result text @@ -503,9 +507,9 @@ Use the `tools` field to limit what a subagent can do: * **Omit`tools`**: the subagent gets every [tool available to subagents](</docs/en/sub-agents#available-tools>) - * **List tools** : the subagent gets only those. A code reviewer that should never edit files, for example, gets `["Read", "Grep", "Glob"]` - -A tool you leave out isn’t in the subagent’s session at all: Claude works without it, with no permission prompt or error. This example creates a read-only analysis agent that can examine code but can’t modify files or run commands. + * **List tools** : the subagent gets those. With [`memory`](</docs/en/sub-agents#enable-persistent-memory>) enabled, it also gets Read, Write, and Edit. A code reviewer that shouldn’t edit files, for example, gets `["Read", "Grep", "Glob"]` + +This example creates a read-only analysis agent that can examine code but can’t modify files or run commands. Python
modified
--- a/subagents.md +++ b/subagents.md @@ -137,7 +137,7 @@ `mcpServers`| `(string | object)[]`| No| MCP servers available to this agent, by name or inline config `initialPrompt`| `string`| No| Auto-submitted as the first user turn when this agent runs as the main thread agent. Ignored when the agent is invoked as a subagent `maxTurns`| `number`| No| Maximum number of agentic turns before the agent stops. When the agent reaches the limit, Claude Code returns its output marked as partial, and you can resume the agent to continue. The partial marking requires Claude Code v2.1.246 or later -`autoCompactWindow`| `number`| No| Lower the [auto-compact window](</docs/en/sub-agents#subagent-auto-compact-window>) for this agent when it runs as a subagent, to a token count from `100000` to `1000000`. Requires TypeScript Agent SDK v0.3.296 or later. The Python SDK’s [`AgentDefinition`](</docs/en/agent-sdk/python#agentdefinition>) doesn’t have this field +`autoCompactWindow`| `number`| No| Lower the [auto-compact window](</docs/en/sub-agents#subagent-auto-compact-window>) for this agent when it runs as a subagent, to a token count of `100000` or more. Requires TypeScript Agent SDK v0.3.296 or later. The Python SDK’s [`AgentDefinition`](</docs/en/agent-sdk/python#agentdefinition>) doesn’t have this field `background`| `boolean`| No| Run this agent as a non-blocking background task when invoked `omitClaudeMd`| `boolean`| No| Leave the user, project, and local CLAUDE.md files out of this agent’s initial context; managed policy files still load. Ignored when the agent runs as the main thread agent. Requires TypeScript Agent SDK v0.3.271 or later. The Python SDK’s [`AgentDefinition`](</docs/en/agent-sdk/python#agentdefinition>) doesn’t have this field `effort`| `'low' | 'medium' | 'high' | 'xhigh' | 'max' | number`| No| Reasoning effort level for this agent
modified
--- a/subagents.md +++ b/subagents.md @@ -182,15 +182,13 @@ ### Automatic invocation -Claude automatically decides when to invoke subagents based on the task and each subagent’s `description`. For example, if you define a `performance-optimizer` subagent with the description “Performance optimization specialist for query tuning”, Claude will invoke it when your prompt mentions optimizing queries. Write clear, specific descriptions so Claude can match tasks to the right subagent. +Claude automatically decides when to invoke subagents based on the task and each subagent’s `description`. For example, a `performance-optimizer` subagent with the description “Performance optimization specialist for query tuning” fits a prompt about optimizing queries. Write clear, specific descriptions so Claude can match tasks to the right subagent. ### Explicit invocation -To guarantee Claude uses a specific subagent, mention it by name in your prompt: +To ask for a specific subagent, mention it by name in your prompt: "Use the code-reviewer agent to check the authentication module" - -This bypasses automatic matching and directly invokes the named subagent. ### Forked subagents
modified
--- a/subagents.md +++ b/subagents.md @@ -131,7 +131,7 @@ `prompt`| `string`| Yes| The agent’s system prompt defining its role and behavior `tools`| `string[]`| No| Array of allowed tool names. If omitted, inherits every [tool available to subagents](</docs/en/sub-agents#available-tools>) `disallowedTools`| `string[]`| No| Array of tool names to remove from the agent’s tool set. MCP server-level patterns are also accepted: `mcp__server` or `mcp__server__*` removes every tool from that server, and `mcp__*` removes every MCP tool from any server -`model`| `string`| No| Model override for this agent. Accepts an alias such as `'fable'`, `'opus'`, `'sonnet'`, `'haiku'`, `'inherit'`, or a full model ID. `'inherit'` uses the main model. When you omit it, Claude Code picks the model in the [subagent model order](</docs/en/sub-agents#choose-a-model>) +`model`| `string`| No| Model override for this agent. Accepts an alias such as `'fable'`, `'opus'`, `'sonnet'`, `'haiku'`, `'inherit'`, or a full model ID. `'inherit'` uses the model of the agent that spawned this one. When you omit `model`, Claude Code picks the model in the [subagent model order](</docs/en/sub-agents#choose-a-model>) `skills`| `string[]`| No| List of skill names to preload into the agent’s context at startup. At most the [first 32 distinct names](</docs/en/sub-agents#preload-skills-into-subagents>) in the list are preloaded. Unlisted skills remain invocable through the Skill tool `memory`| `'user' | 'project' | 'local'`| No| Memory source for this agent `mcpServers`| `(string | object)[]`| No| MCP servers available to this agent, by name or inline config
modified
Show diff (286 lines)
--- a/subagents.md +++ b/subagents.md @@ -1,10 +1,6 @@ Subagents are separate agent instances that your main agent can spawn to handle focused subtasks. Use them to isolate context, run multiple analyses in parallel, and apply specialized instructions without adding to the main agent’s prompt. -## - -​ - -Overview +## Overview You can create subagents in three ways: @@ -14,11 +10,7 @@ This guide focuses on the programmatic approach, which is recommended for SDK applications. -## - -​ - -Benefits of using subagents +## Benefits of using subagents Because subagents are separate agent instances, delegating work to them gives you four benefits: @@ -27,17 +19,9 @@ * **Specialized instructions and knowledge** : each subagent can have a tailored system prompt with specific expertise, best practices, and constraints. A `database-migration` subagent can have detailed knowledge about SQL best practices, rollback strategies, and data integrity checks that would be unnecessary noise in the main agent’s instructions. * **Tool restrictions** : subagents can be limited to specific tools, reducing the risk of unintended actions. A `doc-reviewer` subagent might only have access to Read and Grep tools, ensuring it can analyze but never accidentally modify your documentation files. -## - -​ - -Create subagents - -### - -​ - -Programmatic definition (recommended) +## Create subagents + +### Programmatic definition (recommended) Define subagents directly in your code using the `agents` parameter. Claude invokes subagents through the `Agent` tool. Most examples on this page print only the final result. To confirm that Claude delegated to a subagent rather than answering directly, see Detect subagent invocation. This example creates two subagents: a code reviewer with read-only access and a test runner that can execute commands. @@ -139,11 +123,7 @@ if ("result" in message) console.log(message.result); } -### - -​ - -AgentDefinition configuration +### AgentDefinition configuration Field| Type| Required| Description ---|---|---|--- @@ -165,32 +145,20 @@ In the Python SDK, multi-word field names such as `disallowedTools` and `mcpServers` keep their camelCase spelling to match the wire format rather than following Python’s snake_case convention. See the [`AgentDefinition` reference](</docs/en/agent-sdk/python#agentdefinition>) for details. -#### - -​ - -Choose whether Claude waits for subagents +#### Choose whether Claude waits for subagents By default, a subagent your main agent spawns [runs in the background](</docs/en/sub-agents#run-subagents-in-foreground-or-background>). The Agent tool result has `status: "async_launched"`, and the report arrives in a later turn. Claude waits when its next action needs the report: the call blocks and returns `status: "completed"`. To make that choice yourself: * **In your prompt** : ask Claude to wait, such as “run the test-runner agent and wait for it” * **In the definition** : set `background` to `true` to keep that agent in the background -### - -​ - -Filesystem-based definition (alternative) +### Filesystem-based definition (alternative) You can also define subagents as markdown files in `.claude/agents/` directories. See the [Claude Code subagents documentation](</docs/en/sub-agents>) for details on this approach. Programmatically defined agents take precedence over filesystem-based agents with the same name. When Claude calls the Agent tool without a `subagent_type`, it gets the built-in `general-purpose` subagent, which Claude can spawn even when you define no agents of your own. Setting [`CLAUDE_AGENT_SDK_DISABLE_BUILTIN_AGENTS=1`](</docs/en/env-vars>) removes that default, and such a call fails with [`subagent_type is required`](</docs/en/errors#subagent-type-is-required>). -## - -​ - -What subagents inherit +## What subagents inherit Unless the subagent is a [fork](</docs/en/sub-agents#fork-the-current-conversation>), its context window starts fresh, with no parent conversation, but isn’t empty. The only content you pass from parent to subagent is the Agent tool’s prompt string, so include any file paths, error messages, or decisions the subagent needs directly in that prompt. A subagent that has the [`SendMessage`](</docs/en/tools-reference>) tool starts with a list of the other named agents running in the session, so it knows which names it can send messages to. Claude Code adds the list to the subagent’s first turn automatically. A [fork](</docs/en/sub-agents#fork-the-current-conversation>) doesn’t get the list because it inherits the parent conversation instead. A subagent also inherits the main session’s extended thinking configuration. The table below lists what a non-fork subagent’s context contains and what it leaves out. @@ -210,25 +178,13 @@ An API error that ends the subagent early, such as a rate limit, is never delivered as its result. See [API errors in subagents](</docs/en/sub-agents#api-errors-in-subagents>) for the foreground and background behavior. -## - -​ - -Invoke subagents - -### - -​ - -Automatic invocation +## Invoke subagents + +### Automatic invocation Claude automatically decides when to invoke subagents based on the task and each subagent’s `description`. For example, if you define a `performance-optimizer` subagent with the description “Performance optimization specialist for query tuning”, Claude will invoke it when your prompt mentions optimizing queries. Write clear, specific descriptions so Claude can match tasks to the right subagent. -### - -​ - -Explicit invocation +### Explicit invocation To guarantee Claude uses a specific subagent, mention it by name in your prompt: @@ -236,11 +192,7 @@ This bypasses automatic matching and directly invokes the named subagent. -### - -​ - -Forked subagents +### Forked subagents A [fork](</docs/en/sub-agents#fork-the-current-conversation>) is a subagent that copies the conversation so far instead of starting fresh. From Claude Code v2.1.302, Claude can start forks in your application with nothing configured, by calling the Agent tool with `subagent_type: "fork"`. To stop Claude from starting forks, add an [`Agent(fork)` deny rule](</docs/en/agent-sdk/permissions#allow-and-deny-rules>): @@ -258,11 +210,7 @@ disallowedTools: ["Agent(fork)"] }; -### - -​ - -Dynamic agent configuration +### Dynamic agent configuration You can create agent definitions dynamically based on runtime conditions. This example creates a security reviewer with different strictness levels, using a more capable model for strict reviews. @@ -333,11 +281,7 @@ if ("result" in message) console.log(message.result); } -## - -​ - -Detect subagent invocation +## Detect subagent invocation Claude 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. 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` stays the same when you resume the subagent. `agent_id` requires TypeScript Agent SDK v0.3.292 or later. @@ -425,11 +369,7 @@ } } -## - -​ - -Resume subagents +## Resume subagents You can resume a subagent to continue where it left off rather than starting fresh. A resumed subagent retains its full conversation history, including all previous tool calls, results, and reasoning. When a subagent stops at its `maxTurns` limit, Claude Code marks the output in the Agent tool result as partial, so Claude knows the run is unfinished. When a subagent completes, the Agent tool result includes a text block containing `agentId: <id>`. The built-in [`Explore` and `Plan` agents](</docs/en/sub-agents#built-in-subagents>) are one-shot and don’t return an `agentId`, so use a custom agent or `general-purpose` when you need to resume. To resume a subagent programmatically: @@ -560,11 +500,7 @@ Subagent transcripts are stored in separate files and persist independently of the main conversation. See [resume subagents in Claude Code](</docs/en/sub-agents#resume-subagents>) for compaction behavior and the `cleanupPeriodDays` cleanup period. -## - -​ - -Tool restrictions +## Tool restrictions Use the `tools` field to limit what a subagent can do: @@ -621,11 +557,7 @@ if ("result" in message) console.log(message.result); } -### - -​ - -Common tool combinations +### Common tool combinations Use case| Tools| Description ---|---|--- @@ -634,11 +566,7 @@ Code modification| `Read`, `Edit`, `Write`, `Grep`, `Glob`| Full read/write access without command execution Full access| All tools| Inherits the tools available to subagents (omit the `tools` field) -## - -​ - -Cap subagent depth, concurrency, and spend +## Cap subagent depth, concurrency, and spend This section describes TypeScript SDK v0.3.219 and Python SDK v0.2.127 and later, the releases that bundle Claude Code v2.1.219 or later. On earlier releases, some of these limits are missing or default differently, so upgrade before you rely on them to bound a run. The [environment variable reference](</docs/en/env-vars>) and [turns and budget](</docs/en/agent-sdk/agent-loop#turns-and-budget>) record the Claude Code version that added each variable and the spend cap’s subagent enforcement. @@ -714,11 +642,7 @@ * **At the spend cap** : you see `error_max_budget_usd` with a cost at or above `5`, and then your error handler runs. * **At the concurrency limit** : you see a `tool_result` block in the message stream carrying `Concurrent subagent limit reached`. Claude receives the same block as the Agent tool’s result. -### - -​ - -Run Opus 5 with subagents +### Run Opus 5 with subagents Claude Opus 5 delegates to subagents more readily than earlier models, so the depth, concurrency, and spend limits matter most on queries that run Opus 5. The [Opus 5 prompting guide](<https://platform.claude.com/docs/en/build-with-claude/prompt-engineering/prompting-claude-opus-5#controlling-subagent-spawning>) has a delegation instruction you can add to any prompt. Whether Claude Code adds an instruction of its own depends on which [system prompt](</docs/en/agent-sdk/modifying-system-prompts#how-system-prompts-work>) you use: @@ -727,36 +651,20 @@ Either instruction only steers Claude, so set the limits as well. Claude Code enforces them however Claude decides to delegate. -## - -​ - -Scale up with dynamic workflows +## Scale up with dynamic workflows Subagents work well for a few delegated tasks per turn. For runs that coordinate dozens to hundreds of agents, use the `Workflow` tool, which moves the orchestration into a script the runtime executes outside the conversation context. See [dynamic workflows](</docs/en/workflows>) for how workflows differ from turn-by-turn subagent delegation. The `Workflow` tool is available in the TypeScript Agent SDK v0.3.149 and later. Include `Workflow` in `allowedTools` to auto-approve workflow runs. The tool input and output schemas are listed in the [TypeScript reference](</docs/en/agent-sdk/typescript#workflow>). -## - -​ - -Troubleshooting - -### - -​ - -Claude not delegating to subagents +## Troubleshooting + +### Claude not delegating to subagents If Claude completes tasks directly instead of delegating to your subagent: * **Use explicit prompting** : mention the subagent by name in your prompt, for example “Use the code-reviewer agent to check the authentication module” * **Write a clear description** : explain exactly when to use the subagent so Claude can match tasks appropriately -### - -​ - -Filesystem-based agents not loading +### Filesystem-based agents not loading Claude Code watches `~/.claude/agents/` and `.claude/agents/` and picks up a new or edited agent file within a few seconds, with no restart needed. If a definition never appears, work through these causes: @@ -768,11 +676,7 @@ For the file format, see [how to write subagent files](</docs/en/sub-agents#write-subagent-files>). -## - -​ - -Related documentation +## Related documentation * [Claude Code subagents](</docs/en/sub-agents>): subagent documentation including filesystem-based definitions * [Dynamic workflows](</docs/en/workflows>): orchestrate many subagents from a script for jobs too large for one conversation
modified
--- a/subagents.md +++ b/subagents.md @@ -152,7 +152,7 @@ `tools`| `string[]`| No| Array of allowed tool names. If omitted, inherits every [tool available to subagents](</docs/en/sub-agents#available-tools>) `disallowedTools`| `string[]`| No| Array of tool names to remove from the agent’s tool set. MCP server-level patterns are also accepted: `mcp__server` or `mcp__server__*` removes every tool from that server, and `mcp__*` removes every MCP tool from any server `model`| `string`| No| Model override for this agent. Accepts an alias such as `'fable'`, `'opus'`, `'sonnet'`, `'haiku'`, `'inherit'`, or a full model ID. `'inherit'` uses the main model. When you omit it, Claude Code picks the model in the [subagent model order](</docs/en/sub-agents#choose-a-model>) -`skills`| `string[]`| No| List of skill names to preload into the agent’s context at startup. Unlisted skills remain invocable through the Skill tool +`skills`| `string[]`| No| List of skill names to preload into the agent’s context at startup. At most the [first 32 distinct names](</docs/en/sub-agents#preload-skills-into-subagents>) in the list are preloaded. Unlisted skills remain invocable through the Skill tool `memory`| `'user' | 'project' | 'local'`| No| Memory source for this agent `mcpServers`| `(string | object)[]`| No| MCP servers available to this agent, by name or inline config `initialPrompt`| `string`| No| Auto-submitted as the first user turn when this agent runs as the main thread agent. Ignored when the agent is invoked as a subagent @@ -339,7 +339,7 @@ Detect subagent invocation -Claude 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. 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. +Claude 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. 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` stays the same when you resume the subagent. `agent_id` requires TypeScript Agent SDK v0.3.292 or later. 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`.
modified
--- a/subagents.md +++ b/subagents.md @@ -163,7 +163,18 @@ `effort`| `'low' | 'medium' | 'high' | 'xhigh' | 'max' | number`| No| Reasoning effort level for this agent `permissionMode`| `PermissionMode`| No| Permission mode for tool execution within this agent. The [subagent inheritance rules](</docs/en/agent-sdk/permissions#available-modes>) decide when it applies -In the Python SDK, multi-word field names such as `disallowedTools` and `mcpServers` keep their camelCase spelling to match the wire format rather than following Python’s snake_case convention. See the [`AgentDefinition` reference](</docs/en/agent-sdk/python#agentdefinition>) for details. Subagents run in the background by default. An Agent tool call that omits the [`run_in_background`](</docs/en/sub-agents#run-subagents-in-foreground-or-background>) input launches a background subagent, and Claude sets `run_in_background: false` when it needs the result before continuing. Set the `background` field to `true` to force background execution for a specific agent regardless of what Claude requests. Before Claude Code v2.1.198, the background default was rolling out gradually, and an Agent tool call that omitted `run_in_background` could run the subagent synchronously. Subagents can also spawn subagents of their own. To limit how deep that nesting goes, how many subagents run at once, and how much a query spends, see Cap subagent depth, concurrency, and spend. +In the Python SDK, multi-word field names such as `disallowedTools` and `mcpServers` keep their camelCase spelling to match the wire format rather than following Python’s snake_case convention. See the [`AgentDefinition` reference](</docs/en/agent-sdk/python#agentdefinition>) for details. + +#### + +​ + +Choose whether Claude waits for subagents + +By default, a subagent your main agent spawns [runs in the background](</docs/en/sub-agents#run-subagents-in-foreground-or-background>). The Agent tool result has `status: "async_launched"`, and the report arrives in a later turn. Claude waits when its next action needs the report: the call blocks and returns `status: "completed"`. To make that choice yourself: + + * **In your prompt** : ask Claude to wait, such as “run the test-runner agent and wait for it” + * **In the definition** : set `background` to `true` to keep that agent in the background ### @@ -224,6 +235,28 @@ "Use the code-reviewer agent to check the authentication module" This bypasses automatic matching and directly invokes the named subagent. + +### + +​ + +Forked subagents + +A [fork](</docs/en/sub-agents#fork-the-current-conversation>) is a subagent that copies the conversation so far instead of starting fresh. From Claude Code v2.1.302, Claude can start forks in your application with nothing configured, by calling the Agent tool with `subagent_type: "fork"`. To stop Claude from starting forks, add an [`Agent(fork)` deny rule](</docs/en/agent-sdk/permissions#allow-and-deny-rules>): + +Python + +TypeScript + + options = ClaudeAgentOptions( + # Deny only the fork subagent type; other subagents still run + disallowed_tools=["Agent(fork)"], + ) + + const options = { + // Deny only the fork subagent type; other subagents still run + disallowedTools: ["Agent(fork)"] + }; ###
modified
--- a/subagents.md +++ b/subagents.md @@ -157,6 +157,7 @@ `mcpServers`| `(string | object)[]`| No| MCP servers available to this agent, by name or inline config `initialPrompt`| `string`| No| Auto-submitted as the first user turn when this agent runs as the main thread agent. Ignored when the agent is invoked as a subagent `maxTurns`| `number`| No| Maximum number of agentic turns before the agent stops. When the agent reaches the limit, Claude Code returns its output marked as partial, and you can resume the agent to continue. The partial marking requires Claude Code v2.1.246 or later +`autoCompactWindow`| `number`| No| Lower the [auto-compact window](</docs/en/sub-agents#subagent-auto-compact-window>) for this agent when it runs as a subagent, to a token count from `100000` to `1000000`. Requires TypeScript Agent SDK v0.3.296 or later. The Python SDK’s [`AgentDefinition`](</docs/en/agent-sdk/python#agentdefinition>) doesn’t have this field `background`| `boolean`| No| Run this agent as a non-blocking background task when invoked `omitClaudeMd`| `boolean`| No| Leave the user, project, and local CLAUDE.md files out of this agent’s initial context; managed policy files still load. Ignored when the agent runs as the main thread agent. Requires TypeScript Agent SDK v0.3.271 or later. The Python SDK’s [`AgentDefinition`](</docs/en/agent-sdk/python#agentdefinition>) doesn’t have this field `effort`| `'low' | 'medium' | 'high' | 'xhigh' | 'max' | number`| No| Reasoning effort level for this agent
modified
--- a/subagents.md +++ b/subagents.md @@ -158,7 +158,7 @@ `initialPrompt`| `string`| No| Auto-submitted as the first user turn when this agent runs as the main thread agent. Ignored when the agent is invoked as a subagent `maxTurns`| `number`| No| Maximum number of agentic turns before the agent stops. When the agent reaches the limit, Claude Code returns its output marked as partial, and you can resume the agent to continue. The partial marking requires Claude Code v2.1.246 or later `background`| `boolean`| No| Run this agent as a non-blocking background task when invoked -`omitClaudeMd`| `boolean`| No| Run this agent without the user, project, and local CLAUDE.md files when it runs as a subagent; managed policy files still load. Ignored when the agent runs as the main thread agent. Requires TypeScript Agent SDK v0.3.271 or later. The Python SDK’s [`AgentDefinition`](</docs/en/agent-sdk/python#agentdefinition>) doesn’t have this field +`omitClaudeMd`| `boolean`| No| Leave the user, project, and local CLAUDE.md files out of this agent’s initial context; managed policy files still load. Ignored when the agent runs as the main thread agent. Requires TypeScript Agent SDK v0.3.271 or later. The Python SDK’s [`AgentDefinition`](</docs/en/agent-sdk/python#agentdefinition>) doesn’t have this field `effort`| `'low' | 'medium' | 'high' | 'xhigh' | 'max' | number`| No| Reasoning effort level for this agent `permissionMode`| `PermissionMode`| No| Permission mode for tool execution within this agent. The [subagent inheritance rules](</docs/en/agent-sdk/permissions#available-modes>) decide when it applies @@ -185,7 +185,7 @@ The subagent receives| The subagent doesn’t receive ---|--- Its own system prompt (`AgentDefinition.prompt`) and the Agent tool’s prompt| The parent’s conversation history or tool results -Project CLAUDE.md (loaded via [`settingSources`](</docs/en/agent-sdk/claude-code-features#control-filesystem-settings-with-settingsources>)), unless the agent sets `omitClaudeMd`| Preloaded skill content, unless listed in `AgentDefinition.skills` +Project CLAUDE.md (loaded via [`settingSources`](</docs/en/agent-sdk/claude-code-features#control-filesystem-settings-with-settingsources>)). An agent that sets `omitClaudeMd` starts without it| Preloaded skill content, unless listed in `AgentDefinition.skills` Tool definitions (inherited from parent or the subset in `tools`, [filtered for background runs](</docs/en/sub-agents#available-tools>))| The parent’s system prompt The parent receives the subagent’s final report, but may summarize it in its own response. To preserve subagent output verbatim in the user-facing response, include an instruction to do so in the prompt or `systemPrompt` option you pass to the main `query()` call.In v2.1.210 and later, Claude Code [scans the final message for instruction-shaped patterns](</docs/en/sub-agents#subagent-output-scanning>) before the parent reads it. The scan treats three kinds of pattern differently:
modified
--- a/subagents.md +++ b/subagents.md @@ -305,11 +305,11 @@ Detect subagent invocation -Claude 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. +Claude 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. 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. 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`. -The 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`. This example iterates through streamed messages, logging when a subagent is invoked and when subsequent messages originate from within that subagent’s execution context. +The 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`. This 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. Python @@ -379,6 +379,11 @@ // Check if this message is from within a subagent's context if (msg.parent_tool_use_id) { console.log(" (running inside subagent)"); + // On assistant and user messages, agent_id matches the task_id + // on that subagent's task_started and other task events + if (msg.agent_id) { + console.log(` agent_id: ${msg.agent_id}`); + } } if ("result" in message) {
modified
--- a/subagents.md +++ b/subagents.md @@ -735,6 +735,6 @@ Related documentation - * [Claude Code subagents](</docs/en/sub-agents>): comprehensive subagent documentation including filesystem-based definitions + * [Claude Code subagents](</docs/en/sub-agents>): subagent documentation including filesystem-based definitions * [Dynamic workflows](</docs/en/workflows>): orchestrate many subagents from a script for jobs too large for one conversation * [SDK overview](</docs/en/agent-sdk/overview>): getting started with the Claude Agent SDK
modified
--- a/subagents.md +++ b/subagents.md @@ -188,7 +188,7 @@ Project CLAUDE.md (loaded via [`settingSources`](</docs/en/agent-sdk/claude-code-features#control-filesystem-settings-with-settingsources>)), unless the agent sets `omitClaudeMd`| Preloaded skill content, unless listed in `AgentDefinition.skills` Tool definitions (inherited from parent or the subset in `tools`, [filtered for background runs](</docs/en/sub-agents#available-tools>))| The parent’s system prompt -The parent receives the subagent’s final message as the Agent tool result, but may summarize it in its own response. To preserve subagent output verbatim in the user-facing response, include an instruction to do so in the prompt or `systemPrompt` option you pass to the main `query()` call.In v2.1.210 and later, Claude Code [scans the final message for instruction-shaped patterns](</docs/en/sub-agents#subagent-output-scanning>) before the parent reads it. The scan treats three kinds of pattern differently: +The parent receives the subagent’s final report, but may summarize it in its own response. To preserve subagent output verbatim in the user-facing response, include an instruction to do so in the prompt or `systemPrompt` option you pass to the main `query()` call.In v2.1.210 and later, Claude Code [scans the final message for instruction-shaped patterns](</docs/en/sub-agents#subagent-output-scanning>) before the parent reads it. The scan treats three kinds of pattern differently: * **Control-tag imitation** : Claude Code neutralizes a tag that only the harness emits, such as a `<system-reminder>` block, in place. It inserts a backslash after the opening angle bracket and deletes nothing. * **Permission-configuration mentions** : Claude Code keeps references to the permission configuration, such as `.claude/settings.json`, `bypassPermissions`, or `--dangerously-skip-permissions`, as written.
modified
--- a/subagents.md +++ b/subagents.md @@ -710,7 +710,7 @@ If Claude completes tasks directly instead of delegating to your subagent: - * **Use explicit prompting** : mention the subagent by name in your prompt, for example “Use the code-reviewer agent to…” + * **Use explicit prompting** : mention the subagent by name in your prompt, for example “Use the code-reviewer agent to check the authentication module” * **Write a clear description** : explain exactly when to use the subagent so Claude can match tasks appropriately ###
modified
--- a/subagents.md +++ b/subagents.md @@ -609,7 +609,7 @@ ---|---|---|--- Depth| [`CLAUDE_CODE_MAX_SUBAGENT_SPAWN_DEPTH`](</docs/en/env-vars>)| `3` layers of subagents below your main agent. `1` stops your subagents from spawning any of their own| Leaves a subagent at the bottom layer unable to spawn, so it does its delegated work itself. See [nested subagents](</docs/en/sub-agents#let-subagents-spawn-their-own-subagents>) Concurrency| [`CLAUDE_CODE_MAX_CONCURRENT_SUBAGENTS`](</docs/en/env-vars>)| `20` subagents running at once, counting every subagent Claude spawns with the Agent tool| Refuses to spawn another subagent, returning `Concurrent subagent limit reached`, until the running count drops below the limit. Sessions with [ultracode](</docs/en/model-config#adjust-effort-level>) active are never refused. See the [concurrent subagent limit](</docs/en/sub-agents#concurrent-subagent-limit>) -Spend| `maxBudgetUsd` in TypeScript, `max_budget_usd` in Python| No limit. Compared against `total_cost_usd`, so subagent requests count| Enforces the cap in three ways: refuses to spawn more subagents, returning `Budget limit reached`, stops background subagents that are still running, and ends the query with the `error_max_budget_usd` result subtype. For how the caps behave across a session, see [turns and budget](</docs/en/agent-sdk/agent-loop#turns-and-budget>) +Spend| `maxBudgetUsd` in TypeScript, `max_budget_usd` in Python| No limit. Counts the call’s own spend, subagent requests included| Enforces the cap in three ways: refuses to spawn more subagents, returning `Budget limit reached`, stops background subagents that are still running, and ends the query with the `error_max_budget_usd` result subtype. For how the caps behave across a session, see [turns and budget](</docs/en/agent-sdk/agent-loop#turns-and-budget>) The two SDKs treat the `env` option differently: the TypeScript SDK replaces the subprocess environment with it, so spread `process.env` into it to keep variables like `PATH`, while the Python SDK merges it into the inherited environment. This example turns nesting off, allows at most five subagents at a time, and stops the query once the estimated spend reaches $5:
modified
--- a/subagents.md +++ b/subagents.md @@ -609,7 +609,7 @@ ---|---|---|--- Depth| [`CLAUDE_CODE_MAX_SUBAGENT_SPAWN_DEPTH`](</docs/en/env-vars>)| `3` layers of subagents below your main agent. `1` stops your subagents from spawning any of their own| Leaves a subagent at the bottom layer unable to spawn, so it does its delegated work itself. See [nested subagents](</docs/en/sub-agents#let-subagents-spawn-their-own-subagents>) Concurrency| [`CLAUDE_CODE_MAX_CONCURRENT_SUBAGENTS`](</docs/en/env-vars>)| `20` subagents running at once, counting every subagent Claude spawns with the Agent tool| Refuses to spawn another subagent, returning `Concurrent subagent limit reached`, until the running count drops below the limit. Sessions with [ultracode](</docs/en/model-config#adjust-effort-level>) active are never refused. See the [concurrent subagent limit](</docs/en/sub-agents#concurrent-subagent-limit>) -Spend| `maxBudgetUsd` in TypeScript, `max_budget_usd` in Python| No limit. Compared against `total_cost_usd`, so subagent requests count| Enforces the cap in three ways: refuses to spawn more subagents, returning `Budget limit reached`, stops background subagents that are still running, and ends the query with the `error_max_budget_usd` result subtype. See [turns and budget](</docs/en/agent-sdk/agent-loop#turns-and-budget>) +Spend| `maxBudgetUsd` in TypeScript, `max_budget_usd` in Python| No limit. Compared against `total_cost_usd`, so subagent requests count| Enforces the cap in three ways: refuses to spawn more subagents, returning `Budget limit reached`, stops background subagents that are still running, and ends the query with the `error_max_budget_usd` result subtype. For how the caps behave across a session, see [turns and budget](</docs/en/agent-sdk/agent-loop#turns-and-budget>) The two SDKs treat the `env` option differently: the TypeScript SDK replaces the subprocess environment with it, so spread `process.env` into it to keep variables like `PATH`, while the Python SDK merges it into the inherited environment. This example turns nesting off, allows at most five subagents at a time, and stops the query once the estimated spend reaches $5:
modified
--- a/subagents.md +++ b/subagents.md @@ -158,6 +158,7 @@ `initialPrompt`| `string`| No| Auto-submitted as the first user turn when this agent runs as the main thread agent. Ignored when the agent is invoked as a subagent `maxTurns`| `number`| No| Maximum number of agentic turns before the agent stops. When the agent reaches the limit, Claude Code returns its output marked as partial, and you can resume the agent to continue. The partial marking requires Claude Code v2.1.246 or later `background`| `boolean`| No| Run this agent as a non-blocking background task when invoked +`omitClaudeMd`| `boolean`| No| Run this agent without the user, project, and local CLAUDE.md files when it runs as a subagent; managed policy files still load. Ignored when the agent runs as the main thread agent. Requires TypeScript Agent SDK v0.3.271 or later. The Python SDK’s [`AgentDefinition`](</docs/en/agent-sdk/python#agentdefinition>) doesn’t have this field `effort`| `'low' | 'medium' | 'high' | 'xhigh' | 'max' | number`| No| Reasoning effort level for this agent `permissionMode`| `PermissionMode`| No| Permission mode for tool execution within this agent. The [subagent inheritance rules](</docs/en/agent-sdk/permissions#available-modes>) decide when it applies @@ -184,7 +185,7 @@ The subagent receives| The subagent doesn’t receive ---|--- Its own system prompt (`AgentDefinition.prompt`) and the Agent tool’s prompt| The parent’s conversation history or tool results -Project CLAUDE.md (loaded via [`settingSources`](</docs/en/agent-sdk/claude-code-features#control-filesystem-settings-with-settingsources>))| Preloaded skill content, unless listed in `AgentDefinition.skills` +Project CLAUDE.md (loaded via [`settingSources`](</docs/en/agent-sdk/claude-code-features#control-filesystem-settings-with-settingsources>)), unless the agent sets `omitClaudeMd`| Preloaded skill content, unless listed in `AgentDefinition.skills` Tool definitions (inherited from parent or the subset in `tools`, [filtered for background runs](</docs/en/sub-agents#available-tools>))| The parent’s system prompt The parent receives the subagent’s final message as the Agent tool result, but may summarize it in its own response. To preserve subagent output verbatim in the user-facing response, include an instruction to do so in the prompt or `systemPrompt` option you pass to the main `query()` call.In v2.1.210 and later, Claude Code [scans the final message for instruction-shaped patterns](</docs/en/sub-agents#subagent-output-scanning>) before the parent reads it. The scan treats three kinds of pattern differently:
modified
--- a/subagents.md +++ b/subagents.md @@ -159,7 +159,7 @@ `maxTurns`| `number`| No| Maximum number of agentic turns before the agent stops. When the agent reaches the limit, Claude Code returns its output marked as partial, and you can resume the agent to continue. The partial marking requires Claude Code v2.1.246 or later `background`| `boolean`| No| Run this agent as a non-blocking background task when invoked `effort`| `'low' | 'medium' | 'high' | 'xhigh' | 'max' | number`| No| Reasoning effort level for this agent -`permissionMode`| `PermissionMode`| No| Permission mode for tool execution within this agent +`permissionMode`| `PermissionMode`| No| Permission mode for tool execution within this agent. The [subagent inheritance rules](</docs/en/agent-sdk/permissions#available-modes>) decide when it applies In the Python SDK, multi-word field names such as `disallowedTools` and `mcpServers` keep their camelCase spelling to match the wire format rather than following Python’s snake_case convention. See the [`AgentDefinition` reference](</docs/en/agent-sdk/python#agentdefinition>) for details. Subagents run in the background by default. An Agent tool call that omits the [`run_in_background`](</docs/en/sub-agents#run-subagents-in-foreground-or-background>) input launches a background subagent, and Claude sets `run_in_background: false` when it needs the result before continuing. Set the `background` field to `true` to force background execution for a specific agent regardless of what Claude requests. Before Claude Code v2.1.198, the background default was rolling out gradually, and an Agent tool call that omitted `run_in_background` could run the subagent synchronously. Subagents can also spawn subagents of their own. To limit how deep that nesting goes, how many subagents run at once, and how much a query spends, see Cap subagent depth, concurrency, and spend.
modified
Show diff (106 lines)
--- a/subagents.md +++ b/subagents.md @@ -1,4 +1,4 @@ -Subagents are separate agent instances that your main agent can spawn to handle focused subtasks. Use them to isolate context, run multiple analyses in parallel, and apply specialized instructions without adding to the main agent’s prompt. This guide explains how to define and use subagents in the SDK using the `agents` parameter. +Subagents are separate agent instances that your main agent can spawn to handle focused subtasks. Use them to isolate context, run multiple analyses in parallel, and apply specialized instructions without adding to the main agent’s prompt. ## @@ -39,7 +39,7 @@ Programmatic definition (recommended) -Define subagents directly in your code using the `agents` parameter. Claude invokes subagents through the `Agent` tool, so include `Agent` in `allowedTools` to auto-approve subagent invocations without a permission prompt. Most examples on this page print only the final result. To confirm that Claude delegated to a subagent rather than answering directly, see Detect subagent invocation. This example creates two subagents: a code reviewer with read-only access and a test runner that can execute commands. +Define subagents directly in your code using the `agents` parameter. Claude invokes subagents through the `Agent` tool. Most examples on this page print only the final result. To confirm that Claude delegated to a subagent rather than answering directly, see Detect subagent invocation. This example creates two subagents: a code reviewer with read-only access and a test runner that can execute commands. Python @@ -52,7 +52,7 @@ async for message in query( prompt="Review the authentication module for security issues", options=ClaudeAgentOptions( - # Auto-approve these tools, including Agent for subagent invocation + # Auto-approve these tools allowed_tools=["Read", "Grep", "Glob", "Agent"], agents={ "code-reviewer": AgentDefinition( @@ -98,7 +98,7 @@ for await (const message of query({ prompt: "Review the authentication module for security issues", options: { - // Auto-approve these tools, including Agent for subagent invocation + // Auto-approve these tools allowedTools: ["Read", "Grep", "Glob", "Agent"], agents: { "code-reviewer": { @@ -161,12 +161,7 @@ `effort`| `'low' | 'medium' | 'high' | 'xhigh' | 'max' | number`| No| Reasoning effort level for this agent `permissionMode`| `PermissionMode`| No| Permission mode for tool execution within this agent -In the Python SDK, multi-word field names such as `disallowedTools` and `mcpServers` keep their camelCase spelling to match the wire format rather than following Python’s snake_case convention. See the [`AgentDefinition` reference](</docs/en/agent-sdk/python#agentdefinition>) for details. Two subagent behaviors changed in Claude Code v2.1.198: - - * Subagents run in the background by default. An Agent tool call that omits the [`run_in_background`](</docs/en/agent-sdk/typescript>) input launches a background subagent, and Claude sets `run_in_background: false` when it needs the result before continuing. Before v2.1.198, omitting `run_in_background` ran the subagent synchronously. Set the `background` field to `true` to force background execution for a specific agent regardless of what Claude requests. - * A subagent inherits the main session’s extended thinking configuration. - -Subagents can also spawn subagents of their own. To limit how deep that nesting goes, how many subagents run at once, and how much a query spends, see Cap subagent depth, concurrency, and spend. +In the Python SDK, multi-word field names such as `disallowedTools` and `mcpServers` keep their camelCase spelling to match the wire format rather than following Python’s snake_case convention. See the [`AgentDefinition` reference](</docs/en/agent-sdk/python#agentdefinition>) for details. Subagents run in the background by default. An Agent tool call that omits the [`run_in_background`](</docs/en/sub-agents#run-subagents-in-foreground-or-background>) input launches a background subagent, and Claude sets `run_in_background: false` when it needs the result before continuing. Set the `background` field to `true` to force background execution for a specific agent regardless of what Claude requests. Before Claude Code v2.1.198, the background default was rolling out gradually, and an Agent tool call that omitted `run_in_background` could run the subagent synchronously. Subagents can also spawn subagents of their own. To limit how deep that nesting goes, how many subagents run at once, and how much a query spends, see Cap subagent depth, concurrency, and spend. ### @@ -176,7 +171,7 @@ You can also define subagents as markdown files in `.claude/agents/` directories. See the [Claude Code subagents documentation](</docs/en/sub-agents>) for details on this approach. Programmatically defined agents take precedence over filesystem-based agents with the same name. -Even without defining custom subagents, Claude can spawn the built-in `general-purpose` subagent. This is useful for delegating research or exploration tasks without creating specialized agents. Include `Agent` in `allowedTools` so these invocations auto-approve without a permission prompt.When Claude calls the Agent tool without a `subagent_type`, it gets this built-in `general-purpose` subagent. If you set [`CLAUDE_AGENT_SDK_DISABLE_BUILTIN_AGENTS=1`](</docs/en/env-vars>), that default is gone too. Such a call then fails with `subagent_type is required: the general-purpose agent is not available in this session`. The message ends with the subagent types that are still available. Before TypeScript SDK v0.3.235 (Python SDK: bundled Claude Code before v2.1.235), the same call failed with `Agent type 'general-purpose' not found`. +When Claude calls the Agent tool without a `subagent_type`, it gets the built-in `general-purpose` subagent, which Claude can spawn even when you define no agents of your own. Setting [`CLAUDE_AGENT_SDK_DISABLE_BUILTIN_AGENTS=1`](</docs/en/env-vars>) removes that default, and such a call fails with [`subagent_type is required`](</docs/en/errors#subagent-type-is-required>). ## @@ -184,7 +179,7 @@ What subagents inherit -Unless the subagent is a [fork](</docs/en/sub-agents#fork-the-current-conversation>), its context window starts fresh, with no parent conversation, but isn’t empty. The only content you pass from parent to subagent is the Agent tool’s prompt string, so include any file paths, error messages, or decisions the subagent needs directly in that prompt. A subagent that has the [`SendMessage`](</docs/en/tools-reference>) tool starts with a list of the other named agents running in the session, so it knows which names it can send messages to. Claude Code adds the list to the subagent’s first turn automatically. A [fork](</docs/en/sub-agents#fork-the-current-conversation>) doesn’t get the list because it inherits the parent conversation instead. The list requires Claude Code v2.1.206 or later. The table below lists what a non-fork subagent’s context contains and what it leaves out. +Unless the subagent is a [fork](</docs/en/sub-agents#fork-the-current-conversation>), its context window starts fresh, with no parent conversation, but isn’t empty. The only content you pass from parent to subagent is the Agent tool’s prompt string, so include any file paths, error messages, or decisions the subagent needs directly in that prompt. A subagent that has the [`SendMessage`](</docs/en/tools-reference>) tool starts with a list of the other named agents running in the session, so it knows which names it can send messages to. Claude Code adds the list to the subagent’s first turn automatically. A [fork](</docs/en/sub-agents#fork-the-current-conversation>) doesn’t get the list because it inherits the parent conversation instead. A subagent also inherits the main session’s extended thinking configuration. The table below lists what a non-fork subagent’s context contains and what it leaves out. The subagent receives| The subagent doesn’t receive ---|--- @@ -311,7 +306,7 @@ Claude 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. -The tool name was renamed from `"Task"` to `"Agent"` in Claude Code v2.1.63. Current SDK releases emit `"Agent"` in `tool_use` blocks but still use `"Task"` in the `system:init` tools list and in `result.permission_denials[].tool_name`. Checking both values in `block.name` ensures compatibility across SDK versions. +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`. The 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`. This example iterates through streamed messages, logging when a subagent is invoked and when subsequent messages originate from within that subagent’s execution context. @@ -400,9 +395,9 @@ 1. **Capture the session ID** : extract `session_id` from messages during the first query 2. **Extract the agent ID** : parse `agentId` from the Agent tool result text - 3. **Resume the session** : pass `resume: sessionId` in the second query’s options, and include the agent ID in your prompt - -You must resume the same session to access the subagent’s transcript. Each `query()` call starts a new session by default, so pass `resume: sessionId` to continue in the same session.When using a custom agent, pass the same agent definition in the `agents` parameter for both queries. + 3. **Resume the session** : pass `resume: sessionId` in the second query’s options, and include the agent ID in your prompt. Each `query()` call starts a new session by default, and you must resume the same session to access the subagent’s transcript. + +When using a custom agent, pass the same agent definition in the `agents` parameter for both queries. The example below defines a custom `endpoint-finder` agent. The first query runs it and captures the session ID and agent ID from the Agent tool result, then the second query resumes the session to ask a follow-up question that requires context from the first analysis. @@ -607,7 +602,7 @@ This section describes TypeScript SDK v0.3.219 and Python SDK v0.2.127 and later, the releases that bundle Claude Code v2.1.219 or later. On earlier releases, some of these limits are missing or default differently, so upgrade before you rely on them to bound a run. The [environment variable reference](</docs/en/env-vars>) and [turns and budget](</docs/en/agent-sdk/agent-loop#turns-and-budget>) record the Claude Code version that added each variable and the spend cap’s subagent enforcement. -Once you include `Agent` in `allowedTools`, Claude decides on its own when to spawn a subagent and how many to spawn. Each subagent makes its own API requests, which count toward the query’s `total_cost_usd`, and a subagent can spawn subagents of its own, so one prompt can grow into a tree of agents. You can cap that growth in three ways: how deeply subagents nest, how many run at once, and how much the whole query spends. Set the depth and concurrency limits as environment variables through the [`env`](</docs/en/agent-sdk/typescript#options>) option, and the spend limit as a query option: +Claude decides on its own when to spawn a subagent and how many to spawn. Each subagent makes its own API requests, which count toward the query’s `total_cost_usd`, and a subagent can spawn subagents of its own, so one prompt can grow into a tree of agents. You can cap that growth in three ways: how deeply subagents nest, how many run at once, and how much the whole query spends. Set the depth and concurrency limits as environment variables through the [`env`](</docs/en/agent-sdk/typescript#options>) option, and the spend limit as a query option: Limit| Set it with| Default| What Claude Code does at the limit ---|---|---|--- @@ -714,7 +709,6 @@ If Claude completes tasks directly instead of delegating to your subagent: - * **Check Agent invocations are approved** : include `Agent` in `allowedTools` to auto-approve subagent calls. Without it, Agent invocations fall through to your `canUseTool` callback or, in `dontAsk` mode, are denied * **Use explicit prompting** : mention the subagent by name in your prompt, for example “Use the code-reviewer agent to…” * **Write a clear description** : explain exactly when to use the subagent so Claude can match tasks appropriately
modified
--- a/subagents.md +++ b/subagents.md @@ -151,7 +151,7 @@ `prompt`| `string`| Yes| The agent’s system prompt defining its role and behavior `tools`| `string[]`| No| Array of allowed tool names. If omitted, inherits every [tool available to subagents](</docs/en/sub-agents#available-tools>) `disallowedTools`| `string[]`| No| Array of tool names to remove from the agent’s tool set. MCP server-level patterns are also accepted: `mcp__server` or `mcp__server__*` removes every tool from that server, and `mcp__*` removes every MCP tool from any server -`model`| `string`| No| Model override for this agent. Accepts an alias such as `'fable'`, `'opus'`, `'sonnet'`, `'haiku'`, `'inherit'`, or a full model ID. Defaults to main model if omitted +`model`| `string`| No| Model override for this agent. Accepts an alias such as `'fable'`, `'opus'`, `'sonnet'`, `'haiku'`, `'inherit'`, or a full model ID. `'inherit'` uses the main model. When you omit it, Claude Code picks the model in the [subagent model order](</docs/en/sub-agents#choose-a-model>) `skills`| `string[]`| No| List of skill names to preload into the agent’s context at startup. Unlisted skills remain invocable through the Skill tool `memory`| `'user' | 'project' | 'local'`| No| Memory source for this agent `mcpServers`| `(string | object)[]`| No| MCP servers available to this agent, by name or inline config
modified
--- a/subagents.md +++ b/subagents.md @@ -234,7 +234,7 @@ Dynamic agent configuration -You can create agent definitions dynamically based on runtime conditions. This example creates a security reviewer with different strictness levels, using a more powerful model for strict reviews. +You can create agent definitions dynamically based on runtime conditions. This example creates a security reviewer with different strictness levels, using a more capable model for strict reviews. Python
modified
--- a/subagents.md +++ b/subagents.md @@ -156,7 +156,7 @@ `memory`| `'user' | 'project' | 'local'`| No| Memory source for this agent `mcpServers`| `(string | object)[]`| No| MCP servers available to this agent, by name or inline config `initialPrompt`| `string`| No| Auto-submitted as the first user turn when this agent runs as the main thread agent. Ignored when the agent is invoked as a subagent -`maxTurns`| `number`| No| Maximum number of agentic turns before the agent stops +`maxTurns`| `number`| No| Maximum number of agentic turns before the agent stops. When the agent reaches the limit, Claude Code returns its output marked as partial, and you can resume the agent to continue. The partial marking requires Claude Code v2.1.246 or later `background`| `boolean`| No| Run this agent as a non-blocking background task when invoked `effort`| `'low' | 'medium' | 'high' | 'xhigh' | 'max' | number`| No| Reasoning effort level for this agent `permissionMode`| `PermissionMode`| No| Permission mode for tool execution within this agent @@ -396,7 +396,7 @@ Resume subagents -You can resume a subagent to continue where it left off rather than starting fresh. A resumed subagent retains its full conversation history, including all previous tool calls, results, and reasoning. When a subagent completes, the Agent tool result includes a text block containing `agentId: <id>`. The built-in [`Explore` and `Plan` agents](</docs/en/sub-agents#built-in-subagents>) are one-shot and don’t return an `agentId`, so use a custom agent or `general-purpose` when you need to resume. To resume a subagent programmatically: +You can resume a subagent to continue where it left off rather than starting fresh. A resumed subagent retains its full conversation history, including all previous tool calls, results, and reasoning. When a subagent stops at its `maxTurns` limit, Claude Code marks the output in the Agent tool result as partial, so Claude knows the run is unfinished. When a subagent completes, the Agent tool result includes a text block containing `agentId: <id>`. The built-in [`Explore` and `Plan` agents](</docs/en/sub-agents#built-in-subagents>) are one-shot and don’t return an `agentId`, so use a custom agent or `general-purpose` when you need to resume. To resume a subagent programmatically: 1. **Capture the session ID** : extract `session_id` from messages during the first query 2. **Extract the agent ID** : parse `agentId` from the Agent tool result text
modified
--- a/subagents.md +++ b/subagents.md @@ -20,37 +20,12 @@ Benefits of using subagents -### - -​ - -Context isolation - -Each subagent runs in its own fresh conversation. Intermediate tool calls and results stay inside the subagent; only its final message returns to the parent. See What subagents inherit for exactly what’s in the subagent’s context. **Example:** a `research-assistant` subagent can explore dozens of files without any of that content accumulating in the main conversation. The parent receives a concise summary, not every file the subagent read. - -### - -​ - -Parallelization - -Multiple subagents can run concurrently, so independent subtasks finish in the time of the slowest one rather than the sum of all of them. **Example:** during a code review, you can run `style-checker`, `security-scanner`, and `test-coverage` subagents simultaneously instead of sequentially. - -### - -​ - -Specialized instructions and knowledge - -Each subagent can have tailored system prompts with specific expertise, best practices, and constraints. **Example:** a `database-migration` subagent can have detailed knowledge about SQL best practices, rollback strategies, and data integrity checks that would be unnecessary noise in the main agent’s instructions. - -### - -​ - -Tool restrictions - -Subagents can be limited to specific tools, reducing the risk of unintended actions. **Example:** a `doc-reviewer` subagent might only have access to Read and Grep tools, ensuring it can analyze but never accidentally modify your documentation files. +Because subagents are separate agent instances, delegating work to them gives you four benefits: + + * **Context isolation** : each subagent runs in its own conversation, which starts fresh unless the subagent is a [fork](</docs/en/sub-agents#fork-the-current-conversation>). Either way, intermediate tool calls and results stay inside the subagent; only its final message returns to the parent. A `research-assistant` subagent can explore dozens of files without any of that content accumulating in the main conversation. The parent receives a concise summary, not every file the subagent read. See What subagents inherit for exactly what’s in the subagent’s context. + * **Parallelization** : multiple subagents can run concurrently, so independent subtasks finish in the time of the slowest one rather than the sum of all of them. During a code review, you can run `style-checker`, `security-scanner`, and `test-coverage` subagents simultaneously instead of sequentially. + * **Specialized instructions and knowledge** : each subagent can have a tailored system prompt with specific expertise, best practices, and constraints. A `database-migration` subagent can have detailed knowledge about SQL best practices, rollback strategies, and data integrity checks that would be unnecessary noise in the main agent’s instructions. + * **Tool restrictions** : subagents can be limited to specific tools, reducing the risk of unintended actions. A `doc-reviewer` subagent might only have access to Read and Grep tools, ensuring it can analyze but never accidentally modify your documentation files. ## @@ -209,7 +184,7 @@ What subagents inherit -A subagent’s context window starts fresh, with no parent conversation, but isn’t empty. The only content you pass from parent to subagent is the Agent tool’s prompt string, so include any file paths, error messages, or decisions the subagent needs directly in that prompt. A subagent that has the [`SendMessage`](</docs/en/tools-reference>) tool starts with a list of the other named agents running in the session, so it knows which names it can send messages to. Claude Code adds the list to the subagent’s first turn automatically. A [fork](</docs/en/sub-agents#fork-the-current-conversation>) doesn’t get the list because it inherits the parent conversation instead. The list requires Claude Code v2.1.206 or later. +Unless the subagent is a [fork](</docs/en/sub-agents#fork-the-current-conversation>), its context window starts fresh, with no parent conversation, but isn’t empty. The only content you pass from parent to subagent is the Agent tool’s prompt string, so include any file paths, error messages, or decisions the subagent needs directly in that prompt. A subagent that has the [`SendMessage`](</docs/en/tools-reference>) tool starts with a list of the other named agents running in the session, so it knows which names it can send messages to. Claude Code adds the list to the subagent’s first turn automatically. A [fork](</docs/en/sub-agents#fork-the-current-conversation>) doesn’t get the list because it inherits the parent conversation instead. The list requires Claude Code v2.1.206 or later. The table below lists what a non-fork subagent’s context contains and what it leaves out. The subagent receives| The subagent doesn’t receive ---|---
modified
--- a/subagents.md +++ b/subagents.md @@ -201,7 +201,7 @@ You can also define subagents as markdown files in `.claude/agents/` directories. See the [Claude Code subagents documentation](</docs/en/sub-agents>) for details on this approach. Programmatically defined agents take precedence over filesystem-based agents with the same name. -Even without defining custom subagents, Claude can spawn the built-in `general-purpose` subagent. This is useful for delegating research or exploration tasks without creating specialized agents. Include `Agent` in `allowedTools` so these invocations auto-approve without a permission prompt. +Even without defining custom subagents, Claude can spawn the built-in `general-purpose` subagent. This is useful for delegating research or exploration tasks without creating specialized agents. Include `Agent` in `allowedTools` so these invocations auto-approve without a permission prompt.When Claude calls the Agent tool without a `subagent_type`, it gets this built-in `general-purpose` subagent. If you set [`CLAUDE_AGENT_SDK_DISABLE_BUILTIN_AGENTS=1`](</docs/en/env-vars>), that default is gone too. Such a call then fails with `subagent_type is required: the general-purpose agent is not available in this session`. The message ends with the subagent types that are still available. Before TypeScript SDK v0.3.235 (Python SDK: bundled Claude Code before v2.1.235), the same call failed with `Agent type 'general-purpose' not found`. ##
modified
--- a/subagents.md +++ b/subagents.md @@ -754,6 +754,7 @@ * **New`agents` directory**: the watcher covers only directories that existed when the session started, so the first file in a new directory needs a session restart. This is the most common cause. * **Invalid frontmatter or a duplicate`name`**: check the file’s YAML, and whether an existing agent already uses the `name`. * **`--disable-slash-commands`** : sessions started with this flag don’t watch these directories and always need a restart to load new files. + * **A file under an added directory** : Claude Code loads `.claude/agents/` from directories added with the `add_dirs` (Python) or `additionalDirectories` (TypeScript) option, or the CLI’s `--add-dir` or `/add-dir`, but doesn’t watch them, so a new or edited file there needs a session restart. * **A programmatic agent with the same name** : `agents` passed to `query()` override a filesystem agent with the same name. For the file format, see [how to write subagent files](</docs/en/sub-agents#write-subagent-files>).
modified
Show diff (111 lines)
--- a/subagents.md +++ b/subagents.md @@ -191,7 +191,7 @@ * Subagents run in the background by default. An Agent tool call that omits the [`run_in_background`](</docs/en/agent-sdk/typescript>) input launches a background subagent, and Claude sets `run_in_background: false` when it needs the result before continuing. Before v2.1.198, omitting `run_in_background` ran the subagent synchronously. Set the `background` field to `true` to force background execution for a specific agent regardless of what Claude requests. * A subagent inherits the main session’s extended thinking configuration. -By default, subagents can spawn subagents of their own, up to three layers below the main conversation. To change the limit, set [`CLAUDE_CODE_MAX_SUBAGENT_SPAWN_DEPTH`](</docs/en/env-vars>) to the number of subagent layers you want below your main conversation, or `1` to turn nesting off; see [nested subagents](</docs/en/sub-agents#let-subagents-spawn-their-own-subagents>). +Subagents can also spawn subagents of their own. To limit how deep that nesting goes, how many subagents run at once, and how much a query spends, see Cap subagent depth, concurrency, and spend. ### @@ -628,6 +628,99 @@ ​ +Cap subagent depth, concurrency, and spend + +This section describes TypeScript SDK v0.3.219 and Python SDK v0.2.127 and later, the releases that bundle Claude Code v2.1.219 or later. On earlier releases, some of these limits are missing or default differently, so upgrade before you rely on them to bound a run. The [environment variable reference](</docs/en/env-vars>) and [turns and budget](</docs/en/agent-sdk/agent-loop#turns-and-budget>) record the Claude Code version that added each variable and the spend cap’s subagent enforcement. + +Once you include `Agent` in `allowedTools`, Claude decides on its own when to spawn a subagent and how many to spawn. Each subagent makes its own API requests, which count toward the query’s `total_cost_usd`, and a subagent can spawn subagents of its own, so one prompt can grow into a tree of agents. You can cap that growth in three ways: how deeply subagents nest, how many run at once, and how much the whole query spends. Set the depth and concurrency limits as environment variables through the [`env`](</docs/en/agent-sdk/typescript#options>) option, and the spend limit as a query option: + +Limit| Set it with| Default| What Claude Code does at the limit +---|---|---|--- +Depth| [`CLAUDE_CODE_MAX_SUBAGENT_SPAWN_DEPTH`](</docs/en/env-vars>)| `3` layers of subagents below your main agent. `1` stops your subagents from spawning any of their own| Leaves a subagent at the bottom layer unable to spawn, so it does its delegated work itself. See [nested subagents](</docs/en/sub-agents#let-subagents-spawn-their-own-subagents>) +Concurrency| [`CLAUDE_CODE_MAX_CONCURRENT_SUBAGENTS`](</docs/en/env-vars>)| `20` subagents running at once, counting every subagent Claude spawns with the Agent tool| Refuses to spawn another subagent, returning `Concurrent subagent limit reached`, until the running count drops below the limit. Sessions with [ultracode](</docs/en/model-config#adjust-effort-level>) active are never refused. See the [concurrent subagent limit](</docs/en/sub-agents#concurrent-subagent-limit>) +Spend| `maxBudgetUsd` in TypeScript, `max_budget_usd` in Python| No limit. Compared against `total_cost_usd`, so subagent requests count| Enforces the cap in three ways: refuses to spawn more subagents, returning `Budget limit reached`, stops background subagents that are still running, and ends the query with the `error_max_budget_usd` result subtype. See [turns and budget](</docs/en/agent-sdk/agent-loop#turns-and-budget>) + +The two SDKs treat the `env` option differently: the TypeScript SDK replaces the subprocess environment with it, so spread `process.env` into it to keep variables like `PATH`, while the Python SDK merges it into the inherited environment. This example turns nesting off, allows at most five subagents at a time, and stops the query once the estimated spend reaches $5: + +Python + +TypeScript + + import asyncio + from claude_agent_sdk import query, ClaudeAgentOptions, ResultMessage + + async def main(): + try: + async for message in query( + prompt="Audit every service in this repo for unhandled promise rejections", + options=ClaudeAgentOptions( + allowed_tools=["Read", "Grep", "Glob", "Agent"], + # env is merged on top of the inherited environment + env={ + "CLAUDE_CODE_MAX_SUBAGENT_SPAWN_DEPTH": "1", + "CLAUDE_CODE_MAX_CONCURRENT_SUBAGENTS": "5", + }, + max_budget_usd=5.0, + ), + ): + if isinstance(message, ResultMessage): + print(f"{message.subtype}: ${message.total_cost_usd}") + except Exception as error: + # A single-shot query() raises after yielding an error result, + # so the budget-capped result has already been printed above. + print(f"Session ended with an error: {error}") + + asyncio.run(main()) + + import { query } from "@anthropic-ai/claude-agent-sdk"; + + try { + for await (const message of query({ + prompt: "Audit every service in this repo for unhandled promise rejections", + options: { + allowedTools: ["Read", "Grep", "Glob", "Agent"], + // env replaces the subprocess environment, so spread process.env to keep PATH + env: { + ...process.env, + CLAUDE_CODE_MAX_SUBAGENT_SPAWN_DEPTH: "1", + CLAUDE_CODE_MAX_CONCURRENT_SUBAGENTS: "5", + }, + maxBudgetUsd: 5, + }, + })) { + if (message.type === "result") { + console.log(`${message.subtype}: $${message.total_cost_usd}`); + } + } + } catch (error) { + // A single-shot query() throws after yielding an error result, + // so the budget-capped result has already been logged above. + console.error(`Session ended with an error: ${error}`); + } + +What you see depends on which limit, if any, the query reaches: + + * **Under the spend cap** : you see `success` and the estimated cost. + * **At the spend cap** : you see `error_max_budget_usd` with a cost at or above `5`, and then your error handler runs. + * **At the concurrency limit** : you see a `tool_result` block in the message stream carrying `Concurrent subagent limit reached`. Claude receives the same block as the Agent tool’s result. + +### + +​ + +Run Opus 5 with subagents + +Claude Opus 5 delegates to subagents more readily than earlier models, so the depth, concurrency, and spend limits matter most on queries that run Opus 5. The [Opus 5 prompting guide](<https://platform.claude.com/docs/en/build-with-claude/prompt-engineering/prompting-claude-opus-5#controlling-subagent-spawning>) has a delegation instruction you can add to any prompt. Whether Claude Code adds an instruction of its own depends on which [system prompt](</docs/en/agent-sdk/modifying-system-prompts#how-system-prompts-work>) you use: + + * **`claude_code` preset**: when the model is Opus 5, Claude Code adds a line to its system prompt telling Claude not to call the Agent tool unless it’s asked to. The Agent tool stays available. + * **A custom prompt, or no`systemPrompt`**: Claude Code doesn’t build its system prompt, so that line is absent. Add the prompting guide’s delegation instruction to your own prompt. + +Either instruction only steers Claude, so set the limits as well. Claude Code enforces them however Claude decides to delegate. + +## + +​ + Scale up with dynamic workflows Subagents work well for a few delegated tasks per turn. For runs that coordinate dozens to hundreds of agents, use the `Workflow` tool, which moves the orchestration into a script the runtime executes outside the conversation context. See [dynamic workflows](</docs/en/workflows>) for how workflows differ from turn-by-turn subagent delegation. The `Workflow` tool is available in the TypeScript Agent SDK v0.3.149 and later. Include `Workflow` in `allowedTools` to auto-approve workflow runs. The tool input and output schemas are listed in the [TypeScript reference](</docs/en/agent-sdk/typescript#workflow>).
modified
--- a/subagents.md +++ b/subagents.md @@ -12,7 +12,7 @@ * **Filesystem-based** : define agents as markdown files in `.claude/agents/` directories. See [defining subagents as files](</docs/en/sub-agents>) * **Built-in general-purpose** : Claude can invoke the built-in `general-purpose` subagent at any time via the Agent tool without you defining anything -This guide focuses on the programmatic approach, which is recommended for SDK applications. When you define subagents, Claude determines whether to invoke them based on each subagent’s `description` field. Write clear descriptions that explain when to use the subagent, and Claude automatically delegates appropriate tasks. You can also explicitly request a subagent by name in your prompt, for example “Use the code-reviewer agent to…”. +This guide focuses on the programmatic approach, which is recommended for SDK applications. ## @@ -189,12 +189,9 @@ In the Python SDK, multi-word field names such as `disallowedTools` and `mcpServers` keep their camelCase spelling to match the wire format rather than following Python’s snake_case convention. See the [`AgentDefinition` reference](</docs/en/agent-sdk/python#agentdefinition>) for details. Two subagent behaviors changed in Claude Code v2.1.198: * Subagents run in the background by default. An Agent tool call that omits the [`run_in_background`](</docs/en/agent-sdk/typescript>) input launches a background subagent, and Claude sets `run_in_background: false` when it needs the result before continuing. Before v2.1.198, omitting `run_in_background` ran the subagent synchronously. Set the `background` field to `true` to force background execution for a specific agent regardless of what Claude requests. - * A subagent inherits the main session’s extended thinking configuration. On earlier versions, extended thinking is disabled inside subagents regardless of the main session’s setting. - -By default, subagents can spawn subagents of their own, up to three layers below the main conversation. To change the limit, set [`CLAUDE_CODE_MAX_SUBAGENT_SPAWN_DEPTH`](</docs/en/env-vars>) to the number of subagent layers you want below your main conversation, or `1` to turn nesting off; see [nested subagents](</docs/en/sub-agents#let-subagents-spawn-their-own-subagents>).Earlier versions used different defaults: - - * **v2.1.172 through v2.1.216** : subagents could nest by default, up to five layers deep, and the limit couldn’t be changed. - * **v2.1.217 through v2.1.218** : the limit defaulted to one, so a subagent couldn’t spawn its own unless you raised it; v2.1.219 raised the default to three. + * A subagent inherits the main session’s extended thinking configuration. + +By default, subagents can spawn subagents of their own, up to three layers below the main conversation. To change the limit, set [`CLAUDE_CODE_MAX_SUBAGENT_SPAWN_DEPTH`](</docs/en/env-vars>) to the number of subagent layers you want below your main conversation, or `1` to turn nesting off; see [nested subagents](</docs/en/sub-agents#let-subagents-spawn-their-own-subagents>). ### @@ -228,7 +225,7 @@ For a control-tag or permission-configuration match, Claude Code prepends a `[harness: ...]` marker line naming the matched patterns; a turn-marker match doesn’t add the marker line. Those are the only modifications the scan makes: it never removes or rewords the subagent’s text. -An API error that ends the subagent early, such as a rate limit, is never delivered as its result. If a rate limit, overload, or server error cuts off a foreground subagent that already produced text output, the Agent tool returns that partial output with a note that the subagent didn’t finish. A subagent that produced nothing, or whose only output was tool calls with no text, fails with an error message, `Agent terminated early due to an API error`, followed by the error detail. See [API errors in subagents](</docs/en/sub-agents#api-errors-in-subagents>) for the foreground and background behavior. This partial-output handling requires Claude Code v2.1.199 or later. In v2.1.199, a rate limit, overload, or server error left the tool-calls-only shape with an empty partial result containing only the cutoff note. +An API error that ends the subagent early, such as a rate limit, is never delivered as its result. See [API errors in subagents](</docs/en/sub-agents#api-errors-in-subagents>) for the foreground and background behavior. ## @@ -551,11 +548,7 @@ console.log("No agentId found in the first query, so there is no subagent to resume."); } -Subagent transcripts persist independently of the main conversation: - - * **Main conversation compaction** : when the main conversation compacts, subagent transcripts are unaffected. They’re stored in separate files. - * **Session persistence** : subagent transcripts persist within their session. You can resume a subagent after restarting Claude Code by resuming the same session. - * **Automatic cleanup** : Claude Code deletes subagent transcripts after the `cleanupPeriodDays` retention period, 30 days by default. +Subagent transcripts are stored in separate files and persist independently of the main conversation. See [resume subagents in Claude Code](</docs/en/sub-agents#resume-subagents>) for compaction behavior and the `cleanupPeriodDays` cleanup period. ##
modified
--- a/subagents.md +++ b/subagents.md @@ -672,14 +672,6 @@ For the file format, see [how to write subagent files](</docs/en/sub-agents#write-subagent-files>). -### - -​ - -Long prompt failures on Windows - -On Windows, subagents with very long prompts may fail due to the command line length limit of 8191 characters. Keep prompts concise or use filesystem-based agents for complex instructions. - ## ​
modified
--- a/subagents.md +++ b/subagents.md @@ -555,7 +555,7 @@ * **Main conversation compaction** : when the main conversation compacts, subagent transcripts are unaffected. They’re stored in separate files. * **Session persistence** : subagent transcripts persist within their session. You can resume a subagent after restarting Claude Code by resuming the same session. - * **Automatic cleanup** : transcripts are cleaned up based on the `cleanupPeriodDays` setting, which defaults to 30 days. + * **Automatic cleanup** : Claude Code deletes subagent transcripts after the `cleanupPeriodDays` retention period, 30 days by default. ##
modified
--- a/subagents.md +++ b/subagents.md @@ -191,7 +191,10 @@ * Subagents run in the background by default. An Agent tool call that omits the [`run_in_background`](</docs/en/agent-sdk/typescript>) input launches a background subagent, and Claude sets `run_in_background: false` when it needs the result before continuing. Before v2.1.198, omitting `run_in_background` ran the subagent synchronously. Set the `background` field to `true` to force background execution for a specific agent regardless of what Claude requests. * A subagent inherits the main session’s extended thinking configuration. On earlier versions, extended thinking is disabled inside subagents regardless of the main session’s setting. -By default, subagents can’t spawn subagents of their own. To let them, set [`CLAUDE_CODE_MAX_SUBAGENT_SPAWN_DEPTH`](</docs/en/env-vars>) to the number of subagent layers you want below your main conversation, `2` or higher; see [nested subagents](</docs/en/sub-agents#let-subagents-spawn-their-own-subagents>). From Claude Code v2.1.172 through v2.1.216, subagents could nest by default, up to five layers. +By default, subagents can spawn subagents of their own, up to three layers below the main conversation. To change the limit, set [`CLAUDE_CODE_MAX_SUBAGENT_SPAWN_DEPTH`](</docs/en/env-vars>) to the number of subagent layers you want below your main conversation, or `1` to turn nesting off; see [nested subagents](</docs/en/sub-agents#let-subagents-spawn-their-own-subagents>).Earlier versions used different defaults: + + * **v2.1.172 through v2.1.216** : subagents could nest by default, up to five layers deep, and the limit couldn’t be changed. + * **v2.1.217 through v2.1.218** : the limit defaulted to one, so a subagent couldn’t spawn its own unless you raised it; v2.1.219 raised the default to three. ###
modified
--- a/subagents.md +++ b/subagents.md @@ -174,7 +174,7 @@ ---|---|---|--- `description`| `string`| Yes| Natural language description of when to use this agent `prompt`| `string`| Yes| The agent’s system prompt defining its role and behavior -`tools`| `string[]`| No| Array of allowed tool names. If omitted, inherits all tools +`tools`| `string[]`| No| Array of allowed tool names. If omitted, inherits every [tool available to subagents](</docs/en/sub-agents#available-tools>) `disallowedTools`| `string[]`| No| Array of tool names to remove from the agent’s tool set. MCP server-level patterns are also accepted: `mcp__server` or `mcp__server__*` removes every tool from that server, and `mcp__*` removes every MCP tool from any server `model`| `string`| No| Model override for this agent. Accepts an alias such as `'fable'`, `'opus'`, `'sonnet'`, `'haiku'`, `'inherit'`, or a full model ID. Defaults to main model if omitted `skills`| `string[]`| No| List of skill names to preload into the agent’s context at startup. Unlisted skills remain invocable through the Skill tool @@ -191,7 +191,7 @@ * Subagents run in the background by default. An Agent tool call that omits the [`run_in_background`](</docs/en/agent-sdk/typescript>) input launches a background subagent, and Claude sets `run_in_background: false` when it needs the result before continuing. Before v2.1.198, omitting `run_in_background` ran the subagent synchronously. Set the `background` field to `true` to force background execution for a specific agent regardless of what Claude requests. * A subagent inherits the main session’s extended thinking configuration. On earlier versions, extended thinking is disabled inside subagents regardless of the main session’s setting. -As of Claude Code v2.1.172, subagents can spawn their own subagents. A subagent five levels below the main agent can’t spawn further subagents, regardless of whether it runs in the foreground or background. To prevent a subagent from spawning others, omit `Agent` from its `tools` array or add it to `disallowedTools`. See [nested subagents](</docs/en/sub-agents#spawn-nested-subagents>) for the full depth rules. +By default, subagents can’t spawn subagents of their own. To let them, set [`CLAUDE_CODE_MAX_SUBAGENT_SPAWN_DEPTH`](</docs/en/env-vars>) to the number of subagent layers you want below your main conversation, `2` or higher; see [nested subagents](</docs/en/sub-agents#let-subagents-spawn-their-own-subagents>). From Claude Code v2.1.172 through v2.1.216, subagents could nest by default, up to five layers. ### @@ -215,7 +215,7 @@ ---|--- Its own system prompt (`AgentDefinition.prompt`) and the Agent tool’s prompt| The parent’s conversation history or tool results Project CLAUDE.md (loaded via [`settingSources`](</docs/en/agent-sdk/claude-code-features#control-filesystem-settings-with-settingsources>))| Preloaded skill content, unless listed in `AgentDefinition.skills` -Tool definitions (inherited from parent, or the subset in `tools`)| The parent’s system prompt +Tool definitions (inherited from parent or the subset in `tools`, [filtered for background runs](</docs/en/sub-agents#available-tools>))| The parent’s system prompt The parent receives the subagent’s final message as the Agent tool result, but may summarize it in its own response. To preserve subagent output verbatim in the user-facing response, include an instruction to do so in the prompt or `systemPrompt` option you pass to the main `query()` call.In v2.1.210 and later, Claude Code [scans the final message for instruction-shaped patterns](</docs/en/sub-agents#subagent-output-scanning>) before the parent reads it. The scan treats three kinds of pattern differently: @@ -560,12 +560,12 @@ Tool restrictions -Subagents can have restricted tool access via the `tools` field: - - * **Omit the field** : agent inherits all available tools (default) - * **Specify tools** : agent can only use listed tools - -This example creates a read-only analysis agent that can examine code but can’t modify files or run commands. +Use the `tools` field to limit what a subagent can do: + + * **Omit`tools`**: the subagent gets every [tool available to subagents](</docs/en/sub-agents#available-tools>) + * **List tools** : the subagent gets only those. A code reviewer that should never edit files, for example, gets `["Read", "Grep", "Glob"]` + +A tool you leave out isn’t in the subagent’s session at all: Claude works without it, with no permission prompt or error. This example creates a read-only analysis agent that can examine code but can’t modify files or run commands. Python @@ -626,7 +626,7 @@ Read-only analysis| `Read`, `Grep`, `Glob`| Can examine code but not modify or execute Test execution| `Bash`, `Read`, `Grep`| Can run commands and analyze output Code modification| `Read`, `Edit`, `Write`, `Grep`, `Glob`| Full read/write access without command execution -Full access| All tools| Inherits all tools from parent (omit `tools` field) +Full access| All tools| Inherits the tools available to subagents (omit the `tools` field) ##
modified
--- a/subagents.md +++ b/subagents.md @@ -217,7 +217,13 @@ Project CLAUDE.md (loaded via [`settingSources`](</docs/en/agent-sdk/claude-code-features#control-filesystem-settings-with-settingsources>))| Preloaded skill content, unless listed in `AgentDefinition.skills` Tool definitions (inherited from parent, or the subset in `tools`)| The parent’s system prompt -The parent receives the subagent’s final message verbatim as the Agent tool result, but may summarize it in its own response. To preserve subagent output verbatim in the user-facing response, include an instruction to do so in the prompt or `systemPrompt` option you pass to the main `query()` call. +The parent receives the subagent’s final message as the Agent tool result, but may summarize it in its own response. To preserve subagent output verbatim in the user-facing response, include an instruction to do so in the prompt or `systemPrompt` option you pass to the main `query()` call.In v2.1.210 and later, Claude Code [scans the final message for instruction-shaped patterns](</docs/en/sub-agents#subagent-output-scanning>) before the parent reads it. The scan treats three kinds of pattern differently: + + * **Control-tag imitation** : Claude Code neutralizes a tag that only the harness emits, such as a `<system-reminder>` block, in place. It inserts a backslash after the opening angle bracket and deletes nothing. + * **Permission-configuration mentions** : Claude Code keeps references to the permission configuration, such as `.claude/settings.json`, `bypassPermissions`, or `--dangerously-skip-permissions`, as written. + * **Turn markers** : a line that starts with `Human:` or `Assistant:` gets a backslash before the colon, so the message can’t imitate a conversation turn boundary. + +For a control-tag or permission-configuration match, Claude Code prepends a `[harness: ...]` marker line naming the matched patterns; a turn-marker match doesn’t add the marker line. Those are the only modifications the scan makes: it never removes or rewords the subagent’s text. An API error that ends the subagent early, such as a rate limit, is never delivered as its result. If a rate limit, overload, or server error cuts off a foreground subagent that already produced text output, the Agent tool returns that partial output with a note that the subagent didn’t finish. A subagent that produced nothing, or whose only output was tool calls with no text, fails with an error message, `Agent terminated early due to an API error`, followed by the error detail. See [API errors in subagents](</docs/en/sub-agents#api-errors-in-subagents>) for the foreground and background behavior. This partial-output handling requires Claude Code v2.1.199 or later. In v2.1.199, a rate limit, overload, or server error left the tool-calls-only shape with an empty partial result containing only the cutoff note.
modified
--- a/subagents.md +++ b/subagents.md @@ -209,7 +209,7 @@ What subagents inherit -A subagent’s context window starts fresh, with no parent conversation, but isn’t empty. The only channel from parent to subagent is the Agent tool’s prompt string, so include any file paths, error messages, or decisions the subagent needs directly in that prompt. +A subagent’s context window starts fresh, with no parent conversation, but isn’t empty. The only content you pass from parent to subagent is the Agent tool’s prompt string, so include any file paths, error messages, or decisions the subagent needs directly in that prompt. A subagent that has the [`SendMessage`](</docs/en/tools-reference>) tool starts with a list of the other named agents running in the session, so it knows which names it can send messages to. Claude Code adds the list to the subagent’s first turn automatically. A [fork](</docs/en/sub-agents#fork-the-current-conversation>) doesn’t get the list because it inherits the parent conversation instead. The list requires Claude Code v2.1.206 or later. The subagent receives| The subagent doesn’t receive ---|---
modified
Show diff (221 lines)
--- a/subagents.md +++ b/subagents.md @@ -117,6 +117,52 @@ print(message.result) asyncio.run(main()) + + import { query } from "@anthropic-ai/claude-agent-sdk"; + + for await (const message of query({ + prompt: "Review the authentication module for security issues", + options: { + // Auto-approve these tools, including Agent for subagent invocation + allowedTools: ["Read", "Grep", "Glob", "Agent"], + agents: { + "code-reviewer": { + // description tells Claude when to use this subagent + description: + "Expert code review specialist. Use for quality, security, and maintainability reviews.", + // prompt defines the subagent's behavior and expertise + prompt: `You are a code review specialist with expertise in security, performance, and best practices. + + When reviewing code: + - Identify security vulnerabilities + - Check for performance issues + - Verify adherence to coding standards + - Suggest specific improvements + + Be thorough but concise in your feedback.`, + // tools restricts what the subagent can do (read-only here) + tools: ["Read", "Grep", "Glob"], + // model overrides the default model for this subagent + model: "sonnet" + }, + "test-runner": { + description: + "Runs and analyzes test suites. Use for test execution and coverage analysis.", + prompt: `You are a test execution specialist. Run tests and provide clear analysis of results. + + Focus on: + - Running test commands + - Analyzing test output + - Identifying failing tests + - Suggesting fixes for failures`, + // Bash access lets this subagent run test commands + tools: ["Bash", "Read", "Grep"] + } + } + } + })) { + if ("result" in message) console.log(message.result); + } ### @@ -245,6 +291,36 @@ print(message.result) asyncio.run(main()) + + import { query, type AgentDefinition } from "@anthropic-ai/claude-agent-sdk"; + + // Factory function that returns an AgentDefinition + // This pattern lets you customize agents based on runtime conditions + function createSecurityAgent(securityLevel: "basic" | "strict"): AgentDefinition { + const isStrict = securityLevel === "strict"; + return { + description: "Security code reviewer", + // Customize the prompt based on strictness level + prompt: `You are a ${isStrict ? "strict" : "balanced"} security reviewer...`, + tools: ["Read", "Grep", "Glob"], + // Key insight: use a more capable model for high-stakes reviews + model: isStrict ? "opus" : "sonnet" + }; + } + + // The agent is created at query time, so each request can use different settings + for await (const message of query({ + prompt: "Review this PR for security issues", + options: { + allowedTools: ["Read", "Grep", "Glob", "Agent"], + agents: { + // Call the factory with your desired configuration + "security-reviewer": createSecurityAgent("strict") + } + } + })) { + if ("result" in message) console.log(message.result); + } ## @@ -298,6 +374,41 @@ asyncio.run(main()) + import { query } from "@anthropic-ai/claude-agent-sdk"; + + for await (const message of query({ + prompt: "Use the code-reviewer agent to review this codebase", + options: { + allowedTools: ["Read", "Glob", "Grep", "Agent"], + agents: { + "code-reviewer": { + description: "Expert code reviewer.", + prompt: "Analyze code quality and suggest improvements.", + tools: ["Read", "Glob", "Grep"] + } + } + } + })) { + const msg = message as any; + + // Check for subagent invocation. Match both names: older SDK versions + // emitted "Task", current versions emit "Agent". + for (const block of msg.message?.content ?? []) { + if (block.type === "tool_use" && (block.name === "Task" || block.name === "Agent")) { + console.log(`Subagent invoked: ${block.input.subagent_type}`); + } + } + + // Check if this message is from within a subagent's context + if (msg.parent_tool_use_id) { + console.log(" (running inside subagent)"); + } + + if ("result" in message) { + console.log(message.result); + } + } + ## ​ @@ -378,6 +489,59 @@ asyncio.run(main()) + import { query, type SDKMessage } from "@anthropic-ai/claude-agent-sdk"; + + const agents = { + "endpoint-finder": { + description: "Locates and catalogs API endpoints in a codebase.", + prompt: "You find and document API endpoints. Report each endpoint's path, method, and handler.", + tools: ["Read", "Grep", "Glob"] + } + }; + + // Stringify content to search for agentId without traversing nested block types + function extractAgentId(message: SDKMessage): string | undefined { + if (message.type !== "assistant" && message.type !== "user") return undefined; + const content = JSON.stringify(message.message.content); + const match = content.match(/agentId:\s*([\w-]+)/); + return match?.[1]; + } + + let agentId: string | undefined; + let sessionId: string | undefined; + + // First invocation - run the endpoint-finder subagent + try { + for await (const message of query({ + prompt: "Use the endpoint-finder agent to find all API endpoints in this codebase", + options: { allowedTools: ["Read", "Grep", "Glob", "Agent"], agents } + })) { + // Capture session_id from ResultMessage (needed to resume this session) + if ("session_id" in message) sessionId = message.session_id; + // Search message content for the agentId (appears in Agent tool results) + const extractedId = extractAgentId(message); + if (extractedId) agentId = extractedId; + // Print the final result + if ("result" in message) console.log(message.result); + } + } catch (error) { + // A single-shot query() throws after yielding an error result, + // so sessionId and agentId have already been captured by the loop above. + console.error(`Session ended with an error: ${error}`); + } + + // Second invocation - resume and ask follow-up + if (agentId && sessionId) { + for await (const message of query({ + prompt: `Resume agent ${agentId} and list the top 3 most complex endpoints`, + options: { allowedTools: ["Read", "Grep", "Glob", "Agent"], agents, resume: sessionId } + })) { + if ("result" in message) console.log(message.result); + } + } else { + console.log("No agentId found in the first query, so there is no subagent to resume."); + } + Subagent transcripts persist independently of the main conversation: * **Main conversation compaction** : when the main conversation compacts, subagent transcripts are unaffected. They’re stored in separate files. @@ -425,6 +589,26 @@ asyncio.run(main()) + import { query } from "@anthropic-ai/claude-agent-sdk"; + + for await (const message of query({ + prompt: "Analyze the architecture of this codebase", + options: { + allowedTools: ["Read", "Grep", "Glob", "Agent"], + agents: { + "code-analyzer": { + description: "Static code analysis and architecture review", + prompt: `You are a code architecture analyst. Analyze code structure, + identify patterns, and suggest improvements without making changes.`, + // Read-only tools: no Edit, Write, or Bash access + tools: ["Read", "Grep", "Glob"] + } + } + } + })) { + if ("result" in message) console.log(message.result); + } + ### ​
modified
--- a/subagents.md +++ b/subagents.md @@ -173,7 +173,7 @@ The parent receives the subagent’s final message verbatim as the Agent tool result, but may summarize it in its own response. To preserve subagent output verbatim in the user-facing response, include an instruction to do so in the prompt or `systemPrompt` option you pass to the main `query()` call. -As of Claude Code v2.1.199, an API error that ends the subagent early, such as a rate limit, is never delivered as its result. If the subagent already produced output, the Agent tool returns that partial output with a note that the subagent didn’t finish; otherwise the tool result is an error message, `Agent terminated early due to an API error`, followed by the error detail. See [API errors in subagents](</docs/en/sub-agents#api-errors-in-subagents>) for the foreground and background behavior. +An API error that ends the subagent early, such as a rate limit, is never delivered as its result. If a rate limit, overload, or server error cuts off a foreground subagent that already produced text output, the Agent tool returns that partial output with a note that the subagent didn’t finish. A subagent that produced nothing, or whose only output was tool calls with no text, fails with an error message, `Agent terminated early due to an API error`, followed by the error detail. See [API errors in subagents](</docs/en/sub-agents#api-errors-in-subagents>) for the foreground and background behavior. This partial-output handling requires Claude Code v2.1.199 or later. In v2.1.199, a rate limit, overload, or server error left the tool-calls-only shape with an empty partial result containing only the cutoff note. ##
modified
Show diff (197 lines)
--- a/subagents.md +++ b/subagents.md @@ -1,4 +1,4 @@ -Subagents are separate agent instances that your main agent can spawn to handle focused subtasks. Use subagents to isolate context for focused subtasks, run multiple analyses in parallel, and apply specialized instructions without adding to the main agent’s prompt. This guide explains how to define and use subagents in the SDK using the `agents` parameter. +Subagents are separate agent instances that your main agent can spawn to handle focused subtasks. Use them to isolate context, run multiple analyses in parallel, and apply specialized instructions without adding to the main agent’s prompt. This guide explains how to define and use subagents in the SDK using the `agents` parameter. ## @@ -8,11 +8,11 @@ You can create subagents in three ways: - * **Programmatically** : use the `agents` parameter in your `query()` options ([TypeScript](</docs/en/agent-sdk/typescript#agentdefinition>), [Python](</docs/en/agent-sdk/python#agentdefinition>)) - * **Filesystem-based** : define agents as markdown files in `.claude/agents/` directories (see [defining subagents as files](</docs/en/sub-agents>)) + * **Programmatically** : use the `agents` parameter in your `query()` options. See the [TypeScript](</docs/en/agent-sdk/typescript#agentdefinition>) and [Python](</docs/en/agent-sdk/python#agentdefinition>) references + * **Filesystem-based** : define agents as markdown files in `.claude/agents/` directories. See [defining subagents as files](</docs/en/sub-agents>) * **Built-in general-purpose** : Claude can invoke the built-in `general-purpose` subagent at any time via the Agent tool without you defining anything -This guide focuses on the programmatic approach, which is recommended for SDK applications. When you define subagents, Claude determines whether to invoke them based on each subagent’s `description` field. Write clear descriptions that explain when the subagent should be used, and Claude will automatically delegate appropriate tasks. You can also explicitly request a subagent by name in your prompt (for example, “Use the code-reviewer agent to…”). +This guide focuses on the programmatic approach, which is recommended for SDK applications. When you define subagents, Claude determines whether to invoke them based on each subagent’s `description` field. Write clear descriptions that explain when to use the subagent, and Claude automatically delegates appropriate tasks. You can also explicitly request a subagent by name in your prompt, for example “Use the code-reviewer agent to…”. ## @@ -56,7 +56,7 @@ ​ -Creating subagents +Create subagents ### @@ -64,7 +64,7 @@ Programmatic definition (recommended) -Define subagents directly in your code using the `agents` parameter. This example creates two subagents: a code reviewer with read-only access and a test runner that can execute commands. Claude invokes subagents through the `Agent` tool, so include `Agent` in `allowedTools` to auto-approve subagent invocations without a permission prompt. Most examples on this page print only the final result. To confirm that Claude delegated to a subagent rather than answering directly, see Detecting subagent invocation. +Define subagents directly in your code using the `agents` parameter. Claude invokes subagents through the `Agent` tool, so include `Agent` in `allowedTools` to auto-approve subagent invocations without a permission prompt. Most examples on this page print only the final result. To confirm that Claude delegated to a subagent rather than answering directly, see Detect subagent invocation. This example creates two subagents: a code reviewer with read-only access and a test runner that can execute commands. Python @@ -140,9 +140,12 @@ `effort`| `'low' | 'medium' | 'high' | 'xhigh' | 'max' | number`| No| Reasoning effort level for this agent `permissionMode`| `PermissionMode`| No| Permission mode for tool execution within this agent -In the Python SDK, multi-word field names such as `disallowedTools` and `mcpServers` keep their camelCase spelling to match the wire format rather than following Python’s snake_case convention. See the [`AgentDefinition` reference](</docs/en/agent-sdk/python#agentdefinition>) for details. - -As of Claude Code v2.1.172, subagents can spawn their own subagents. A subagent five levels below the main agent cannot spawn further subagents, regardless of whether it runs in the foreground or background. To prevent a subagent from spawning others, omit `Agent` from its `tools` array or add it to `disallowedTools`. See [nested subagents](</docs/en/sub-agents#spawn-nested-subagents>) for the full depth rules. +In the Python SDK, multi-word field names such as `disallowedTools` and `mcpServers` keep their camelCase spelling to match the wire format rather than following Python’s snake_case convention. See the [`AgentDefinition` reference](</docs/en/agent-sdk/python#agentdefinition>) for details. Two subagent behaviors changed in Claude Code v2.1.198: + + * Subagents run in the background by default. An Agent tool call that omits the [`run_in_background`](</docs/en/agent-sdk/typescript>) input launches a background subagent, and Claude sets `run_in_background: false` when it needs the result before continuing. Before v2.1.198, omitting `run_in_background` ran the subagent synchronously. Set the `background` field to `true` to force background execution for a specific agent regardless of what Claude requests. + * A subagent inherits the main session’s extended thinking configuration. On earlier versions, extended thinking is disabled inside subagents regardless of the main session’s setting. + +As of Claude Code v2.1.172, subagents can spawn their own subagents. A subagent five levels below the main agent can’t spawn further subagents, regardless of whether it runs in the foreground or background. To prevent a subagent from spawning others, omit `Agent` from its `tools` array or add it to `disallowedTools`. See [nested subagents](</docs/en/sub-agents#spawn-nested-subagents>) for the full depth rules. ### @@ -160,21 +163,23 @@ What subagents inherit -A subagent’s context window starts fresh (no parent conversation) but isn’t empty. The only channel from parent to subagent is the Agent tool’s prompt string, so include any file paths, error messages, or decisions the subagent needs directly in that prompt. - -The subagent receives| The subagent does not receive +A subagent’s context window starts fresh, with no parent conversation, but isn’t empty. The only channel from parent to subagent is the Agent tool’s prompt string, so include any file paths, error messages, or decisions the subagent needs directly in that prompt. + +The subagent receives| The subagent doesn’t receive ---|--- Its own system prompt (`AgentDefinition.prompt`) and the Agent tool’s prompt| The parent’s conversation history or tool results Project CLAUDE.md (loaded via [`settingSources`](</docs/en/agent-sdk/claude-code-features#control-filesystem-settings-with-settingsources>))| Preloaded skill content, unless listed in `AgentDefinition.skills` Tool definitions (inherited from parent, or the subset in `tools`)| The parent’s system prompt -The parent receives the subagent’s final message verbatim as the Agent tool result, but may summarize it in its own response. To preserve subagent output verbatim in the user-facing response, include an instruction to do so in the prompt or `systemPrompt` option you pass to the **main** `query()` call. - -## - -​ - -Invoking subagents +The parent receives the subagent’s final message verbatim as the Agent tool result, but may summarize it in its own response. To preserve subagent output verbatim in the user-facing response, include an instruction to do so in the prompt or `systemPrompt` option you pass to the main `query()` call. + +As of Claude Code v2.1.199, an API error that ends the subagent early, such as a rate limit, is never delivered as its result. If the subagent already produced output, the Agent tool returns that partial output with a note that the subagent didn’t finish; otherwise the tool result is an error message, `Agent terminated early due to an API error`, followed by the error detail. See [API errors in subagents](</docs/en/sub-agents#api-errors-in-subagents>) for the foreground and background behavior. + +## + +​ + +Invoke subagents ### @@ -245,13 +250,13 @@ ​ -Detecting subagent invocation - -Subagents are invoked via 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. +Detect subagent invocation + +Claude 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. The tool name was renamed from `"Task"` to `"Agent"` in Claude Code v2.1.63. Current SDK releases emit `"Agent"` in `tool_use` blocks but still use `"Task"` in the `system:init` tools list and in `result.permission_denials[].tool_name`. Checking both values in `block.name` ensures compatibility across SDK versions. -The message structure differs between SDKs. In Python, content blocks are accessed directly via `message.content`. In TypeScript, `SDKAssistantMessage` wraps the Claude API message, so content is accessed via `message.message.content`. This example iterates through streamed messages, logging when a subagent is invoked and when subsequent messages originate from within that subagent’s execution context. +The 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`. This example iterates through streamed messages, logging when a subagent is invoked and when subsequent messages originate from within that subagent’s execution context. Python @@ -297,13 +302,13 @@ ​ -Resuming subagents - -Subagents can be resumed to continue where they left off. Resumed subagents retain their full conversation history, including all previous tool calls, results, and reasoning. The subagent picks up exactly where it stopped rather than starting fresh. When a subagent completes, the Agent tool result includes a text block containing `agentId: <id>`. The built-in [`Explore` and `Plan` agents](</docs/en/sub-agents#built-in-subagents>) are one-shot and do not return an `agentId`, so use a custom agent or `general-purpose` when you need to resume. To resume a subagent programmatically: - - 1. **Capture the session ID** : Extract `session_id` from messages during the first query - 2. **Extract the agent ID** : Parse `agentId` from the Agent tool result text - 3. **Resume the session** : Pass `resume: sessionId` in the second query’s options, and include the agent ID in your prompt +Resume subagents + +You can resume a subagent to continue where it left off rather than starting fresh. A resumed subagent retains its full conversation history, including all previous tool calls, results, and reasoning. When a subagent completes, the Agent tool result includes a text block containing `agentId: <id>`. The built-in [`Explore` and `Plan` agents](</docs/en/sub-agents#built-in-subagents>) are one-shot and don’t return an `agentId`, so use a custom agent or `general-purpose` when you need to resume. To resume a subagent programmatically: + + 1. **Capture the session ID** : extract `session_id` from messages during the first query + 2. **Extract the agent ID** : parse `agentId` from the Agent tool result text + 3. **Resume the session** : pass `resume: sessionId` in the second query’s options, and include the agent ID in your prompt You must resume the same session to access the subagent’s transcript. Each `query()` call starts a new session by default, so pass `resume: sessionId` to continue in the same session.When using a custom agent, pass the same agent definition in the `agents` parameter for both queries. @@ -375,9 +380,9 @@ Subagent transcripts persist independently of the main conversation: - * **Main conversation compaction** : When the main conversation compacts, subagent transcripts are unaffected. They’re stored in separate files. - * **Session persistence** : Subagent transcripts persist within their session. You can resume a subagent after restarting Claude Code by resuming the same session. - * **Automatic cleanup** : Transcripts are cleaned up based on the `cleanupPeriodDays` setting (default: 30 days). + * **Main conversation compaction** : when the main conversation compacts, subagent transcripts are unaffected. They’re stored in separate files. + * **Session persistence** : subagent transcripts persist within their session. You can resume a subagent after restarting Claude Code by resuming the same session. + * **Automatic cleanup** : transcripts are cleaned up based on the `cleanupPeriodDays` setting, which defaults to 30 days. ## @@ -390,7 +395,7 @@ * **Omit the field** : agent inherits all available tools (default) * **Specify tools** : agent can only use listed tools -This example creates a read-only analysis agent that can examine code but cannot modify files or run commands. +This example creates a read-only analysis agent that can examine code but can’t modify files or run commands. Python @@ -455,9 +460,9 @@ If Claude completes tasks directly instead of delegating to your subagent: - 1. **Check Agent invocations are approved** : include `Agent` in `allowedTools` to auto-approve subagent calls. Without it, Agent invocations fall through to your `canUseTool` callback or, in `dontAsk` mode, are denied - 2. **Use explicit prompting** : mention the subagent by name in your prompt (for example, “Use the code-reviewer agent to…”) - 3. **Write a clear description** : explain exactly when the subagent should be used so Claude can match tasks appropriately + * **Check Agent invocations are approved** : include `Agent` in `allowedTools` to auto-approve subagent calls. Without it, Agent invocations fall through to your `canUseTool` callback or, in `dontAsk` mode, are denied + * **Use explicit prompting** : mention the subagent by name in your prompt, for example “Use the code-reviewer agent to…” + * **Write a clear description** : explain exactly when to use the subagent so Claude can match tasks appropriately ### @@ -465,15 +470,22 @@ Filesystem-based agents not loading -Agents defined in `.claude/agents/` are loaded at startup only. If you create a new agent file while Claude Code is running, restart the session to load it. - -### - -​ - -Windows: long prompt failures - -On Windows, subagents with very long prompts may fail due to command line length limits (8191 chars). Keep prompts concise or use filesystem-based agents for complex instructions. +Claude Code watches `~/.claude/agents/` and `.claude/agents/` and picks up a new or edited agent file within a few seconds, with no restart needed. If a definition never appears, work through these causes: + + * **New`agents` directory**: the watcher covers only directories that existed when the session started, so the first file in a new directory needs a session restart. This is the most common cause. + * **Invalid frontmatter or a duplicate`name`**: check the file’s YAML, and whether an existing agent already uses the `name`. + * **`--disable-slash-commands`** : sessions started with this flag don’t watch these directories and always need a restart to load new files. + * **A programmatic agent with the same name** : `agents` passed to `query()` override a filesystem agent with the same name. + +For the file format, see [how to write subagent files](</docs/en/sub-agents#write-subagent-files>). + +### + +​ + +Long prompt failures on Windows + +On Windows, subagents with very long prompts may fail due to the command line length limit of 8191 characters. Keep prompts concise or use filesystem-based agents for complex instructions. ##
modified
--- a/subagents.md +++ b/subagents.md @@ -64,7 +64,7 @@ Programmatic definition (recommended) -Define subagents directly in your code using the `agents` parameter. This example creates two subagents: a code reviewer with read-only access and a test runner that can execute commands. Claude invokes subagents through the `Agent` tool, so include `Agent` in `allowedTools` to auto-approve subagent invocations without a permission prompt. +Define subagents directly in your code using the `agents` parameter. This example creates two subagents: a code reviewer with read-only access and a test runner that can execute commands. Claude invokes subagents through the `Agent` tool, so include `Agent` in `allowedTools` to auto-approve subagent invocations without a permission prompt. Most examples on this page print only the final result. To confirm that Claude delegated to a subagent rather than answering directly, see Detecting subagent invocation. Python @@ -140,7 +140,7 @@ `effort`| `'low' | 'medium' | 'high' | 'xhigh' | 'max' | number`| No| Reasoning effort level for this agent `permissionMode`| `PermissionMode`| No| Permission mode for tool execution within this agent -In the Python SDK, these field names use camelCase to match the wire format. See the [`AgentDefinition` reference](</docs/en/agent-sdk/python#agentdefinition>) for details. +In the Python SDK, multi-word field names such as `disallowedTools` and `mcpServers` keep their camelCase spelling to match the wire format rather than following Python’s snake_case convention. See the [`AgentDefinition` reference](</docs/en/agent-sdk/python#agentdefinition>) for details. As of Claude Code v2.1.172, subagents can spawn their own subagents. A subagent five levels below the main agent cannot spawn further subagents, regardless of whether it runs in the foreground or background. To prevent a subagent from spawning others, omit `Agent` from its `tools` array or add it to `disallowedTools`. See [nested subagents](</docs/en/sub-agents#spawn-nested-subagents>) for the full depth rules. @@ -338,20 +338,25 @@ session_id = None # First invocation - run the endpoint-finder subagent - async for message in query( - prompt="Use the endpoint-finder agent to find all API endpoints in this codebase", - options=ClaudeAgentOptions(allowed_tools=["Read", "Grep", "Glob", "Agent"], agents=AGENTS), - ): - # Capture session_id from ResultMessage (needed to resume this session) - if hasattr(message, "session_id"): - session_id = message.session_id - # Search tool results for the agentId trailer - for block in getattr(message, "content", None) or []: - if isinstance(block, ToolResultBlock): - agent_id = extract_agent_id(block) or agent_id - # Print the final result - if hasattr(message, "result"): - print(message.result) + try: + async for message in query( + prompt="Use the endpoint-finder agent to find all API endpoints in this codebase", + options=ClaudeAgentOptions(allowed_tools=["Read", "Grep", "Glob", "Agent"], agents=AGENTS), + ): + # Capture session_id from ResultMessage (needed to resume this session) + if hasattr(message, "session_id"): + session_id = message.session_id + # Search tool results for the agentId trailer + for block in getattr(message, "content", None) or []: + if isinstance(block, ToolResultBlock): + agent_id = extract_agent_id(block) or agent_id + # Print the final result + if hasattr(message, "result"): + print(message.result) + except Exception as error: + # A single-shot query() raises after yielding an error result, + # so session_id and agent_id have already been captured by the loop above. + print(f"Session ended with an error: {error}") # Second invocation - resume and ask follow-up if agent_id and session_id: @@ -363,6 +368,8 @@ ): if hasattr(message, "result"): print(message.result) + else: + print("No agentId found in the first query, so there is no subagent to resume.") asyncio.run(main())
modified
--- a/subagents.md +++ b/subagents.md @@ -142,7 +142,7 @@ In the Python SDK, these field names use camelCase to match the wire format. See the [`AgentDefinition` reference](</docs/en/agent-sdk/python#agentdefinition>) for details. -As of Claude Code v2.1.172, subagents can spawn their own subagents. A background subagent five levels below the main agent cannot spawn further subagents; foreground subagents can spawn at any depth. To prevent a subagent from spawning others, omit `Agent` from its `tools` array or add it to `disallowedTools`. See [nested subagents](</docs/en/sub-agents#spawn-nested-subagents>) for the full depth rules. +As of Claude Code v2.1.172, subagents can spawn their own subagents. A subagent five levels below the main agent cannot spawn further subagents, regardless of whether it runs in the foreground or background. To prevent a subagent from spawning others, omit `Agent` from its `tools` array or add it to `disallowedTools`. See [nested subagents](</docs/en/sub-agents#spawn-nested-subagents>) for the full depth rules. ###
modified
--- a/subagents.md +++ b/subagents.md @@ -129,7 +129,7 @@ `description`| `string`| Yes| Natural language description of when to use this agent `prompt`| `string`| Yes| The agent’s system prompt defining its role and behavior `tools`| `string[]`| No| Array of allowed tool names. If omitted, inherits all tools -`disallowedTools`| `string[]`| No| Array of tool names to remove from the agent’s tool set +`disallowedTools`| `string[]`| No| Array of tool names to remove from the agent’s tool set. MCP server-level patterns are also accepted: `mcp__server` or `mcp__server__*` removes every tool from that server, and `mcp__*` removes every MCP tool from any server `model`| `string`| No| Model override for this agent. Accepts an alias such as `'fable'`, `'opus'`, `'sonnet'`, `'haiku'`, `'inherit'`, or a full model ID. Defaults to main model if omitted `skills`| `string[]`| No| List of skill names to preload into the agent’s context at startup. Unlisted skills remain invocable through the Skill tool `memory`| `'user' | 'project' | 'local'`| No| Memory source for this agent
modified
--- a/subagents.md +++ b/subagents.md @@ -142,7 +142,7 @@ In the Python SDK, these field names use camelCase to match the wire format. See the [`AgentDefinition` reference](</docs/en/agent-sdk/python#agentdefinition>) for details. -Subagents cannot spawn their own subagents. Don’t include `Agent` in a subagent’s `tools` array. +As of Claude Code v2.1.172, subagents can spawn their own subagents. A background subagent five levels below the main agent cannot spawn further subagents; foreground subagents can spawn at any depth. To prevent a subagent from spawning others, omit `Agent` from its `tools` array or add it to `disallowedTools`. See [nested subagents](</docs/en/sub-agents#spawn-nested-subagents>) for the full depth rules. ###
modified
--- a/subagents.md +++ b/subagents.md @@ -130,11 +130,11 @@ `prompt`| `string`| Yes| The agent’s system prompt defining its role and behavior `tools`| `string[]`| No| Array of allowed tool names. If omitted, inherits all tools `disallowedTools`| `string[]`| No| Array of tool names to remove from the agent’s tool set -`model`| `string`| No| Model override for this agent. Accepts an alias such as `'sonnet'`, `'opus'`, `'haiku'`, `'inherit'`, or a full model ID. Defaults to main model if omitted +`model`| `string`| No| Model override for this agent. Accepts an alias such as `'fable'`, `'opus'`, `'sonnet'`, `'haiku'`, `'inherit'`, or a full model ID. Defaults to main model if omitted `skills`| `string[]`| No| List of skill names to preload into the agent’s context at startup. Unlisted skills remain invocable through the Skill tool `memory`| `'user' | 'project' | 'local'`| No| Memory source for this agent `mcpServers`| `(string | object)[]`| No| MCP servers available to this agent, by name or inline config -`initialPrompt`| `string`| No| Auto-submitted as the first user turn when this agent runs as the main thread agent +`initialPrompt`| `string`| No| Auto-submitted as the first user turn when this agent runs as the main thread agent. Ignored when the agent is invoked as a subagent `maxTurns`| `number`| No| Maximum number of agentic turns before the agent stops `background`| `boolean`| No| Run this agent as a non-blocking background task when invoked `effort`| `'low' | 'medium' | 'high' | 'xhigh' | 'max' | number`| No| Reasoning effort level for this agent @@ -299,7 +299,7 @@ Resuming subagents -Subagents can be resumed to continue where they left off. Resumed subagents retain their full conversation history, including all previous tool calls, results, and reasoning. The subagent picks up exactly where it stopped rather than starting fresh. When a subagent completes, the Agent tool result includes a text block containing `agentId: <id>`. The built-in [`Explore` and `Plan` agents](</docs/en/sub-agents#built-in-subagents>) are one-shot and omit this trailer, so use a custom agent or `general-purpose` when you need to resume. To resume a subagent programmatically: +Subagents can be resumed to continue where they left off. Resumed subagents retain their full conversation history, including all previous tool calls, results, and reasoning. The subagent picks up exactly where it stopped rather than starting fresh. When a subagent completes, the Agent tool result includes a text block containing `agentId: <id>`. The built-in [`Explore` and `Plan` agents](</docs/en/sub-agents#built-in-subagents>) are one-shot and do not return an `agentId`, so use a custom agent or `general-purpose` when you need to resume. To resume a subagent programmatically: 1. **Capture the session ID** : Extract `session_id` from messages during the first query 2. **Extract the agent ID** : Parse `agentId` from the Agent tool result text
modified
Show diff (161 lines)
--- a/subagents.md +++ b/subagents.md @@ -1,4 +1,4 @@ -Subagents are separate agent instances that your main agent can spawn to handle focused subtasks. Use subagents to isolate context for focused subtasks, run multiple analyses in parallel, and apply specialized instructions without bloating the main agent’s prompt. This guide explains how to define and use subagents in the SDK using the `agents` parameter. +Subagents are separate agent instances that your main agent can spawn to handle focused subtasks. Use subagents to isolate context for focused subtasks, run multiple analyses in parallel, and apply specialized instructions without adding to the main agent’s prompt. This guide explains how to define and use subagents in the SDK using the `agents` parameter. ## @@ -34,7 +34,7 @@ Parallelization -Multiple subagents can run concurrently, dramatically speeding up complex workflows. **Example:** during a code review, you can run `style-checker`, `security-scanner`, and `test-coverage` subagents simultaneously, reducing review time from minutes to seconds. +Multiple subagents can run concurrently, so independent subtasks finish in the time of the slowest one rather than the sum of all of them. **Example:** during a code review, you can run `style-checker`, `security-scanner`, and `test-coverage` subagents simultaneously instead of sequentially. ### @@ -134,6 +134,7 @@ `skills`| `string[]`| No| List of skill names to preload into the agent’s context at startup. Unlisted skills remain invocable through the Skill tool `memory`| `'user' | 'project' | 'local'`| No| Memory source for this agent `mcpServers`| `(string | object)[]`| No| MCP servers available to this agent, by name or inline config +`initialPrompt`| `string`| No| Auto-submitted as the first user turn when this agent runs as the main thread agent `maxTurns`| `number`| No| Maximum number of agentic turns before the agent stops `background`| `boolean`| No| Run this agent as a non-blocking background task when invoked `effort`| `'low' | 'medium' | 'high' | 'xhigh' | 'max' | number`| No| Reasoning effort level for this agent @@ -164,7 +165,7 @@ The subagent receives| The subagent does not receive ---|--- Its own system prompt (`AgentDefinition.prompt`) and the Agent tool’s prompt| The parent’s conversation history or tool results -Project CLAUDE.md (loaded via `settingSources`)| Preloaded skill content, unless listed in `AgentDefinition.skills` +Project CLAUDE.md (loaded via [`settingSources`](</docs/en/agent-sdk/claude-code-features#control-filesystem-settings-with-settingsources>))| Preloaded skill content, unless listed in `AgentDefinition.skills` Tool definitions (inherited from parent, or the subset in `tools`)| The parent’s system prompt The parent receives the subagent’s final message verbatim as the Agent tool result, but may summarize it in its own response. To preserve subagent output verbatim in the user-facing response, include an instruction to do so in the prompt or `systemPrompt` option you pass to the **main** `query()` call. @@ -250,9 +251,7 @@ The tool name was renamed from `"Task"` to `"Agent"` in Claude Code v2.1.63. Current SDK releases emit `"Agent"` in `tool_use` blocks but still use `"Task"` in the `system:init` tools list and in `result.permission_denials[].tool_name`. Checking both values in `block.name` ensures compatibility across SDK versions. -This example iterates through streamed messages, logging when a subagent is invoked and when subsequent messages originate from within that subagent’s execution context. - -The message structure differs between SDKs. In Python, content blocks are accessed directly via `message.content`. In TypeScript, `SDKAssistantMessage` wraps the Claude API message, so content is accessed via `message.message.content`. +The message structure differs between SDKs. In Python, content blocks are accessed directly via `message.content`. In TypeScript, `SDKAssistantMessage` wraps the Claude API message, so content is accessed via `message.message.content`. This example iterates through streamed messages, logging when a subagent is invoked and when subsequent messages originate from within that subagent’s execution context. Python @@ -300,58 +299,72 @@ Resuming subagents -Subagents can be resumed to continue where they left off. Resumed subagents retain their full conversation history, including all previous tool calls, results, and reasoning. The subagent picks up exactly where it stopped rather than starting fresh. When a subagent completes, Claude receives its agent ID in the Agent tool result. To resume a subagent programmatically: +Subagents can be resumed to continue where they left off. Resumed subagents retain their full conversation history, including all previous tool calls, results, and reasoning. The subagent picks up exactly where it stopped rather than starting fresh. When a subagent completes, the Agent tool result includes a text block containing `agentId: <id>`. The built-in [`Explore` and `Plan` agents](</docs/en/sub-agents#built-in-subagents>) are one-shot and omit this trailer, so use a custom agent or `general-purpose` when you need to resume. To resume a subagent programmatically: 1. **Capture the session ID** : Extract `session_id` from messages during the first query - 2. **Extract the agent ID** : Parse `agentId` from the message content + 2. **Extract the agent ID** : Parse `agentId` from the Agent tool result text 3. **Resume the session** : Pass `resume: sessionId` in the second query’s options, and include the agent ID in your prompt -You must resume the same session to access the subagent’s transcript. Each `query()` call starts a new session by default, so pass `resume: sessionId` to continue in the same session.If you’re using a custom agent (not a built-in one), you also need to pass the same agent definition in the `agents` parameter for both queries. - -The example below demonstrates this flow: the first query runs a subagent and captures the session ID and agent ID, then the second query resumes the session to ask a follow-up question that requires context from the first analysis. +You must resume the same session to access the subagent’s transcript. Each `query()` call starts a new session by default, so pass `resume: sessionId` to continue in the same session.When using a custom agent, pass the same agent definition in the `agents` parameter for both queries. + +The example below defines a custom `endpoint-finder` agent. The first query runs it and captures the session ID and agent ID from the Agent tool result, then the second query resumes the session to ask a follow-up question that requires context from the first analysis. + +Python TypeScript -Python - - import { query, type SDKMessage } from "@anthropic-ai/claude-agent-sdk"; - - // Helper to extract agentId from message content - // Stringify to avoid traversing different block types (TextBlock, ToolResultBlock, etc.) - function extractAgentId(message: SDKMessage): string | undefined { - if (message.type !== "assistant" && message.type !== "user") return undefined; - // Stringify the content so we can search it without traversing nested blocks - const content = JSON.stringify(message.message.content); - const match = content.match(/agentId:\s*([a-f0-9-]+)/); - return match?.[1]; + import asyncio + import re + from claude_agent_sdk import query, ClaudeAgentOptions, AgentDefinition, ToolResultBlock + + AGENTS = { + "endpoint-finder": AgentDefinition( + description="Locates and catalogs API endpoints in a codebase.", + prompt="You find and document API endpoints. Report each endpoint's path, method, and handler.", + tools=["Read", "Grep", "Glob"], + ) } - let agentId: string | undefined; - let sessionId: string | undefined; - - // First invocation - use the Explore agent to find API endpoints - for await (const message of query({ - prompt: "Use the Explore agent to find all API endpoints in this codebase", - options: { allowedTools: ["Read", "Grep", "Glob", "Agent"] } - })) { - // Capture session_id from ResultMessage (needed to resume this session) - if ("session_id" in message) sessionId = message.session_id; - // Search message content for the agentId (appears in Agent tool results) - const extractedId = extractAgentId(message); - if (extractedId) agentId = extractedId; - // Print the final result - if ("result" in message) console.log(message.result); - } - - // Second invocation - resume and ask follow-up - if (agentId && sessionId) { - for await (const message of query({ - prompt: `Resume agent ${agentId} and list the top 3 most complex endpoints`, - options: { allowedTools: ["Read", "Grep", "Glob", "Agent"], resume: sessionId } - })) { - if ("result" in message) console.log(message.result); - } - } + def extract_agent_id(block: ToolResultBlock) -> str | None: + """Extract agentId from an Agent tool result's text content.""" + parts = block.content if isinstance(block.content, list) else [{"text": block.content}] + for part in parts: + if match := re.search(r"agentId:\s*([\w-]+)", part.get("text") or ""): + return match.group(1) + return None + + async def main(): + agent_id = None + session_id = None + + # First invocation - run the endpoint-finder subagent + async for message in query( + prompt="Use the endpoint-finder agent to find all API endpoints in this codebase", + options=ClaudeAgentOptions(allowed_tools=["Read", "Grep", "Glob", "Agent"], agents=AGENTS), + ): + # Capture session_id from ResultMessage (needed to resume this session) + if hasattr(message, "session_id"): + session_id = message.session_id + # Search tool results for the agentId trailer + for block in getattr(message, "content", None) or []: + if isinstance(block, ToolResultBlock): + agent_id = extract_agent_id(block) or agent_id + # Print the final result + if hasattr(message, "result"): + print(message.result) + + # Second invocation - resume and ask follow-up + if agent_id and session_id: + async for message in query( + prompt=f"Resume agent {agent_id} and list the top 3 most complex endpoints", + options=ClaudeAgentOptions( + allowed_tools=["Read", "Grep", "Glob", "Agent"], agents=AGENTS, resume=session_id + ), + ): + if hasattr(message, "result"): + print(message.result) + + asyncio.run(main()) Subagent transcripts persist independently of the main conversation:
modified
--- a/subagents.md +++ b/subagents.md @@ -1,9 +1,3 @@ -> ## Documentation Index -> -> Fetch the complete documentation index at: <https://code.claude.com/docs/llms.txt> -> -> Use this file to discover all available pages before exploring further. - Subagents are separate agent instances that your main agent can spawn to handle focused subtasks. Use subagents to isolate context for focused subtasks, run multiple analyses in parallel, and apply specialized instructions without bloating the main agent’s prompt. This guide explains how to define and use subagents in the SDK using the `agents` parameter. ##
modified
--- a/subagents.md +++ b/subagents.md @@ -423,6 +423,14 @@ ​ +Scale up with dynamic workflows + +Subagents work well for a few delegated tasks per turn. For runs that coordinate dozens to hundreds of agents, use the `Workflow` tool, which moves the orchestration into a script the runtime executes outside the conversation context. See [dynamic workflows](</docs/en/workflows>) for how workflows differ from turn-by-turn subagent delegation. The `Workflow` tool is available in the TypeScript Agent SDK v0.3.149 and later. Include `Workflow` in `allowedTools` to auto-approve workflow runs. The tool input and output schemas are listed in the [TypeScript reference](</docs/en/agent-sdk/typescript#workflow>). + +## + +​ + Troubleshooting ### @@ -460,4 +468,5 @@ Related documentation * [Claude Code subagents](</docs/en/sub-agents>): comprehensive subagent documentation including filesystem-based definitions + * [Dynamic workflows](</docs/en/workflows>): orchestrate many subagents from a script for jobs too large for one conversation * [SDK overview](</docs/en/agent-sdk/overview>): getting started with the Claude Agent SDK
modified
--- a/subagents.md +++ b/subagents.md @@ -70,7 +70,7 @@ Programmatic definition (recommended) -Define subagents directly in your code using the `agents` parameter. This example creates two subagents: a code reviewer with read-only access and a test runner that can execute commands. The `Agent` tool must be included in `allowedTools` since Claude invokes subagents through the Agent tool. +Define subagents directly in your code using the `agents` parameter. This example creates two subagents: a code reviewer with read-only access and a test runner that can execute commands. Claude invokes subagents through the `Agent` tool, so include `Agent` in `allowedTools` to auto-approve subagent invocations without a permission prompt. Python @@ -83,7 +83,7 @@ async for message in query( prompt="Review the authentication module for security issues", options=ClaudeAgentOptions( - # Agent tool is required for subagent invocation + # Auto-approve these tools, including Agent for subagent invocation allowed_tools=["Read", "Grep", "Glob", "Agent"], agents={ "code-reviewer": AgentDefinition( @@ -157,7 +157,7 @@ You can also define subagents as markdown files in `.claude/agents/` directories. See the [Claude Code subagents documentation](</docs/en/sub-agents>) for details on this approach. Programmatically defined agents take precedence over filesystem-based agents with the same name. -Even without defining custom subagents, Claude can spawn the built-in `general-purpose` subagent when `Agent` is in your `allowedTools`. This is useful for delegating research or exploration tasks without creating specialized agents. +Even without defining custom subagents, Claude can spawn the built-in `general-purpose` subagent. This is useful for delegating research or exploration tasks without creating specialized agents. Include `Agent` in `allowedTools` so these invocations auto-approve without a permission prompt. ## @@ -433,7 +433,7 @@ If Claude completes tasks directly instead of delegating to your subagent: - 1. **Include the Agent tool** : subagents are invoked via the Agent tool, so it must be in `allowedTools` + 1. **Check Agent invocations are approved** : include `Agent` in `allowedTools` to auto-approve subagent calls. Without it, Agent invocations fall through to your `canUseTool` callback or, in `dontAsk` mode, are denied 2. **Use explicit prompting** : mention the subagent by name in your prompt (for example, “Use the code-reviewer agent to…”) 3. **Write a clear description** : explain exactly when the subagent should be used so Claude can match tasks appropriately
modified
--- a/subagents.md +++ b/subagents.md @@ -265,7 +265,7 @@ TypeScript import asyncio - from claude_agent_sdk import query, ClaudeAgentOptions, AgentDefinition + from claude_agent_sdk import query, ClaudeAgentOptions, AgentDefinition, ToolUseBlock async def main(): async for message in query( @@ -285,7 +285,7 @@ # versions emitted "Task", current versions emit "Agent". if hasattr(message, "content") and message.content: for block in message.content: - if getattr(block, "type", None) == "tool_use" and block.name in ( + if isinstance(block, ToolUseBlock) and block.name in ( "Task", "Agent", ): @@ -325,7 +325,7 @@ // Helper to extract agentId from message content // Stringify to avoid traversing different block types (TextBlock, ToolResultBlock, etc.) function extractAgentId(message: SDKMessage): string | undefined { - if (!("message" in message)) return undefined; + if (message.type !== "assistant" && message.type !== "user") return undefined; // Stringify the content so we can search it without traversing nested blocks const content = JSON.stringify(message.message.content); const match = content.match(/agentId:\s*([a-f0-9-]+)/);
modified
--- a/subagents.md +++ b/subagents.md @@ -137,7 +137,7 @@ `tools`| `string[]`| No| Array of allowed tool names. If omitted, inherits all tools `disallowedTools`| `string[]`| No| Array of tool names to remove from the agent’s tool set `model`| `string`| No| Model override for this agent. Accepts an alias such as `'sonnet'`, `'opus'`, `'haiku'`, `'inherit'`, or a full model ID. Defaults to main model if omitted -`skills`| `string[]`| No| List of skill names available to this agent +`skills`| `string[]`| No| List of skill names to preload into the agent’s context at startup. Unlisted skills remain invocable through the Skill tool `memory`| `'user' | 'project' | 'local'`| No| Memory source for this agent `mcpServers`| `(string | object)[]`| No| MCP servers available to this agent, by name or inline config `maxTurns`| `number`| No| Maximum number of agentic turns before the agent stops @@ -170,7 +170,7 @@ The subagent receives| The subagent does not receive ---|--- Its own system prompt (`AgentDefinition.prompt`) and the Agent tool’s prompt| The parent’s conversation history or tool results -Project CLAUDE.md (loaded via `settingSources`)| Skills (unless listed in `AgentDefinition.skills`) +Project CLAUDE.md (loaded via `settingSources`)| Preloaded skill content, unless listed in `AgentDefinition.skills` Tool definitions (inherited from parent, or the subset in `tools`)| The parent’s system prompt The parent receives the subagent’s final message verbatim as the Agent tool result, but may summarize it in its own response. To preserve subagent output verbatim in the user-facing response, include an instruction to do so in the prompt or `systemPrompt` option you pass to the **main** `query()` call.
modified
--- a/subagents.md +++ b/subagents.md @@ -14,7 +14,7 @@ You can create subagents in three ways: - * **Programmatically** : use the `agents` parameter in your `query()` options ([TypeScript](</docs/en/agent-sdk/typescript#agent-definition>), [Python](</docs/en/agent-sdk/python#agent-definition>)) + * **Programmatically** : use the `agents` parameter in your `query()` options ([TypeScript](</docs/en/agent-sdk/typescript#agentdefinition>), [Python](</docs/en/agent-sdk/python#agentdefinition>)) * **Filesystem-based** : define agents as markdown files in `.claude/agents/` directories (see [defining subagents as files](</docs/en/sub-agents>)) * **Built-in general-purpose** : Claude can invoke the built-in `general-purpose` subagent at any time via the Agent tool without you defining anything @@ -145,7 +145,7 @@ `effort`| `'low' | 'medium' | 'high' | 'xhigh' | 'max' | number`| No| Reasoning effort level for this agent `permissionMode`| `PermissionMode`| No| Permission mode for tool execution within this agent -In the Python SDK, these field names use camelCase to match the wire format. See the [`AgentDefinition` reference](</docs/en/agent-sdk/python#agent-definition>) for details. +In the Python SDK, these field names use camelCase to match the wire format. See the [`AgentDefinition` reference](</docs/en/agent-sdk/python#agentdefinition>) for details. Subagents cannot spawn their own subagents. Don’t include `Agent` in a subagent’s `tools` array.
modified
--- a/subagents.md +++ b/subagents.md @@ -1,3 +1,9 @@ +> ## Documentation Index +> +> Fetch the complete documentation index at: <https://code.claude.com/docs/llms.txt> +> +> Use this file to discover all available pages before exploring further. + Subagents are separate agent instances that your main agent can spawn to handle focused subtasks. Use subagents to isolate context for focused subtasks, run multiple analyses in parallel, and apply specialized instructions without bloating the main agent’s prompt. This guide explains how to define and use subagents in the SDK using the `agents` parameter. ##
modified
--- a/subagents.md +++ b/subagents.md @@ -129,10 +129,17 @@ `description`| `string`| Yes| Natural language description of when to use this agent `prompt`| `string`| Yes| The agent’s system prompt defining its role and behavior `tools`| `string[]`| No| Array of allowed tool names. If omitted, inherits all tools -`model`| `'sonnet' | 'opus' | 'haiku' | 'inherit'`| No| Model override for this agent. Defaults to main model if omitted +`disallowedTools`| `string[]`| No| Array of tool names to remove from the agent’s tool set +`model`| `string`| No| Model override for this agent. Accepts an alias such as `'sonnet'`, `'opus'`, `'haiku'`, `'inherit'`, or a full model ID. Defaults to main model if omitted `skills`| `string[]`| No| List of skill names available to this agent -`memory`| `'user' | 'project' | 'local'`| No| Memory source for this agent (Python only) +`memory`| `'user' | 'project' | 'local'`| No| Memory source for this agent `mcpServers`| `(string | object)[]`| No| MCP servers available to this agent, by name or inline config +`maxTurns`| `number`| No| Maximum number of agentic turns before the agent stops +`background`| `boolean`| No| Run this agent as a non-blocking background task when invoked +`effort`| `'low' | 'medium' | 'high' | 'xhigh' | 'max' | number`| No| Reasoning effort level for this agent +`permissionMode`| `PermissionMode`| No| Permission mode for tool execution within this agent + +In the Python SDK, these field names use camelCase to match the wire format. See the [`AgentDefinition` reference](</docs/en/agent-sdk/python#agent-definition>) for details. Subagents cannot spawn their own subagents. Don’t include `Agent` in a subagent’s `tools` array.
added
Show diff (453 lines)
--- a/subagents.md +++ b/subagents.md @@ -0,0 +1,450 @@ +Subagents are separate agent instances that your main agent can spawn to handle focused subtasks. Use subagents to isolate context for focused subtasks, run multiple analyses in parallel, and apply specialized instructions without bloating the main agent’s prompt. This guide explains how to define and use subagents in the SDK using the `agents` parameter. + +## + +​ + +Overview + +You can create subagents in three ways: + + * **Programmatically** : use the `agents` parameter in your `query()` options ([TypeScript](</docs/en/agent-sdk/typescript#agent-definition>), [Python](</docs/en/agent-sdk/python#agent-definition>)) + * **Filesystem-based** : define agents as markdown files in `.claude/agents/` directories (see [defining subagents as files](</docs/en/sub-agents>)) + * **Built-in general-purpose** : Claude can invoke the built-in `general-purpose` subagent at any time via the Agent tool without you defining anything + +This guide focuses on the programmatic approach, which is recommended for SDK applications. When you define subagents, Claude determines whether to invoke them based on each subagent’s `description` field. Write clear descriptions that explain when the subagent should be used, and Claude will automatically delegate appropriate tasks. You can also explicitly request a subagent by name in your prompt (for example, “Use the code-reviewer agent to…”). + +## + +​ + +Benefits of using subagents + +### + +​ + +Context isolation + +Each subagent runs in its own fresh conversation. Intermediate tool calls and results stay inside the subagent; only its final message returns to the parent. See What subagents inherit for exactly what’s in the subagent’s context. **Example:** a `research-assistant` subagent can explore dozens of files without any of that content accumulating in the main conversation. The parent receives a concise summary, not every file the subagent read. + +### + +​ + +Parallelization + +Multiple subagents can run concurrently, dramatically speeding up complex workflows. **Example:** during a code review, you can run `style-checker`, `security-scanner`, and `test-coverage` subagents simultaneously, reducing review time from minutes to seconds. + +### + +​ + +Specialized instructions and knowledge + +Each subagent can have tailored system prompts with specific expertise, best practices, and constraints. **Example:** a `database-migration` subagent can have detailed knowledge about SQL best practices, rollback strategies, and data integrity checks that would be unnecessary noise in the main agent’s instructions. + +### + +​ + +Tool restrictions + +Subagents can be limited to specific tools, reducing the risk of unintended actions. **Example:** a `doc-reviewer` subagent might only have access to Read and Grep tools, ensuring it can analyze but never accidentally modify your documentation files. + +## + +​ + +Creating subagents + +### + +​ + +Programmatic definition (recommended) + +Define subagents directly in your code using the `agents` parameter. This example creates two subagents: a code reviewer with read-only access and a test runner that can execute commands. The `Agent` tool must be included in `allowedTools` since Claude invokes subagents through the Agent tool. + +Python + +TypeScript + + import asyncio + from claude_agent_sdk import query, ClaudeAgentOptions, AgentDefinition + + async def main(): + async for message in query( + prompt="Review the authentication module for security issues", + options=ClaudeAgentOptions( + # Agent tool is required for subagent invocation + allowed_tools=["Read", "Grep", "Glob", "Agent"], + agents={ + "code-reviewer": AgentDefinition( + # description tells Claude when to use this subagent + description="Expert code review specialist. Use for quality, security, and maintainability reviews.", + # prompt defines the subagent's behavior and expertise + prompt="""You are a code review specialist with expertise in security, performance, and best practices. + + When reviewing code: + - Identify security vulnerabilities + - Check for performance issues + - Verify adherence to coding standards + - Suggest specific improvements + + Be thorough but concise in your feedback.""", + # tools restricts what the subagent can do (read-only here) + tools=["Read", "Grep", "Glob"], + # model overrides the default model for this subagent + model="sonnet", + ), + "test-runner": AgentDefinition( + description="Runs and analyzes test suites. Use for test execution and coverage analysis.", + prompt="""You are a test execution specialist. Run tests and provide clear analysis of results. + + Focus on: + - Running test commands + - Analyzing test output + - Identifying failing tests + - Suggesting fixes for failures""", + # Bash access lets this subagent run test commands + tools=["Bash", "Read", "Grep"], + ), + }, + ), + ): + if hasattr(message, "result"): + print(message.result) + + asyncio.run(main()) + +### + +​ + +AgentDefinition configuration + +Field| Type| Required| Description +---|---|---|--- +`description`| `string`| Yes| Natural language description of when to use this agent +`prompt`| `string`| Yes| The agent’s system prompt defining its role and behavior +`tools`| `string[]`| No| Array of allowed tool names. If omitted, inherits all tools +`model`| `'sonnet' | 'opus' | 'haiku' | 'inherit'`| No| Model override for this agent. Defaults to main model if omitted +`skills`| `string[]`| No| List of skill names available to this agent +`memory`| `'user' | 'project' | 'local'`| No| Memory source for this agent (Python only) +`mcpServers`| `(string | object)[]`| No| MCP servers available to this agent, by name or inline config + +Subagents cannot spawn their own subagents. Don’t include `Agent` in a subagent’s `tools` array. + +### + +​ + +Filesystem-based definition (alternative) + +You can also define subagents as markdown files in `.claude/agents/` directories. See the [Claude Code subagents documentation](</docs/en/sub-agents>) for details on this approach. Programmatically defined agents take precedence over filesystem-based agents with the same name. + +Even without defining custom subagents, Claude can spawn the built-in `general-purpose` subagent when `Agent` is in your `allowedTools`. This is useful for delegating research or exploration tasks without creating specialized agents. + +## + +​ + +What subagents inherit + +A subagent’s context window starts fresh (no parent conversation) but isn’t empty. The only channel from parent to subagent is the Agent tool’s prompt string, so include any file paths, error messages, or decisions the subagent needs directly in that prompt. + +The subagent receives| The subagent does not receive +---|--- +Its own system prompt (`AgentDefinition.prompt`) and the Agent tool’s prompt| The parent’s conversation history or tool results +Project CLAUDE.md (loaded via `settingSources`)| Skills (unless listed in `AgentDefinition.skills`) +Tool definitions (inherited from parent, or the subset in `tools`)| The parent’s system prompt + +The parent receives the subagent’s final message verbatim as the Agent tool result, but may summarize it in its own response. To preserve subagent output verbatim in the user-facing response, include an instruction to do so in the prompt or `systemPrompt` option you pass to the **main** `query()` call. + +## + +​ + +Invoking subagents + +### + +​ + +Automatic invocation + +Claude automatically decides when to invoke subagents based on the task and each subagent’s `description`. For example, if you define a `performance-optimizer` subagent with the description “Performance optimization specialist for query tuning”, Claude will invoke it when your prompt mentions optimizing queries. Write clear, specific descriptions so Claude can match tasks to the right subagent. + +### + +​ + +Explicit invocation + +To guarantee Claude uses a specific subagent, mention it by name in your prompt: + + "Use the code-reviewer agent to check the authentication module" + +This bypasses automatic matching and directly invokes the named subagent. + +### + +​ + +Dynamic agent configuration + +You can create agent definitions dynamically based on runtime conditions. This example creates a security reviewer with different strictness levels, using a more powerful model for strict reviews. + +Python + +TypeScript + + import asyncio + from claude_agent_sdk import query, ClaudeAgentOptions, AgentDefinition + + # Factory function that returns an AgentDefinition + # This pattern lets you customize agents based on runtime conditions + def create_security_agent(security_level: str) -> AgentDefinition: + is_strict = security_level == "strict" + return AgentDefinition( + description="Security code reviewer", + # Customize the prompt based on strictness level + prompt=f"You are a {'strict' if is_strict else 'balanced'} security reviewer...", + tools=["Read", "Grep", "Glob"], + # Key insight: use a more capable model for high-stakes reviews + model="opus" if is_strict else "sonnet", + ) + + async def main(): + # The agent is created at query time, so each request can use different settings + async for message in query( + prompt="Review this PR for security issues", + options=ClaudeAgentOptions( + allowed_tools=["Read", "Grep", "Glob", "Agent"], + agents={ + # Call the factory with your desired configuration + "security-reviewer": create_security_agent("strict") + }, + ), + ): + if hasattr(message, "result"): + print(message.result) + + asyncio.run(main()) + +## + +​ + +Detecting subagent invocation + +Subagents are invoked via 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. + +The tool name was renamed from `"Task"` to `"Agent"` in Claude Code v2.1.63. Current SDK releases emit `"Agent"` in `tool_use` blocks but still use `"Task"` in the `system:init` tools list and in `result.permission_denials[].tool_name`. Checking both values in `block.name` ensures compatibility across SDK versions. + +This example iterates through streamed messages, logging when a subagent is invoked and when subsequent messages originate from within that subagent’s execution context. + +The message structure differs between SDKs. In Python, content blocks are accessed directly via `message.content`. In TypeScript, `SDKAssistantMessage` wraps the Claude API message, so content is accessed via `message.message.content`. + +Python + +TypeScript + + import asyncio + from claude_agent_sdk import query, ClaudeAgentOptions, AgentDefinition + + async def main(): + async for message in query( + prompt="Use the code-reviewer agent to review this codebase", + options=ClaudeAgentOptions( + allowed_tools=["Read", "Glob", "Grep", "Agent"], + agents={ + "code-reviewer": AgentDefinition( + description="Expert code reviewer.", + prompt="Analyze code quality and suggest improvements.", + tools=["Read", "Glob", "Grep"], + ) + }, + ), + ): + # Check for subagent invocation. Match both names: older SDK + # versions emitted "Task", current versions emit "Agent". + if hasattr(message, "content") and message.content: + for block in message.content: + if getattr(block, "type", None) == "tool_use" and block.name in ( + "Task", + "Agent", + ): + print(f"Subagent invoked: {block.input.get('subagent_type')}") + + # Check if this message is from within a subagent's context + if hasattr(message, "parent_tool_use_id") and message.parent_tool_use_id: + print(" (running inside subagent)") + + if hasattr(message, "result"): + print(message.result) + + asyncio.run(main()) + +## + +​ + +Resuming subagents + +Subagents can be resumed to continue where they left off. Resumed subagents retain their full conversation history, including all previous tool calls, results, and reasoning. The subagent picks up exactly where it stopped rather than starting fresh. When a subagent completes, Claude receives its agent ID in the Agent tool result. To resume a subagent programmatically: + + 1. **Capture the session ID** : Extract `session_id` from messages during the first query + 2. **Extract the agent ID** : Parse `agentId` from the message content + 3. **Resume the session** : Pass `resume: sessionId` in the second query’s options, and include the agent ID in your prompt + +You must resume the same session to access the subagent’s transcript. Each `query()` call starts a new session by default, so pass `resume: sessionId` to continue in the same session.If you’re using a custom agent (not a built-in one), you also need to pass the same agent definition in the `agents` parameter for both queries. + +The example below demonstrates this flow: the first query runs a subagent and captures the session ID and agent ID, then the second query resumes the session to ask a follow-up question that requires context from the first analysis. + +TypeScript + +Python + + import { query, type SDKMessage } from "@anthropic-ai/claude-agent-sdk"; + + // Helper to extract agentId from message content + // Stringify to avoid traversing different block types (TextBlock, ToolResultBlock, etc.) + function extractAgentId(message: SDKMessage): string | undefined { + if (!("message" in message)) return undefined; + // Stringify the content so we can search it without traversing nested blocks + const content = JSON.stringify(message.message.content); + const match = content.match(/agentId:\s*([a-f0-9-]+)/); + return match?.[1]; + } + + let agentId: string | undefined; + let sessionId: string | undefined; + + // First invocation - use the Explore agent to find API endpoints + for await (const message of query({ + prompt: "Use the Explore agent to find all API endpoints in this codebase", + options: { allowedTools: ["Read", "Grep", "Glob", "Agent"] } + })) { + // Capture session_id from ResultMessage (needed to resume this session) + if ("session_id" in message) sessionId = message.session_id; + // Search message content for the agentId (appears in Agent tool results) + const extractedId = extractAgentId(message); + if (extractedId) agentId = extractedId; + // Print the final result + if ("result" in message) console.log(message.result); + } + + // Second invocation - resume and ask follow-up + if (agentId && sessionId) { + for await (const message of query({ + prompt: `Resume agent ${agentId} and list the top 3 most complex endpoints`, + options: { allowedTools: ["Read", "Grep", "Glob", "Agent"], resume: sessionId } + })) { + if ("result" in message) console.log(message.result); + } + } + +Subagent transcripts persist independently of the main conversation: + + * **Main conversation compaction** : When the main conversation compacts, subagent transcripts are unaffected. They’re stored in separate files. + * **Session persistence** : Subagent transcripts persist within their session. You can resume a subagent after restarting Claude Code by resuming the same session. + * **Automatic cleanup** : Transcripts are cleaned up based on the `cleanupPeriodDays` setting (default: 30 days). + +## + +​ + +Tool restrictions + +Subagents can have restricted tool access via the `tools` field: + + * **Omit the field** : agent inherits all available tools (default) + * **Specify tools** : agent can only use listed tools + +This example creates a read-only analysis agent that can examine code but cannot modify files or run commands. + +Python + +TypeScript + + import asyncio + from claude_agent_sdk import query, ClaudeAgentOptions, AgentDefinition + + async def main(): + async for message in query( + prompt="Analyze the architecture of this codebase", + options=ClaudeAgentOptions( + allowed_tools=["Read", "Grep", "Glob", "Agent"], + agents={ + "code-analyzer": AgentDefinition( + description="Static code analysis and architecture review", + prompt="""You are a code architecture analyst. Analyze code structure, + identify patterns, and suggest improvements without making changes.""", + # Read-only tools: no Edit, Write, or Bash access + tools=["Read", "Grep", "Glob"], + ) + }, + ), + ): + if hasattr(message, "result"): + print(message.result) + + asyncio.run(main()) + +### + +​ + +Common tool combinations + +Use case| Tools| Description +---|---|--- +Read-only analysis| `Read`, `Grep`, `Glob`| Can examine code but not modify or execute +Test execution| `Bash`, `Read`, `Grep`| Can run commands and analyze output +Code modification| `Read`, `Edit`, `Write`, `Grep`, `Glob`| Full read/write access without command execution +Full access| All tools| Inherits all tools from parent (omit `tools` field) + +## + +​ + +Troubleshooting + +### + +​ + +Claude not delegating to subagents + +If Claude completes tasks directly instead of delegating to your subagent: + + 1. **Include the Agent tool** : subagents are invoked via the Agent tool, so it must be in `allowedTools` + 2. **Use explicit prompting** : mention the subagent by name in your prompt (for example, “Use the code-reviewer agent to…”) + 3. **Write a clear description** : explain exactly when the subagent should be used so Claude can match tasks appropriately + +### + +​ + +Filesystem-based agents not loading + +Agents defined in `.claude/agents/` are loaded at startup only. If you create a new agent file while Claude Code is running, restart the session to load it. + +### + +​ + +Windows: long prompt failures + +On Windows, subagents with very long prompts may fail due to command line length limits (8191 chars). Keep prompts concise or use filesystem-based agents for complex instructions. + +## + +​ + +Related documentation + + * [Claude Code subagents](</docs/en/sub-agents>): comprehensive subagent documentation including filesystem-based definitions + * [SDK overview](</docs/en/agent-sdk/overview>): getting started with the Claude Agent SDK