Overview
TheAutoSwarmBuilder class automatically builds and manages swarms of AI agents by intelligently decomposing tasks and creating specialized agents as needed. It uses a sophisticated boss agent system to delegate work, design agent architectures, and orchestrate multi-agent collaboration.
Class Definition
Parameters
str
default:"auto-swarm-builder"
The name of the swarm builder instance
str
default:"Auto Swarm Builder"
A description of the swarm builder’s purpose
bool
default:"True"
Whether to output detailed logs during execution
int
default:"1"
Maximum number of execution loops. Must be greater than 0
str
default:"gpt-5.4"
The LLM model to use for the boss agent that designs the swarm architecture
int
default:"8000"
Maximum tokens for the LLM responses from the boss agent
str
default:"return-agents"
Type of execution to perform. Options: “return-agents”, “return-swarm-router-config”, “return-agents-objects”
str
default:"BOSS_SYSTEM_PROMPT"
System prompt for the boss agent that designs swarm architectures. Defaults to comprehensive agent design prompt
dict
default:"{}"
Additional arguments to pass to the LiteLLM wrapper
bool
default:"False"
Default value for
run()’s execute parameter. When True, run() builds real Agent objects from a freshly generated spec and executes them through a SwarmRouter (via build_and_run_swarm()) instead of returning a spec-only resultExecution Types
return-agents
Returns agent specifications as a dictionary:return-swarm-router-config
Returns complete SwarmRouter configuration:return-agents-objects
Returns instantiated Agent objects ready for use:Methods
run()
str
required
The task to execute. The boss agent will analyze this task and design an appropriate swarm architecture
Optional[bool]
default:"None"
When
True, builds real agents and a SwarmRouter from a freshly generated spec and runs it (see build_and_run_swarm()), returning a dict that includes the execution output. When False, only a spec-only result is returned, per swarm_type. Defaults to self.auto_execute when left as NoneAny
When executing (
execute resolves to True): a dict from build_and_run_swarm(). Otherwise, the result depends on swarm_type:- “return-agents”: Dictionary with agent specifications
- “return-swarm-router-config”: SwarmRouter configuration dictionary
- “return-agents-objects”: List of instantiated Agent objects
ValueError: If swarm_type is invalidException: If there’s an error during swarm execution
build_and_run_swarm()
task and executes it. Generates one validated SwarmRouterConfig, builds the real Agent objects from that same spec’s agents, and runs a SwarmRouter with them — so the agents that execute are exactly the ones described in the returned metadata, unlike chaining create_agents() and initialize_swarm_router() (which make two independent, potentially-disagreeing LLM calls). This is what run() calls when execute resolves to True.
Parameters:
str
required
The task to design and run a swarm for
dict
{"name", "description", "agents" (names actually built), "swarm_type", "task", "output"} where output is the SwarmRouter’s execution resultcreate_agents()
str
required
The task to create agents for
dict
Dictionary containing agent specifications with comprehensive system prompts and configurations
Exception: If there’s an error during agent creation
create_agents_from_specs()
Any
required
Dictionary or Pydantic model containing agent specifications
List[Agent]
List of instantiated Agent objects ready for use
create_router_config()
str
required
The task to create router configuration for
dict
Complete SwarmRouter configuration including agents, swarm type, and execution parameters
Exception: If there’s an error during router config creation
batch_run()
List[str]
required
List of tasks to execute
List[Any]
List of results from each task execution
reliability_check()
ValueError: If max_loops is set to 0
list_types()
List[str]
List of available execution types
initialize_swarm_router()
SwarmRouterConfig for the task via the boss agent, then constructs and runs a SwarmRouter with the given agents. Not called automatically by run() for any swarm_type — call it directly if you want AutoSwarmBuilder to both design agents (via create_agents) and immediately execute them through a SwarmRouter.
Parameters:
List[Agent]
required
Agents to pass to the constructed
SwarmRouterstr
required
The task used to derive the
SwarmRouter configuration and to executeswarm_router.run(task)
Raises: Exception if router config generation or execution fails
dict_to_agent()
Agent objects directly from a raw {"agents": [...]} dictionary via Agent(**agent_config), without the field-name mapping (description → agent_description) that create_agents_from_specs() performs.
Parameters:
dict
required
Dictionary containing an
"agents" list of agent configuration dictsList[Agent]
Data Models
AgentSpec
SwarmRouterConfig
Boss Agent Design Principles
The AutoSwarmBuilder uses a sophisticated boss agent with expertise in:- Comprehensive Task Analysis: Deconstructing tasks into components and sub-tasks
- Agent Design Excellence: Creating agents with clear purposes and complementary personalities
- System Prompt Engineering: Crafting detailed prompts with role, capabilities, and protocols
- Multi-Agent Coordination: Designing communication channels and task handoff procedures
- Quality Assurance: Establishing success criteria and validation procedures
Usage Examples
Basic Agent Creation
Create Agent Objects
Generate SwarmRouter Config
Batch Processing
Advanced Features
Custom System Prompt
Additional LLM Arguments
Multi-Agent Architecture Types
The boss agent can design swarms using various architectures:- AgentRearrange: Dynamic task reallocation
- MixtureOfAgents: Parallel specialized processing
- SequentialWorkflow: Linear task progression
- ConcurrentWorkflow: Parallel execution
- GroupChat: Collaborative discussion
- HierarchicalSwarm: Layered decision-making
- HeavySwarm: High-capacity specialized processing
- MajorityVoting: Democratic decision-making
- And more…