A Swarm is a collection of multiple agents working together to accomplish complex tasks. Just as individual agents combine LLM + Tools + Memory, swarms combine multiple agents with different specializations, perspectives, and capabilities to solve problems that would be difficult or impossible for a single agent.
Why Swarms? Complex tasks often require different types of expertise, perspectives, and approaches. Swarms enable you to decompose problems and leverage specialized agents working in harmony.
Pattern: Agents execute tasks in a linear chain, where each agent builds upon the previous agent’s output.Best For: Step-by-step processes, data transformation pipelines, content creation workflows
from swarms import Agent, SequentialWorkflow# Create specialized agentsresearcher = Agent( agent_name="Researcher", system_prompt="Research topics and gather comprehensive information.", model_name="gpt-4o-mini",)writer = Agent( agent_name="Writer", system_prompt="Transform research into engaging, well-structured content.", model_name="gpt-4o-mini",)editor = Agent( agent_name="Editor", system_prompt="Review and polish content for clarity and correctness.", model_name="gpt-4o-mini",)# Create sequential workflow: Researcher -> Writer -> Editorworkflow = SequentialWorkflow(agents=[researcher, writer, editor])# Execute the workflowfinal_article = workflow.run("Write an article about quantum computing")print(final_article)
Pattern: All agents receive the same task and execute simultaneously, providing diverse perspectives.Best For: Analysis tasks, getting multiple viewpoints, parallel data processing
from swarms import Agent, ConcurrentWorkflow# Create expert analystsmarket_analyst = Agent( agent_name="Market-Analyst", system_prompt="Analyze market trends and competitive landscape.", model_name="gpt-4o-mini",)financial_analyst = Agent( agent_name="Financial-Analyst", system_prompt="Analyze financial metrics and profitability.", model_name="gpt-4o-mini",)risk_analyst = Agent( agent_name="Risk-Analyst", system_prompt="Identify and assess potential risks.", model_name="gpt-4o-mini",)# Run all agents concurrentlyworkflow = ConcurrentWorkflow( agents=[market_analyst, financial_analyst, risk_analyst])# All agents analyze the same task simultaneouslyanalysis_results = workflow.run( "Analyze the investment potential of renewable energy sector")
Pattern: Multiple expert agents process tasks in parallel, then an aggregator synthesizes their outputs.Best For: Complex decision-making, leveraging diverse expertise, state-of-the-art performance
from swarms import Agent, MixtureOfAgents# Create expert agentsfinancial_expert = Agent( agent_name="Financial-Expert", system_prompt="Expert in financial analysis and investment strategies.", model_name="gpt-4o-mini")market_expert = Agent( agent_name="Market-Expert", system_prompt="Expert in market trends and competitive analysis.", model_name="gpt-4o-mini")risk_expert = Agent( agent_name="Risk-Expert", system_prompt="Expert in risk assessment and mitigation.", model_name="gpt-4o-mini")# Create aggregator to synthesize expert opinionsaggregator = Agent( agent_name="Investment-Advisor", system_prompt="Synthesize expert analyses into actionable recommendations.", model_name="gpt-4o-mini")# Create MoA swarmmoa_swarm = MixtureOfAgents( agents=[financial_expert, market_expert, risk_expert], aggregator_agent=aggregator,)recommendation = moa_swarm.run("Should we invest in NVIDIA stock?")
Pattern: A director agent creates plans and distributes tasks to specialized worker agents.Best For: Complex project management, team coordination, hierarchical decision-making
from swarms import Agent, HierarchicalSwarm# Create specialized workerscontent_strategist = Agent( agent_name="Content-Strategist", system_prompt="Develop content strategies and editorial calendars.", model_name="gpt-4o-mini")creative_director = Agent( agent_name="Creative-Director", system_prompt="Create compelling advertising concepts and campaigns.", model_name="gpt-4o-mini")seo_specialist = Agent( agent_name="SEO-Specialist", system_prompt="Optimize content for search engines and organic growth.", model_name="gpt-4o-mini")# Director coordinates the teammarketing_swarm = HierarchicalSwarm( name="Marketing-Team", description="Comprehensive marketing team for product launches", agents=[content_strategist, creative_director, seo_specialist], max_loops=2, # Allow for feedback and refinement)strategy = marketing_swarm.run( "Develop a marketing strategy for our new SaaS product launch")
Pattern: Agents engage in conversational collaboration, discussing and debating solutions.Best For: Brainstorming, decision-making, collaborative problem-solving
from swarms import Agent, GroupChat# Create agents with different perspectivesoptimist = Agent( agent_name="Optimist", system_prompt="Present the benefits and opportunities of ideas.", model_name="gpt-4o-mini")critic = Agent( agent_name="Critic", system_prompt="Identify potential problems and challenges.", model_name="gpt-4o-mini")realist = Agent( agent_name="Realist", system_prompt="Provide balanced, practical perspectives.", model_name="gpt-4o-mini")# Create group chatchat = GroupChat( agents=[optimist, critic, realist], max_loops=4, # Number of conversation rounds)conversation = chat.run( "Should we adopt AI agents for customer support?")
from swarms.structs.swarm_router import SwarmRouter, SwarmType# Use the same agents with different strategiesrouter = SwarmRouter( swarm_type=SwarmType.SequentialWorkflow, # or ConcurrentWorkflow, MixtureOfAgents, etc. agents=[agent1, agent2, agent3])result = router.run(task)