subagent tool spawns independent AI agent instances to handle specific tasks with full tool access and autonomous execution.
subagent
Execute a subagent task synchronously and return the result. Use this for delegating specific tasks to an independent agent instance.Input Parameters
The task for the subagent to complete. Be specific and provide clear instructions.
Optional short label for the task (for display and tracking). Defaults to “(unnamed)” if not provided.
Response
Returns execution results with different detail levels for LLM and user.For LLM (detailed):For User (summary):
Truncated to 500 characters maximum for readability.
Usage Examples
Basic task delegation:Execution Flow
-
Task initialization:
- Subagent receives task description
- System prompt configures independent operation
- Full tool registry access granted
-
Tool loop execution:
- Maximum iterations: 10 (configurable)
- LLM model: Same as parent agent
- Tools: Full access to all registered tools
- Options:
max_tokens: 4096,temperature: 0.7
-
Result aggregation:
- Final output captured from last iteration
- Iteration count tracked
- Results formatted for LLM and user
-
Synchronous return:
- Main agent waits for completion
- Full results returned in ToolResult
System Prompt
Subagents are initialized with the following instructions:- Independent operation without parent context pollution
- Focus on task completion
- Clear result communication
Tool Access
Subagents have access to the same tools as the parent agent, including:- Filesystem tools (read_file, write_file, list_dir)
- Shell execution (exec)
- Web tools (web_search, web_fetch)
- Hardware tools (i2c, spi) if available
- Image generation (generate_image) if configured
- Recursive subagent spawning (use with caution)
Error Conditions
Validation errors:task is required- Missing task parameter (with error object)Subagent manager not configured- Manager is nil (with error object)
Subagent execution failed: {error}- Tool loop failed or timed out (with error object)
IsError: trueflagErrfield set with error object- Error message in both
ForLLMandForUser(truncated for user)
Response Format
Successful execution:Performance Characteristics
Execution time:- Depends on task complexity and tool calls
- Maximum iterations: 10 (can complete earlier)
- Each iteration: ~1-10 seconds (model-dependent)
- Typical range: 5-60 seconds
- Creates new conversation context (isolated from parent)
- LLM tokens: ~500-4000 per iteration
- Memory: Minimal overhead per subagent
- Concurrent subagents: Limited by system resources
Comparison: Subagent vs Parent Execution
| Feature | Subagent | Parent Agent |
|---|---|---|
| Context | Isolated | Shared with conversation |
| Tool access | Full registry | Full registry |
| Execution | Synchronous blocking | Asynchronous streaming |
| Iterations | Max 10 (default) | Unlimited |
| Result | Returned to parent | Shown to user |
| Use case | Focused subtasks | Interactive conversation |
Best Practices
When to use subagents:- ✅ Isolated, well-defined subtasks
- ✅ Tasks requiring multiple tool calls
- ✅ Parallel execution of independent operations
- ✅ Complex multi-step procedures
- ❌ Simple single-tool operations (use tool directly)
- ❌ Tasks requiring user interaction
- ❌ Operations needing conversation context
- ❌ Recursive or deeply nested tasks
- Use kebab-case:
code-analysis,web-research - Keep short: 2-4 words
- Descriptive:
dependency-checknottask1
Advanced Usage
Parallel subagent execution: While individual subagents are synchronous, the parent agent can coordinate multiple subagents for parallel work:- Spawn subagent for task A → Get result A
- Spawn subagent for task B → Get result B
- Synthesize final answer from A + B
Implementation Details
SubagentManager:- Manages subagent lifecycle
- Tracks active tasks (ID, status, result)
- Provides tool registry to subagents
- Handles message bus communication
Testing
Fromsubagent_tool_test.go:
Test coverage:
- ✅ Name and description verification
- ✅ Parameter schema validation
- ✅ Context setting
- ✅ Successful task execution
- ✅ Execution without label (unnamed)
- ✅ Missing task parameter error
- ✅ Nil manager error handling
- ✅ Context passing verification
- ✅ ForUser truncation (500 char limit)
MockLLMProvider that returns:
- Subagent receives correct task
- Results are properly formatted
- Error handling works correctly