AI Agent Hub
AI エージェント / プラグイン情報メディア
← 一覧へ
Swarm
CODEX

Swarm

Swarm

OpenAI が公開した教育目的の軽量マルチエージェントフレームワーク。Agent 間のハンドオフをシンプルな関数呼び出しで表現する設計を学べる(後継は Agents SDK)。

原文: Educational framework exploring ergonomic, lightweight multi-agent orchestration. Managed by OpenAI Solution team.
#公式#学習用#マルチエージェント
REPO STATS

リポジトリ統計

⭐ Stars
21.4k
🍴 Forks
2.3k
⚠️ Open Issues
26
🌿 Language
Python
📄 License
MIT
🕒 最終更新
2026.04.16 (3週間前)
📅 公開日
2024.02.23
🌿 Branch
main
README

ドキュメント

— AI による自動翻訳 (2026.05.07 更新)

Swarm Logo

Swarm(実験的、教育用)

重要

Swarm は OpenAI Agents SDK に置き換えられました。これは Swarm の本番環境対応版の進化版です。Agents SDK は重要な改善を備えており、OpenAI チームによって積極的に保守されます。

本番環境のすべてのユースケースでは、Agents SDK への移行をお勧めします。

インストール

Python 3.10 以上が必須です

pip install git+ssh://git@github.com/openai/swarm.git

または

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"])
Hope glimmers brightly,
New paths converge gracefully,
What can I assist?

目次

概要

Swarm は、エージェント間の調整実行を軽量で、高度に制御可能で、簡単にテストできるようにすることに焦点を当てています。

これは 2 つの基本的な抽象化により実現されます:Agentハンドオフです。Agentinstructionstoolsを包含し、いつでも会話を別のAgentに引き継ぐことを選択できます。

これらの基本概念は、ツールとエージェントネットワーク間の豊かなダイナミクスを表現するのに十分な力を持ち、急な学習曲線を避けながら、スケーラブルな実世界のソリューションを構築できます。

注記

Swarm Agents は Assistants API の Assistants とは関連がありません。便宜上、同じような名前になっていますが、それ以外は完全に無関係です。Swarm は Chat Completions API によってのみ駆動され、したがってコール間でステートレスです。

Swarm が選ばれる理由

Swarm は、軽量でスケーラブルかつ設計段階で高度にカスタマイズ可能なパターンを探索します。Swarm と同様のアプローチは、多数の独立した機能と指示を扱う場合に最適です。これらは単一のプロンプトにエンコードするのが難しいものです。

Assistants API は、完全にホストされたスレッドと組み込みのメモリ管理および取得機能を探している開発者にとって優れたオプションです。一方、Swarm はマルチエージェント オーケストレーションについて学ぶことに関心のある開発者向けの教育的リソースです。Swarm はほぼ完全にクライアント上で実行され、Chat Completions API と同様に、呼び出し間でステータスを保存しません。

/examples をチェックしてインスピレーションを得てください!各例の README で詳しく学べます。

  • basic: セットアップ、関数呼び出し、ハンドオフ、コンテキスト変数などの基礎的な簡単な例
  • triage_agent: 基本的なトリアージステップを設定して適切なエージェントにハンドオフする簡単な例
  • weather_agent: 関数呼び出しの簡単な例
  • airline: 航空会社のコンテキストで異なるカスタマーサービスリクエストを処理するためのマルチエージェント設定
  • support_bot: ユーザーインターフェースエージェントとヘルプセンターエージェント(複数のツール付き)を含むカスタマーサービスボット
  • personal_shopper: 販売と注文の払い戻しを支援できるパーソナルショッピングエージェント

ドキュメント

Swarm Diagram

Swarmの実行

Swarmクライアントをインスタンス化することから始めます(内部的には単にOpenAIクライアントをインスタンス化するだけです)。

from swarm import Swarm

client = Swarm()

client.run()

Swarmのrun()関数はChat Completions APIのchat.completions.create()関数に相当します。これはmessagesを受け取りmessagesを返し、呼び出し間での状態を保存しません。ただし重要なことに、Agent関数の実行、ハンドオフ、コンテキスト変数の参照を処理し、ユーザーに戻る前に複数のターンを実行することができます。

本質的には、Swarmのclient.run()は以下のループを実装しています。

  1. 現在のAgentから完了を取得する
  2. ツール呼び出しを実行し、結果を追加する
  3. 必要に応じてAgentを切り替える
  4. 必要に応じてコンテキスト変数を更新する
  5. 新しい関数呼び出しがない場合は、返す

引数

引数 説明 デフォルト
agent Agent 呼び出される(最初の)Agent。 (必須)
messages List メッセージオブジェクトのリスト。Chat Completions messagesと同じです。 (必須)
context_variables dict 関数とAgent指示に利用可能な追加のコンテキスト変数の辞書。 {}
max_turns int 許可される会話ターンの最大数。 float("inf")
model_override str Agentによって使用されるモデルをオーバーライドするオプション文字列。 None
execute_tools bool Falseの場合、Agentが関数を呼び出そうとするときに実行を中断し、tool_callsメッセージを即座に返します。 True
stream bool Trueの場合、ストリーミング応答を有効にします。 False
debug bool Trueの場合、デバッグログを有効にします。 False

client.run()が完了すると(agentとツールへの複数の呼び出しの後の可能性があります)、すべての関連する更新された状態を含むResponseを返します。具体的には、新しいmessages、呼び出された最後のAgent、および最新のcontext_variablesです。これらの値(加えて新しいユーザーメッセージ)を次のclient.run()の実行に渡して、中断したところから相互作用を続けることができます。これはchat.completions.create()と同じようなものです。(run_demo_loop関数は/swarm/repl/repl.py内での完全な実行ループの例を実装しています。)

Responseフィールド

フィールド 説明
messages List 会話中に生成されたメッセージオブジェクトのリスト。Chat Completions messagesと非常に似ていますが、メッセージがどのAgentから発信されたかを示すsenderフィールドを持っています。
agent Agent メッセージを処理した最後のAgent。
context_variables dict 入力変数と同じですが、変更があればそれを含みます。

Agents

Agentinstructionsのセットとfunctionsのセット(加えて以下の追加設定)をカプセル化したもので、実行を別のAgentにハンドオフする機能を備えています。

Agentを「Xをする誰か」として人格化したくなるかもしれませんが、instructionsfunctionsのセットで定義された非常に特定のワークフローまたはステップ(例えば、ステップのセット、複雑な取得、データ変換の単一ステップなど)を表すのに使うこともできます。これによりAgentは「agent」「workflow」「task」のネットワークに組み合わせることができ、すべて同じプリミティブで表現されます。

Agent フィールド

フィールド 説明 デフォルト値
name str エージェントの名前です。 "Agent"
model str エージェントで使用するモデルです。 "gpt-4o"
instructions str または func() -> str エージェント用の指示です。文字列または文字列を返す呼び出し可能なものです。 "You are a helpful agent."
functions List エージェントが呼び出せる関数のリストです。 []
tool_choice str エージェントのツール選択(ある場合)です。 None

指示(Instructions)

Agentinstructions は会話の system プロンプト(最初のメッセージとして)に直接変換されます。任意の時点で、アクティブな Agentinstructions だけが存在します(例えば、Agent ハンドオフがある場合、system プロンプトは変更されますが、チャット履歴は変更されません)。

agent = Agent(
   instructions="You are a helpful agent."
)

instructions は通常の str または str を返す関数です。この関数は context_variables パラメーター(client.run() に渡される context_variables で入力されます)をオプションで受け取ることができます。

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"}
)
print(response.messages[-1]["content"])
Hi John, how can I assist you today?

関数

  • Swarm Agent は Python 関数を直接呼び出すことができます。
  • 関数は通常 str を返すべきです(値は str にキャストされます)。
  • 関数が Agent を返す場合、実行はその Agent に転送されます。
  • 関数が context_variables パラメータを定義する場合、client.run() に渡された context_variables で入力されます。
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]
)

client.run(
   agent=agent,
   messages=[{"role": "user", "content": "Usa greet() por favor."}],
   context_variables={"user_name": "John"}
)
Hola, John!
  • Agent 関数呼び出しにエラー(関数がない、引数が間違っている、エラーがある)がある場合、エラーレスポンスがチャットに追加され、Agent は正常に回復できます。
  • Agent によって複数の関数が呼び出される場合、それらはその順序で実行されます。

ハンドオフとコンテキスト変数の更新

Agent は関数で別の Agent を返すことによってハンドオフできます。

sales_agent = Agent(name="Sales Agent")

def transfer_to_sales():
   return sales_agent

agent = Agent(functions=[transfer_to_sales])

response = client.run(agent, [{"role":"user", "content":"Transfer me to sales."}])
print(response.agent.name)
Sales Agent

また、より完全な Result オブジェクトを返すことで context_variables を更新することもできます。これは valueagent も含むことができ、単一の関数が値を返し、エージェントを更新し、コンテキスト変数を更新したい場合(またはこれらの3つの任意のサブセット)に便利です。

— GitHub から取得した原文(一部省略の場合あり)

Swarm Logo

Swarm (experimental, educational)

Important

Swarm is now replaced by the OpenAI Agents SDK, which is a production-ready evolution of Swarm. The Agents SDK features key improvements and will be actively maintained by the OpenAI team.

We recommend migrating to the Agents SDK for all production use cases.

Install

Requires Python 3.10+

pip install git+ssh://git@github.com/openai/swarm.git

or

pip install git+https://github.com/openai/swarm.git

Usage

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"])
Hope glimmers brightly,
New paths converge gracefully,
What can I assist?

Table of Contents

Overview

Swarm focuses on making agent coordination and execution lightweight, highly controllable, and easily testable.

It accomplishes this through two primitive abstractions: Agents and handoffs. An Agent encompasses instructions and tools, and can at any point choose to hand off a conversation to another Agent.

These primitives are powerful enough to express rich dynamics between tools and networks of agents, allowing you to build scalable, real-world solutions while avoiding a steep learning curve.

Note

Swarm Agents are not related to Assistants in the Assistants API. They are named similarly for convenience, but are otherwise completely unrelated. Swarm is entirely powered by the Chat Completions API and is hence stateless between calls.

Why Swarm

Swarm explores patterns that are lightweight, scalable, and highly customizable by design. Approaches similar to Swarm are best suited for situations dealing with a large number of independent capabilities and instructions that are difficult to encode into a single prompt.

The Assistants API is a great option for developers looking for fully-hosted threads and built in memory management and retrieval. However, Swarm is an educational resource for developers curious to learn about multi-agent orchestration. Swarm runs (almost) entirely on the client and, much like the Chat Completions API, does not store state between calls.

Examples

Check out /examples for inspiration! Learn more about each one in its README.

  • basic: Simple examples of fundamentals like setup, function calling, handoffs, and context variables
  • triage_agent: Simple example of setting up a basic triage step to hand off to the right agent
  • weather_agent: Simple example of function calling
  • airline: A multi-agent setup for handling different customer service requests in an airline context.
  • support_bot: A customer service bot which includes a user interface agent and a help center agent with several tools
  • personal_shopper: A personal shopping agent that can help with making sales and refunding orders

Documentation

Swarm Diagram

Running Swarm

Start by instantiating a Swarm client (which internally just instantiates an OpenAI client).

from swarm import Swarm

client = Swarm()

client.run()

Swarm's run() function is analogous to the chat.completions.create() function in the Chat Completions API – it takes messages and returns messages and saves no state between calls. Importantly, however, it also handles Agent function execution, hand-offs, context variable references, and can take multiple turns before returning to the user.

At its core, Swarm's client.run() implements the following loop:

  1. Get a completion from the current Agent
  2. Execute tool calls and append results
  3. Switch Agent if necessary
  4. Update context variables, if necessary
  5. If no new function calls, return

Arguments

Argument Type Description Default
agent Agent The (initial) agent to be called. (required)
messages List A list of message objects, identical to Chat Completions messages (required)
context_variables dict A dictionary of additional context variables, available to functions and Agent instructions {}
max_turns int The maximum number of conversational turns allowed float("inf")
model_override str An optional string to override the model being used by an Agent None
execute_tools bool If False, interrupt execution and immediately returns tool_calls message when an Agent tries to call a function True
stream bool If True, enables streaming responses False
debug bool If True, enables debug logging False

Once client.run() is finished (after potentially multiple calls to agents and tools) it will return a Response containing all the relevant updated state. Specifically, the new messages, the last Agent to be called, and the most up-to-date context_variables. You can pass these values (plus new user messages) in to your next execution of client.run() to continue the interaction where it left off – much like chat.completions.create(). (The run_demo_loop function implements an example of a full execution loop in /swarm/repl/repl.py.)

Response Fields

Field Type Description
messages List A list of message objects generated during the conversation. Very similar to Chat Completions messages, but with a sender field indicating which Agent the message originated from.
agent Agent The last agent to handle a message.
context_variables dict The same as the input variables, plus any changes.

Agents

An Agent simply encapsulates a set of instructions with a set of functions (plus some additional settings below), and has the capability to hand off execution to another Agent.

While it's tempting to personify an Agent as "someone who does X", it can also be used to represent a very specific workflow or step defined by a set of instructions and functions (e.g. a set of steps, a complex retrieval, single step of data transformation, etc). This allows Agents to be composed into a network of "agents", "workflows", and "tasks", all represented by the same primitive.

Agent Fields

Field Type Description Default
name str The name of the agent. "Agent"
model str The model to be used by the agent. "gpt-4o"
instructions str or func() -> str Instructions for the agent, can be a string or a callable returning a string. "You are a helpful agent."
functions List A list of functions that the agent can call. []
tool_choice str The tool choice for the agent, if any. None

Instructions

Agent instructions are directly converted into the system prompt of a conversation (as the first message). Only the instructions of the active Agent will be present at any given time (e.g. if there is an Agent handoff, the system prompt will change, but the chat history will not.)

agent = Agent(
   instructions="You are a helpful agent."
)

The instructions can either be a regular str, or a function that returns a str. The function can optionally receive a context_variables parameter, which will be populated by the context_variables passed into client.run().

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"}
)
print(response.messages[-1]["content"])
Hi John, how can I assist you today?

Functions

  • Swarm Agents can call python functions directly.
  • Function should usually return a str (values will be attempted to be cast as a str).
  • If a function returns an Agent, execution will be transferred to that Agent.
  • If a function defines a context_variables parameter, it will be populated by the context_variables passed into client.run().
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]
)

client.run(
   agent=agent,
   messages=[{"role": "user", "content": "Usa greet() por favor."}],
   context_variables={"user_name": "John"}
)
Hola, John!
  • If an Agent function call has an error (missing function, wrong argument, error) an error response will be appended to the chat so the Agent can recover gracefully.
  • If multiple functions are called by the Agent, they will be executed in that order.

Handoffs and Updating Context Variables

An Agent can hand off to another Agent by returning it in a function.

sales_agent = Agent(name="Sales Agent")

def transfer_to_sales():
   return sales_agent

agent = Agent(functions=[transfer_to_sales])

response = client.run(agent, [{"role":"user", "content":"Transfer me to sales."}])
print(response.agent.name)
Sales Agent

It can also update the context_variables by returning a more complete Result object. This can also contain a value and an agent, in case you want a single function to return a value, update the agent, and update the context variables (or any subset of the three).