| layout | title | nav_order | has_children |
|---|---|---|---|
default |
OpenAI Swarm Tutorial |
71 |
true |
🐝 Build Agent Teams That Work Together Seamlessly
SwarmView Repo is an educational framework from OpenAI designed for lightweight multi-agent orchestration. It provides a simple, ergonomic way to coordinate multiple AI agents, enabling seamless handoffs and collaborative problem-solving.
| Concept | Description |
|---|---|
| Agents | Autonomous units with instructions, tools, and behaviors |
| Routines | Predefined sequences of actions an agent can perform |
| Handoffs | Seamless transfer of control between agents |
| Context Variables | Shared state passed between agents during handoffs |
| Function Calling | Tool integration for agent capabilities |
flowchart LR
A[User Request] --> B[Triage Agent]
B -->|Technical Issue| C[Support Agent]
B -->|Sales Query| D[Sales Agent]
B -->|General Info| E[Info Agent]
C -->|Escalation| F[Expert Agent]
D -->|Complex Deal| G[Manager Agent]
C --> H[Resolution]
D --> H
E --> H
F --> H
G --> H
classDef entry fill:#e1f5fe,stroke:#01579b
classDef agents fill:#f3e5f5,stroke:#4a148c
classDef escalation fill:#fff3e0,stroke:#ef6c00
classDef output fill:#e8f5e8,stroke:#1b5e20
class A entry
class B,C,D,E agents
class F,G escalation
class H output
- repository:
openai/swarm - stars: about 21.2k
- Chapter 1: Getting Started - Installation, setup, and your first Swarm agent
- Chapter 2: Agent Design - Creating agents with instructions and personalities
- Chapter 3: Function Calling & Tools - Equipping agents with capabilities
- Chapter 4: Routines - Building predefined action sequences
- Chapter 5: Agent Handoffs - Seamless control transfer between agents
- Chapter 6: Context Variables - Managing shared state across agents
- Chapter 7: Multi-Agent Patterns - Complex orchestration strategies
- Chapter 8: Production Considerations - Scaling, monitoring, and best practices
- Design Specialized Agents with distinct roles and capabilities
- Implement Smooth Handoffs between agents based on context
- Build Complex Workflows with multi-agent collaboration
- Manage Shared State using context variables
- Integrate External Tools through function calling
- Create Resilient Systems with proper error handling
- Scale Agent Systems for production use cases
- Python 3.10+
- OpenAI API key
- Basic understanding of LLMs and prompting
- Familiarity with async Python (helpful)
# Install Swarm
pip install git+https://github.com/openai/swarm.git
# Set your OpenAI API key
export OPENAI_API_KEY="your-api-key"from swarm import Swarm, Agent
client = Swarm()
# Define a simple agent
agent = Agent(
name="Helper",
instructions="You are a helpful assistant. Be concise and friendly."
)
# Run the agent
response = client.run(
agent=agent,
messages=[{"role": "user", "content": "Hello!"}]
)
print(response.messages[-1]["content"])from swarm import Swarm, Agent
client = Swarm()
# Transfer functions
def transfer_to_sales():
"""Transfer to sales agent for pricing and purchases."""
return sales_agent
def transfer_to_support():
"""Transfer to support agent for technical issues."""
return support_agent
# Define agents
triage_agent = Agent(
name="Triage",
instructions="""You are a customer service triage agent.
Determine the nature of the customer's request and transfer to the appropriate agent:
- Sales questions → transfer_to_sales()
- Technical issues → transfer_to_support()
""",
functions=[transfer_to_sales, transfer_to_support]
)
sales_agent = Agent(
name="Sales",
instructions="You are a sales agent. Help with pricing, plans, and purchases."
)
support_agent = Agent(
name="Support",
instructions="You are a technical support agent. Help troubleshoot issues."
)
# Run the system
response = client.run(
agent=triage_agent,
messages=[{"role": "user", "content": "I need help setting up my account"}]
)
print(f"Final agent: {response.agent.name}")
print(f"Response: {response.messages[-1]['content']}")- Chapters 1-3: Setup, basic agents, and function calling
- Focus on single-agent workflows
- Chapters 4-6: Routines, handoffs, and context management
- Build multi-agent systems
- Chapters 7-8: Complex patterns and production deployment
- Master enterprise-grade agent orchestration
Ready to build multi-agent systems? Let's begin with Chapter 1: Getting Started!
Generated for Awesome Code Docs
- Start Here: Chapter 1: Getting Started with OpenAI Swarm
- Back to Main Catalog
- Browse A-Z Tutorial Directory
- Search by Intent
- Explore Category Hubs
- Chapter 1: Getting Started with OpenAI Swarm
- Chapter 2: Agent Design
- Chapter 3: Function Calling & Tools
- Chapter 4: Routines
- Chapter 5: Agent Handoffs
- Chapter 6: Context Variables
- Chapter 7: Multi-Agent Patterns
- Chapter 8: Production Considerations
Generated by AI Codebase Knowledge Builder