Overview
TheSwarmRouter class dynamically routes tasks to different swarm architectures based on user selection or automatic matching. It provides a unified interface for managing multiple swarm types and enables flexible task execution with logging, type validation, and metadata capture.
Class Definition
Parameters
Unique identifier for the SwarmRouter instance
Name identifier for the SwarmRouter instance
Description of the SwarmRouter’s purpose
Maximum number of execution loops
List of Agent objects or callables to use in the swarm
Type of swarm to use. Options: “AgentRearrange”, “MixtureOfAgents”, “SequentialWorkflow”, “ConcurrentWorkflow”, “GroupChat”, “MultiAgentRouter”, “HierarchicalSwarm”, “HeavySwarm”, “MajorityVoting”, “CouncilAsAJudge”, “BatchedGridWorkflow”, “LLMCouncil”, “DebateWithJudge”, “RoundRobin”
Whether to enable autosaving of swarm configuration, state, and metadata to workspace_dir/swarms/SwarmRouter/-/
If True, use timestamp in directory name; if False, use UUID
Flow configuration string for AgentRearrange swarm type
Whether to return results as JSON
Whether to auto-generate agent prompts
Shared memory system for agents
Rules to inject into every agent’s system prompt
List of document file paths to use
Output format type. Supported: ‘str’, ‘string’, ‘list’, ‘json’, ‘dict’, ‘yaml’, ‘xml’
Whether to enable multi-agent collaboration prompts
Enable verbose logging output
Available Swarm Types
- AgentRearrange: Optimizes agent arrangement for task execution with custom flow configuration
- MixtureOfAgents: Combines multiple agent types for diverse task perspectives
- SequentialWorkflow: Executes tasks sequentially through agents in order
- ConcurrentWorkflow: Executes tasks in parallel across agents
- GroupChat: Enables collaborative discussion and consensus-building
- MultiAgentRouter: Intelligent routing and load balancing across agents
- HierarchicalSwarm: Layered decision-making with director and worker agents
- HeavySwarm: High-capacity processing with specialized question generation and synthesis
- MajorityVoting: Democratic decision-making with voting mechanisms
- CouncilAsAJudge: Deliberative decision-making with expert evaluation
- BatchedGridWorkflow: Batched task processing across agent grids
- LLMCouncil: Council-based deliberation with chairman coordination
- DebateWithJudge: Structured debate with pro, con, and judge agents
- RoundRobin: Cycles through agents in round-robin fashion
Methods
reliability_check()
SwarmRouterConfigError: If swarm configuration is invalid
run()
The task to be executed by the swarm
Optional image input for the task
Optional list of tasks for batch processing (used by BatchedGridWorkflow)
The result of the swarm’s execution, format depends on output_type setting
SwarmRouterRunError: If task execution fails
batch_run()
A list of tasks to be executed by the swarm
Optional image input for all tasks
Optional list of images corresponding to each task
A list of results from the swarm’s execution for each task
concurrent_run()
The task to be executed by the swarm
Optional image input for the task
The result of the swarm’s concurrent execution
to_dict()
A dictionary representation of the SwarmRouter’s configuration and state
Usage Example
Autosave Configuration
Whenautosave=True, the SwarmRouter automatically saves:
- config.json: Initial configuration on initialization
- state.json: Current state after each run
- metadata.json: Execution metadata including results
workspace_dir/swarms/SwarmRouter/{swarm-name}-{timestamp}/