AgentTool

Description

The sub-agent spawning tool — creates child agent instances that run with their own context, tools, and permission scope. AgentTool is the primary mechanism for parallelism in Claude Code: background research, worktree-isolated PoCs, forked agents sharing the parent's prompt cache, and coordinator-managed workers.

See agent-lifecycle for the complete spawn-to-completion lifecycle, including AsyncLocalStorage isolation, fork path cache sharing, and optimizations (omitClaudeMd, gitStatus drop).

Implementation: src/tools/AgentTool/AgentTool.tsx (765 lines)

Input parameters

Parameter Type Purpose
prompt string Task description for the agent
description string 3-5 word label
subagent_type string? Agent type (Explore, Plan, general-purpose, worker)
model enum? Model override (sonnet/opus/haiku)
run_in_background boolean? Async spawn — returns immediately
name string? Addressable name for SendMessage routing
team_name string? Team context for multi-agent
mode enum? Permission mode override
isolation 'worktree'? Filesystem isolation via git worktree

Two execution paths

Synchronous — agent runs inline within the parent's turn. Blocks parent completion. Can be auto-backgrounded after 120 seconds.

Asynchronous — agent runs in a detached closure. Returns immediately with agentId and outputFile path. Parent continues working. Forced for coordinator mode workers.

Agent resolution

  1. Explicit subagent_type wins over defaults
  2. Fork experiment gate can route untyped spawns to Fork subagent
  3. Recursive fork spawning is guarded against
  4. Agent definition lookup applies permission filtering

Key claims

Relations

Sources

Source code at src/tools/AgentTool/AgentTool.tsx, src/tools/AgentTool/runAgent.ts