This document describes Claude Code's Task tool and its specialized sub-agent system for delegating complex, multi-step operations to autonomous agents. The Task tool enables the main agent to spawn specialized worker agents that execute tasks independently and return final results.
For information about Claude Code's other tools, see Tool Architecture and Categories. For file editing operations, see File Editing Operations. For command execution capabilities, see Shell Command Execution.
The Task tool provides a delegation mechanism where Claude Code's main agent can launch specialized sub-agents to handle specific categories of complex work autonomously. Each sub-agent has access to a curated subset of tools appropriate for its domain, and operates independently until completion.
Key Characteristics:
| Property | Value |
|---|---|
| Tool Name | Task |
| Delegation Model | Fire-and-forget with single result message |
| Execution Mode | Stateless, autonomous |
| Communication | One-way: prompt in, final report out |
| Concurrency | Multiple agents can run in parallel |
| Agent Types | 3 specialized types |
Sources: Anthropic/Claude Code/Tools.json1-30
Sources: Anthropic/Claude Code/Tools.json4-5
Description: General-purpose agent for researching complex questions, searching for code, and executing multi-step tasks. When searching for keywords or files and not confident about finding the right match in the first few tries, delegate to this agent.
Tool Access: All tools (*)
Primary Use Cases:
Grep and GlobDescription: Specialized agent for configuring the user's Claude Code status line setting.
Tool Access: Read, Edit
Primary Use Cases:
Description: Specialized agent for creating Claude Code output styles.
Tool Access: Read, Write, Edit, Glob, LS, Grep
Primary Use Cases:
Sources: Anthropic/Claude Code/Tools.json4-5
| Parameter | Type | Required | Description |
|---|---|---|---|
description | string | Yes | Short 3-5 word description of the task |
prompt | string | Yes | Detailed, autonomous task description for the agent |
subagent_type | string | Yes | Type of specialized agent: general-purpose, statusline-setup, or output-style-setup |
Sources: Anthropic/Claude Code/Tools.json6-29
Stateless Operation:
Communication Constraints:
Prompt Requirements: The prompt parameter must contain a highly detailed task description that enables autonomous execution, including:
Sources: Anthropic/Claude Code/Tools.json13-15
Appropriate Scenarios:
Grep, Glob, and Read operationsExample Use Case:
User request: "Find all implementations of error handling across the codebase"
Decision: Use Task tool with general-purpose sub-agent
Reason: Requires multiple Grep rounds with different patterns,
reading multiple files, and synthesizing findings
Sources: Anthropic/Claude Code/Tools.json7-12
The tool explicitly lists scenarios where direct tool usage is faster and more appropriate:
| Scenario | Use Instead | Reason |
|---|---|---|
| Reading specific file path | Read or Glob | Faster direct access |
Searching for specific class definition (e.g., "class Foo") | Glob | Faster pattern matching |
| Searching within specific 2-3 files | Read | Direct file access more efficient |
| Tasks unrelated to agent descriptions | Other tools | No delegation benefit |
Example Use Case:
User request: "Read the file at src/utils/helpers.js"
Decision: Use Read tool directly
Reason: Specific path known, no search required, faster direct access
Sources: Anthropic/Claude Code/Tools.json7-12
Performance Optimization:
Example Pattern:
User: "Analyze authentication, error handling, and database patterns"
Main Agent Response:
- Single message invoking 3 Task tools simultaneously
- Each with different prompt and same subagent_type (general-purpose)
- All execute in parallel
- Results aggregated when all complete
Performance Impact:
Sources: Anthropic/Claude Code/Tools.json13
Trust Model:
Communication Requirement: The sub-agent result is not visible to the user. The main agent must:
Sources: Anthropic/Claude Code/Tools.json14-15
The documentation includes example agent descriptions to illustrate when agents should be used proactively:
Example from Documentation:
"code-reviewer": use this agent after you are done writing a significant piece of code
"greeting-responder": use this agent to respond to user greetings with a friendly joke
If agent description mentions proactive usage:
Example Workflow:
User: "Please write a function that checks if a number is prime"
Main Agent Actions:
1. Write the isPrime function using Write tool
2. Proactively invoke Task tool with code-reviewer sub-agent
(because significant code was written)
3. Report both implementation and review findings to user
Sources: Anthropic/Claude Code/Tools.json17-18
Since sub-agents are stateless and cannot communicate back, the prompt parameter must be comprehensive:
Required Elements:
Good Prompt Example:
{
"description": "Search authentication flows",
"prompt": "Search the codebase for all authentication implementations.
This is a RESEARCH task - do not write any code.
Find:
1. All files containing authentication logic
2. The authentication methods used (JWT, OAuth, session, etc.)
3. Where credentials are validated
4. Any security middleware
Return in your final message:
- List of files with authentication code
- Brief description of authentication flow
- Identification of auth method(s) used
Use Grep to search for patterns like 'auth', 'login', 'authenticate'.
Use Read to examine promising files in detail.",
"subagent_type": "general-purpose"
}
Poor Prompt Example:
{
"description": "Find auth stuff",
"prompt": "Look for authentication",
"subagent_type": "general-purpose"
}
Issues: Unclear what to return, no guidance on research vs code, insufficient context.
Sources: Anthropic/Claude Code/Tools.json15-16
| Tool Category | Direct Usage | Task Tool Delegation |
|---|---|---|
| Read | Specific known file paths | Sub-agent uses Read during research |
| Grep | Single targeted search pattern | Sub-agent performs iterative Grep rounds |
| Glob | Known class/function name pattern | Sub-agent uses Glob as part of discovery |
| Edit | Main agent direct edits | statusline-setup/output-style-setup agents use Edit |
| Write | Main agent creates files | output-style-setup agent uses Write |
| Bash | Main agent command execution | general-purpose agent can use Bash |
Sources: Anthropic/Claude Code/Tools.json1-30
Maximize Concurrency
Appropriate Delegation
Comprehensive Prompts
prompt parameterTrust Agent Results
Proactive Usage
| Anti-Pattern | Why It's Wrong | Correct Approach |
|---|---|---|
| Using Task for known file paths | Slower than direct Read | Use Read tool directly |
| Vague prompts to sub-agents | Agent cannot operate autonomously | Provide detailed, complete instructions |
| Sequential Task invocations | Misses parallelization opportunity | Single message with multiple Task tools |
| Showing raw sub-agent output to user | Output is not user-facing | Summarize in concise text message |
| Delegating trivial operations | Overhead exceeds benefit | Use direct tools for simple tasks |
Sources: Anthropic/Claude Code/Tools.json7-18
Summary: The Task tool provides a sophisticated delegation architecture where Claude Code's main agent can spawn specialized sub-agents with curated tool access to handle complex, multi-step operations autonomously. The system emphasizes parallel execution, stateless operation, and clear separation between agent research and user communication. Proper usage requires understanding when delegation provides value versus when direct tool usage is more efficient.
Refresh this wiki