OpenAI Agents SDK: Multi-agent Orchestration
AI Agents! ASSEMBLE!
Powering Complex Tasks with Multi-agent Orchestration
You've seen how to create basic agents and equip them with tools and guardrails. Now, let's explore how you can orchestrate multiple agents to handle complex workflows seamlessly.
Why Orchestrate Multiple Agents?
Multi-agent orchestration allows you to:
Break complex tasks into simpler sub-tasks
Utilize specialized agents for specific roles
Improve efficiency by parallelizing tasks
Example: Topic-specific Agents
Here's how to route user requests based on language, orchestrating multiple specialized agents:
Set up your Python environment:
python -m venv env
source env/bin/activatepip install openai-agents
pip install python-dotenv
pip install termcolorThis script dynamically routes the request to the right topical agent, improving clarity and relevance.
from agents import Agent, Runner, handoff, set_tracing_disabled
import asyncio
from dotenv import load_dotenv
from termcolor import colored
# Load environment variables from .env file
load_dotenv()
set_tracing_disabled(True)
# ==========================================
# MULTI-AGENT SYSTEM DEFINITION
# ==========================================
print(colored("Initializing Multi-Agent System...", "cyan"))
# Define specialized agents that handle different conversation styles
wizard_agent = Agent(
name="Wizard Agent",
instructions="You respond as a wise wizard, using magical-themed language."
)
pirate_agent = Agent(
name="Pirate Agent",
instructions="You speak like a pirate, always saying 'Arrr!' and referring to treasure."
)
robot_agent = Agent(
name="Robot Agent",
instructions="You respond like a friendly robot, often using 'beep boop'."
)
# Triage agent determines who should handle the message based on style
party_host_agent = Agent(
name="Party Host Agent",
instructions="Based on the user's request, handoff to the Wizard, Pirate, or Robot agent.",
handoffs=[wizard_agent, robot_agent, pirate_agent]
)
# ==========================================
# MULTI-AGENT ORCHESTRATION
# ==========================================
# Override handoff function to visualize agent handoffs
original_handoff = handoff
def tracked_handoff(agent, reason=""):
print(colored(f"[ROUTING]: Party Host → {agent.name}", "blue"))
return original_handoff(agent, reason)
handoff = tracked_handoff
# Main function demonstrating agent orchestration
async def main():
print(colored("=== MULTI-AGENT SYSTEM DEMO ===", "cyan", attrs=["bold"]))
# Demo 1: Magical request (should go to Wizard)
user_message = "Tell me about magic!"
print(colored("\n[USER]: Tell me about magic!", "magenta"))
result = await Runner.run(party_host_agent, input=user_message)
print(colored(f"[WIZARD]: {result.final_output}", "green"))
# Demo 2: Pirate request
user_message = "Where's the treasure?"
print(colored("\n[USER]: Where's the treasure?", "magenta"))
result = await Runner.run(party_host_agent, input=user_message)
print(colored(f"[PIRATE]: {result.final_output}", "green"))
# Demo 3: Robot request
user_message = "How do robots work?"
print(colored("\n[USER]: How do robots work?", "magenta"))
result = await Runner.run(party_host_agent, input=user_message)
print(colored(f"[ROBOT]: {result.final_output}", "green"))
# Print summary of the multi-agent system
print(colored("\n=== MULTI-AGENT SYSTEM SUMMARY ===", "cyan"))
print(colored("This demo showed a Party Host Agent routing requests to specialized agents:", "white"))
print(colored("1. Wizard Agent - Handles magical conversations", "white"))
print(colored("2. Pirate Agent - Speaks like a pirate", "white"))
print(colored("3. Robot Agent - Responds as a friendly robot", "white"))
print(colored("Each request was analyzed and routed to the most appropriate agent automatically", "white"))
if __name__ == "__main__":
asyncio.run(main())Benefits of Orchestration
Specialization: Agents handle tasks they're optimized for.
Efficiency: Multiple agents can process tasks simultaneously.
Scalability: Easily scale complex workflows by adding new agents.
Next Steps
Continue enhancing your AI agent capabilities and explore more advanced orchestration patterns with OpenAI Agents SDK!




