Overview
Weaver offers two types of agent spawning:- Asynchronous Spawning (
spawntool): Launches a background agent that reports back when complete - Synchronous Spawning (
subagenttool): Executes a task and waits for the result
Spawn Tool (Async)
Thespawn tool launches a subagent in the background, allowing the parent agent to continue working while the subagent completes its task.
When to Use Spawn
Use async spawning for:- Long-running tasks that don’t block the parent
- Parallel execution of independent tasks
- Background monitoring or data collection
- Tasks where immediate results aren’t needed
Example Usage
Basic Spawn
The parent agent can spawn a subagent with a simple task:The subagent will:
- Start execution in the background
- Access the same tools as the parent
- Report results via the message bus when complete
Spawn Returns Immediately
The spawn tool returns a result immediately:The parent agent can continue with other tasks while the subagent works.
Spawn Implementation
From pkg/tools/spawn.go:58:Subagent Tool (Sync)
Thesubagent tool executes a task synchronously and returns the full result to the parent agent.
When to Use Subagent
Use synchronous subagents for:- Tasks where you need the result to continue
- Delegation of specialized work
- Breaking down complex tasks into manageable pieces
- Tasks with dependencies on the result
Example Usage
Subagent Executes with Tools
The subagent has access to all tools and can:
- Execute web searches
- Read and write files
- Run shell commands
- Spawn its own subagents (nested spawning)
max_tool_iterations times (default: 10).Subagent Implementation
From pkg/tools/subagent.go:255:Subagent Manager
The SubagentManager (pkg/tools/subagent.go:24) orchestrates all subagent execution:Key Features
- Task Tracking: Each spawned agent gets a unique ID (
subagent-1,subagent-2, etc.) - Tool Access: Subagents inherit the parent’s tool registry
- Message Bus: Results are published back via the message bus
- Context Cancellation: Supports graceful cancellation via context
Tool Loop Execution
Both spawn and subagent tools use the same tool loop (pkg/tools/toolloop.go):Tool Iteration Loop
The subagent executes up to
maxIterations times:- LLM generates response with optional tool calls
- Tools are executed with results fed back to LLM
- Loop continues until task is complete or max iterations reached
- Final response is returned as the result
Advanced Patterns
Parallel Task Execution
Spawn multiple agents for parallel work:Nested Subagents
Subagents can spawn their own subagents for recursive task decomposition:Task Monitoring
From pkg/tools/subagent.go:64:Configuration
Configure subagent behavior in your agent defaults:max_tool_iterations: Maximum tool calls per subagent executionworkspace: Directory where subagents can read/write filesrestrict_to_workspace: Sandbox subagents to workspace only
Best Practices
Choose the Right Spawn Type
Choose the Right Spawn Type
- Use
spawnfor fire-and-forget background tasks - Use
subagentwhen you need the result to continue - Consider task duration and dependencies
Provide Clear Task Descriptions
Provide Clear Task Descriptions
Subagents work best with specific, actionable tasks:Good:Bad:
Use Labels for Tracking
Use Labels for Tracking
Always provide a
label to identify subagent tasks:Handle Failures Gracefully
Handle Failures Gracefully
Subagents can fail due to:
- Context cancellation
- Max iterations exceeded
- Tool execution errors
Next Steps
- Learn about Multi-Channel Setup to spawn agents from different chat platforms
- Explore Docker Deployment for production agent orchestration
- Review Configuration for tuning subagent parameters