Overview
TheHierarchicalSwarm class implements a hierarchical architecture where a director agent creates plans and distributes tasks to worker agents. The director can provide feedback and iterate on results through multiple loops to achieve desired outcomes while maintaining conversation history throughout the process.
Class Definition
Parameters
The name identifier for this swarm instance
A description of the swarm’s purpose and capabilities
The director agent that coordinates the swarm. If None, a default director will be created
List of worker agents available for task execution. Must not be empty
Maximum number of feedback loops the swarm can perform (must be > 0)
Format for the final output of the swarm
Model name for the feedback director
Name identifier for the director agent
Model name for the main director agent
Whether to add collaboration prompts to agents
Whether director feedback is enabled
Enable interactive dashboard with real-time monitoring
Custom system prompt for the director agent
Enable enhanced multi-agent collaboration prompts for worker agents
Temperature parameter for director agent’s LLM
Top-p parameter for director agent’s LLM
Whether to enable the planning phase before order distribution
Whether to enable autosaving of conversation history to workspace directory
Enable verbose logging output
Methods
run()
The initial task to be processed by the swarm. If None and interactive mode is enabled, will prompt for input
Optional image input for the agents
Callback function for streaming agent outputs. Parameters are (agent_name, chunk, is_final)
The formatted conversation history as output, formatted according to output_type configuration
- Director creates a plan and distributes orders to agents
- Agents execute tasks and report back to director
- Director evaluates results and issues new orders if needed (up to max_loops)
- All context and conversation history is preserved throughout
- Returns final output formatted per output_type
step()
The task to be processed in this step
Optional image input for the task
Callback for streaming outputs
The results from this step, either agent outputs or director feedback
- Director runs to create plan and orders
- Orders are parsed and distributed
- Agents execute assigned tasks
- Optional director feedback on results
display_hierarchy()
reliability_checks()
- At least one agent is provided
- max_loops is greater than 0
- Director is available (creates default if needed)
ValueError: If swarm configuration is invalid
Data Models
HierarchicalOrder
SwarmSpec
Interactive Dashboard
Wheninteractive=True, the HierarchicalSwarm displays a real-time dashboard with:
- Operations Status: Swarm name, description, current loop, agent count
- Director Operations: Current plan and active orders
- Agent Monitoring Matrix: Real-time agent status, tasks, and outputs
- Progress Tracking: Loop completion and runtime metrics
Usage Example
Conversation Autosave
Whenautosave=True, conversation history is automatically saved to:
workspace_dir/swarms/HierarchicalSwarm/{swarm-name}-{timestamp}/conversation_history.json
This enables:
- Post-execution analysis
- Debugging and monitoring
- Historical tracking of swarm operations