Skip to main content

What This Example Shows

  • Creating a hierarchical swarm with leader and worker agents
  • Coordinating multiple specialized medical professionals
  • Implementing role-based agent responsibilities
  • Managing complex multi-agent workflows

Installation

Get Your Swarms API Key

  1. Visit https://swarms.world/platform/api-keys
  2. Create an account or sign in
  3. Generate a new API key
  4. Store it securely in your environment variables

Code

Swarm Architecture Explained

Agent Roles

  • Leader Agent (Dr. Smith): Coordinates the team, makes final decisions
  • Worker Agents (Nurses & Assistant): Execute specific tasks and report back

Swarm Type: HierarchicalSwarm

This swarm type creates a clear chain of command where:
  1. The leader agent receives the initial task
  2. Worker agents process their specialized areas
  3. Results flow back to the leader for final synthesis
  4. The leader provides the comprehensive final output

Expected Output

The swarm will provide a coordinated medical assessment including:
  • Nurse Alice’s Assessment: Vital signs, immediate observations
  • Nurse Bob’s Care Plan: Medication and monitoring recommendations
  • Medical Assistant’s Support: Administrative and preparation tasks
  • Dr. Smith’s Final Diagnosis: Comprehensive treatment plan and coordination

Use Cases

This pattern is ideal for:
  • Medical Teams: Coordinated patient care and diagnosis
  • Legal Teams: Multi-expert document review and analysis
  • Research Teams: Collaborative data analysis and interpretation
  • Support Teams: Coordinated customer issue resolution
  • Development Teams: Code review and quality assurance

Environment Setup

Create a .env file in your project directory:

Customization Ideas

Adapt this pattern for:
  • Emergency Response: Fire, police, and medical coordination
  • Project Management: Team leads coordinating specialists
  • Quality Control: Inspectors coordinating with technicians
  • Customer Service: Supervisors managing support agents

Next Steps

After mastering hierarchical swarms, explore:
  • Sequential workflows for step-by-step processes
  • Concurrent workflows for parallel execution
  • Majority voting for consensus-based decisions
  • Agent routing for dynamic task distribution