How sub-agents work
When Claude decides to delegate work, it invokes the Agent tool with a prompt and a short description. Claude Code spins up a new agent process, runs it to completion (or in the background), and returns the result.- Has its own conversation context and system prompt
- Assembles its own tool pool based on its permission mode
- Can itself spawn further sub-agents (up to the nesting limit)
Agent tool input schema
The Agent tool accepts the following parameters:| Parameter | Type | Description |
|---|---|---|
description | string | Short (3–5 word) task description shown in the UI |
prompt | string | Full task instructions for the agent |
subagent_type | string? | Specialized agent type (e.g. a custom agent from your .claude/agents/ directory) |
model | "sonnet" | "opus" | "haiku"? | Model override; inherits from parent if omitted |
run_in_background | boolean? | Run asynchronously; returns immediately with an agent ID |
isolation | "worktree"? | Create an isolated git worktree for this agent |
cwd | string? | Absolute working directory for the agent |
name | string? | Addressable name for multi-agent team routing |
Parallel task execution
Claude automatically runs independent agents in parallel. For tasks with no data dependencies, multiple agents are launched simultaneously and their results are aggregated. Example use cases:- Running unit tests for multiple packages at once
- Refactoring several modules in a large codebase independently
- Generating documentation for different subsystems in parallel
- Investigating multiple bug reports simultaneously
Background agents
Setrun_in_background: true to launch an agent without blocking the parent turn. The Agent tool returns immediately with an async_launched result:
TaskOutput tool to block until completion.
Background agents survive the parent pressing
Esc. They continue running and surface results via a task notification when complete.Managing running agents
Use the/tasks command to see all running and completed background agents. From the tasks dialog you can:
- View progress and current activity
- Kill a running agent
- Promote a background agent to the foreground
CLAUDE_CODE_DISABLE_BACKGROUND_TASKS=1 to prevent agents from being run in the background entirely.
Agent isolation with worktrees
Passisolation: "worktree" to give an agent its own isolated copy of the repository:
Create a git worktree
A temporary branch is created at a slug like
agent-abc12345, and the worktree is checked out alongside the main working tree.Run the agent in the worktree
All filesystem and shell operations within the agent are scoped to the worktree path.
Specialized agent types
You can define custom agent types in.claude/agents/ as Markdown files with YAML frontmatter. Claude will use the appropriate agent based on the task.
Agent output schema
When an agent completes synchronously, the Agent tool returns acompleted result:
Multi-agent teams
Agent teams (swarm mode) require a plan that includes multi-agent access.
SendMessage. Spawn a teammate with a name and team_name:
"frontend" using SendMessage({ to: "frontend" }).
Use cases
Large codebase refactoring
Decompose a large refactor into per-module sub-tasks. Each agent handles one module in its own worktree, and the parent merges the results.
Parallel test runs
Spawn one agent per test suite. All suites run simultaneously and report back — dramatically faster than sequential execution.
Research and implementation
One agent researches the codebase and produces a plan; a second agent implements while a third writes tests — all in parallel.
CI triage
Launch one agent per failing test or lint error. Each agent fixes its assigned issue in an isolated worktree and opens a diff.