Overview
Thetask() tool allows you to delegate work to specialized sub-agents that run in their own isolated context. This helps preserve the main conversation context and enables autonomous execution of complex, multi-step tasks.
task
Delegate a task to a specialized sub-agent that runs in its own context.Parameters
A short (3-5 word) description of the task for logging and display. ALWAYS PROVIDE THIS PARAMETER FIRST.
The task description for the sub-agent. Be specific and clear about what needs to be done. ALWAYS PROVIDE THIS PARAMETER SECOND.
The type of sub-agent to use. ALWAYS PROVIDE THIS PARAMETER THIRD.
Optional maximum number of agent turns. Defaults to the sub-agent’s configured maximum.
Sub-Agent Types
general-purpose
A capable agent for complex, multi-step tasks that require both exploration and action. Configuration:- Max turns: 50 (default)
- Timeout: 900 seconds (15 minutes)
- Available tools: All parent tools (except
task,ask_clarification,present_files)
- The task requires both exploration and modification
- Complex reasoning is needed to interpret results
- Multiple dependent steps must be executed
- The task would benefit from isolated context management
- Simple, single-step operations (use tools directly)
- Tasks requiring user interaction or clarification
bash
Command execution specialist for running bash commands in a separate context. Configuration:- Max turns: 30 (default)
- Timeout: 900 seconds (15 minutes)
- Available tools:
bash,ls,read_file,write_file,str_replaceonly
- You need to run a series of related bash commands
- Terminal operations like git, npm, docker, etc.
- Command output is verbose and would clutter main context
- Build, test, or deployment operations
- Simple single commands (use
bashtool directly instead)
When to Use Sub-Agents
Sub-agents help you:- Preserve context: Keep exploration and implementation separate from the main conversation
- Handle complexity: Execute complex multi-step tasks autonomously
- Isolate execution: Run commands or operations without cluttering the main context
- Parallelize work: Delegate independent tasks to run concurrently
Good Use Cases
When NOT to Use
Delegation Flow
-
Task Initialization
- Sub-agent executor is created with specified configuration
- Parent context (sandbox, thread data, model) is inherited
- Task starts in background thread
-
Execution
- Sub-agent runs autonomously in isolated context
- Progress updates stream to parent via task events
- Timeout and max turns enforced automatically
-
Completion
- Results returned to parent agent
- Success/failure/timeout status reported
- Execution logs available for debugging
Task Events
The parent agent receives real-time events:task_started: Task begins executiontask_running: Sub-agent sends progress messagestask_completed: Task finishes successfullytask_failed: Task encounters an errortask_timed_out: Task exceeds timeout limit
Return Values
Thetask() tool returns a string with the execution result:
Success
Failure
Timeout
Advanced Usage
Custom Max Turns
Limit the number of agent iterations:Parallel Delegation
Delegate multiple independent tasks:Best Practices
Clear Instructions
Provide specific, actionable prompts:Choose the Right Sub-Agent
- Use
general-purposefor tasks requiring reasoning and exploration - Use
bashfor pure command execution sequences - Use tools directly for simple operations
Handle Results
Sub-agents return structured results that should be parsed:Technical Details
Context Inheritance
Sub-agents inherit from parent:- Sandbox state and environment
- Thread data (workspace, uploads, outputs paths)
- Model configuration
- Available tools (with restrictions)
Disallowed Tools
Sub-agents cannot use:task: Prevents recursive nestingask_clarification: Sub-agents must work autonomouslypresent_files: Only parent can present files to user
Timeout Handling
- Default timeout: 900 seconds (15 minutes)
- Enforced at thread pool level
- Polling timeout includes 60-second buffer
- Status checked every 5 seconds
Tracing
All sub-agent executions are traced:- Unique trace ID propagated to sub-agent
- Logs include trace ID for correlation
- Task ID matches tool call ID for traceability