OpenAI Swarm is an educational framework for exploring ergonomic, lightweight multi-agent orchestration. Managed by the OpenAI Solutions team, this project aims to provide developers with a simple, flexible, and controllable tool for building multi-agent systems.
Important Update: Swarm has now been superseded by the OpenAI Agents SDK, which is the production-ready version of Swarm. The OpenAI team recommends migrating all production use cases to the Agents SDK.
Swarm achieves agent coordination through two core abstractions:
pip install git+ssh://git@github.com/openai/swarm.git
# Or
pip install git+https://github.com/openai/swarm.git
from swarm import Swarm, Agent
client = Swarm()
def transfer_to_agent_b():
return agent_b
agent_a = Agent(
name="Agent A",
instructions="You are a helpful agent.",
functions=[transfer_to_agent_b],
)
agent_b = Agent(
name="Agent B",
instructions="Only speak in Haikus.",
)
response = client.run(
agent=agent_a,
messages=[{"role": "user", "content": "I want to talk to agent B."}],
)
print(response.messages[-1]["content"])
Output Example:
Hope glimmers brightly,
New paths converge gracefully,
What can I assist?
Swarm's client.run()
function implements the following loop:
Parameter | Type | Description | Default Value |
---|---|---|---|
agent | Agent | The (initial) agent to call | (Required) |
messages | List | List of message objects | (Required) |
context_variables | dict | Context variables | {} |
max_turns | int | Maximum number of turns | float("inf") |
model_override | str | Model override | None |
execute_tools | bool | Whether to execute tools | True |
stream | bool | Whether to enable streaming responses | False |
debug | bool | Whether to enable debug logging | False |
Field | Type | Description | Default Value |
---|---|---|---|
name | str | Agent name | "Agent" |
model | str | Model to use | "gpt-4o" |
instructions | str or function | Agent instructions | "You are a helpful agent." |
functions | List | List of callable functions | [] |
tool_choice | str | Tool selection strategy | None |
def instructions(context_variables):
user_name = context_variables["user_name"]
return f"Help the user, {user_name}, do whatever they want."
agent = Agent(instructions=instructions)
response = client.run(
agent=agent,
messages=[{"role":"user", "content": "Hi!"}],
context_variables={"user_name":"John"}
)
def greet(context_variables, language):
user_name = context_variables["user_name"]
greeting = "Hola" if language.lower() == "spanish" else "Hello"
print(f"{greeting}, {user_name}!")
return "Done"
agent = Agent(functions=[greet])
sales_agent = Agent(name="Sales Agent")
def transfer_to_sales():
return sales_agent
agent = Agent(functions=[transfer_to_sales])
def talk_to_sales():
print("Hello, World!")
return Result(
value="Done",
agent=sales_agent,
context_variables={"department": "sales"}
)
Swarm provides several example projects demonstrating different application scenarios:
Demonstrates basic usage of setup, function calls, handoffs, and context variables
Simple triage setup that forwards requests to the appropriate agent
Simple example demonstrating function calls
Multi-agent setup for handling different types of customer service requests for an airline
Includes a user interface agent and a help center agent with various tools
Personal shopping agent that helps with sales and refund order processing
Swarm supports streaming responses, using the same events as the Chat Completions API:
stream = client.run(agent, messages, stream=True)
for chunk in stream:
print(chunk)
New event types:
{"delim":"start"}
and {"delim":"end"}
- Mark the beginning and end of an agent processing a single message{"response": Response}
- Returns the complete Response object at the end of the streamUse run_demo_loop
for command-line testing:
from swarm.repl import run_demo_loop
run_demo_loop(agent, stream=True)
Swarm is particularly well-suited for the following situations:
Note: While Swarm has been superseded by the OpenAI Agents SDK, it remains an excellent educational resource for understanding the basic concepts and implementation methods of multi-agent systems. For production environments, it is recommended to migrate to the official Agents SDK.
OpenAI Swarm provides a simple yet powerful framework for learning and developing multi-agent systems. Through the two core concepts of agents and handoffs, developers can build complex AI workflows while maintaining code readability and maintainability. Although it has been superseded by the new SDK, its design philosophy and educational value remain important.