Swarm is an experimental, educational multi-agent orchestration framework maintained by the OpenAI Solution team, and has now been superseded by the OpenAI Agents SDK; the official recommendation is to migrate all production scenarios to the Agents SDK. It requires Python 3.10+.
Purpose: to keep agent coordination and execution lightweight, highly controllable, and easy to test, making it suitable for handling a large number of independent capabilities and instructions that are difficult to write into a single prompt.
Main capabilities: at its core are two primitives—Agent and handoff. An Agent encapsulates instructions and tools, and can decide at any time to hand off the conversation to another Agent. It can express rich dynamics between tools and a network of agents. Swarm does not represent the Assistants in the Assistants API; it is entirely based on the Chat Completions API, and calls are stateless (no state is saved).
Usage: instantiate the Swarm client (which internally instantiates the OpenAI client). client.run() is similar to chat.completions.create(), accepting messages and returning messages, but it handles Agent function execution, handoffs, context variable references, and may return only after multiple rounds. Its loop is: get a completion from the current Agent, execute tool calls and append the results, switch Agents when necessary, update context variables when necessary, and return if there are no new function calls. The returned Response contains the new messages, the last Agent called, and the latest context_variables, and can be passed back into the next run() to continue the interaction.
An Agent's instructions are converted into the first message as the system prompt, and at any time only the current Agent's instructions are retained; they can be a str or a function that returns a str, and the function can receive context_variables. An Agent can directly call Python functions: a function usually returns a str (it will attempt to convert to str); if it returns an Agent, execution is handed off; if a context_variables parameter is defined, it will be populated with the passed-in value; if a function call errors, the error response is appended to the conversation so it can recover; multiple functions are executed in order. Handoff is implemented by returning another Agent in a function, and it can also return a more completeResult object to simultaneously update context_variables, the value, and the agent. Functions are automatically converted into JSON Schema and passed into Chat Completions tools, the docstring is converted into the function description, parameters without default values are set as required, type hints are mapped to types, and per-parameter descriptions are not currently supported.
Examples: basic, triage_agent, weather_agent, airline, support_bot, personal_shopper. run_demo_loop can be used to test in the command line as a REPL, with streaming support; streaming adds {"delim":"start"}/{"delim":"end"} and an event that returns an aggregated Response at the end. It is recommended to bring your own evaluation suite to test swarm performance.