はじめに
AIエージェントに「何でもできる道具」を全部詰め込むと、どの機能をどう使うかが複雑になり、プロンプトで正しく指示しても意図通りに動かないことが多くなります。
この課題を解決する方法のひとつが Supervisor(スーパーバイザー)エージェント です。
Supervisor は 司令塔 の役割を持ち、「四則演算だけ担当」「単位変換だけ担当」といった専門エージェントに仕事を割り振ります。
役割を明確に分けることで、1つのAIに全部やらせる場合よりも指示が通りやすくなり、 開発後の修正や機能追加も簡単になります。
特に Supervisor(as tool-calling) という方法では、専門エージェントを“道具”のように呼び出し、必要な順番で組み合わせて使うことが可能です。これにより、複雑なタスクでも工程を分解して確実に処理できます。
本記事では、四則演算用と単位変換用の2種類のエージェントを Supervisor が連携させる例を通して、 その仕組みとプロンプト設計のポイントを確認します。
マルチエージェントシステムのパターン
LangGraph では、複数のエージェントを連携させるためのさまざまなパターンが用意されています。
ここでは公式で紹介されているパターンの概要を整理し、その中でも今回取り上げる Supervisor(as tool-calling) に注目します。
パターン | 特徴(公式の要点) | 主な適用例 |
---|---|---|
Single agent(参考) | 単一のエージェントのみで処理を完結。最小構成。 ※本記事の対象外だが基準として記載。 |
FAQボット、定型Q&A、単発要約など「1本の流れ」で完結する処理 |
Network | エージェント同士が相互通信し、次に呼ぶ相手を自分で決定する多対多構造。 順序や上下関係が曖昧な問題に適する。 |
旅行計画(調査↔見積↔日程調整を相互呼び出し)、創作支援(プロット↔設定監修↔文体調整を自由遷移) |
Supervisor | 中央の監督(スーパーバイザー)が次に呼ぶエージェントを決定。 明確な分担と逐次判断が必要な場面に向く。 |
工程管理(調査→計算→要約→レビュー)、受発注の段階審査(入力検証→照会→承認) |
Supervisor(tool-calling) | サブエージェントをツールとして公開し、Supervisorがツールコールで呼び出す。 Supervisorは「ツール呼び出しを繰り返すLLMループ」として動作。 |
社内ボット(調査・計画・実行を関数委譲)、複雑な業務フローの自動化 |
Hierarchical | 監督の監督を置く多層構造。 チーム単位のSupervisorをさらに上位が束ね、大規模・複雑な制御に対応。 |
大規模RAGチームの統括(法務・財務・技術チーム)、社内ナレッジ横断案件 |
Custom multi-agent workflow | 一部の流れを固定しつつ、一部は動的にルーティング可能なカスタム構成。 通信相手を部分集合に制限可能。 |
固定工程+要確認時のみ専門家エージェントにハンドオフする検品プロセス |
今回の記事で扱うのは Supervisor(tool-calling) パターンです。
これは、専門エージェントを関数のように呼び出し、Supervisorがタスクを順序立てて実行させる方式です。 役割分担を明確に保ちながら、複雑なタスクも組み立てられる点が特徴です。
Supervisor as tool-calling の例
ここでは、Supervisor(tool-calling) パターンを使った具体的な実装例を紹介します。
今回の構成は以下の3つのエージェントで構成されます。
- Supervisorエージェント:司令塔の役割を持つ。タスクを解析し、どのエージェントに任せるかを判断する
- ArithmeticAgent(四則演算エージェント):加算・減算・乗算・除算のみを担当
- UnitConversionAgent(単位変換エージェント):メートル⇔フィート、摂氏⇔華氏、kg⇔ポンドなどの単位変換を担当
Supervisorは、必要に応じてこれらのエージェントをツールとして呼び出し、タスクを順序立てて処理します。
ソースコードは下記から取得できます
from langchain_core.tools import tool
from langchain.chat_models import init_chat_model
from langchain_core.messages import convert_to_messages
from langgraph_supervisor import create_supervisor
from langgraph.prebuilt import create_react_agent
# 四則演算ツールの定義
@tool("add", description="Adds two numbers.")
def add(a: float, b: float) -> float:
"""Adds two numbers."""
print(f"Adding {a} and {b}")
return a + b
@tool("subtract", description="Subtracts the second number from the first.")
def subtract(a: float, b: float) -> float:
"""Subtracts the second number from the first."""
print(f"Subtracting {b} from {a}")
return a - b
@tool("multiply", description="Multiplies two numbers.")
def multiply(a: float, b: float) -> float:
"""Multiplies two numbers."""
print(f"Multiplying {a} and {b}")
return a * b
@tool("divide", description="Divides the first number by the second.")
def divide(a: float, b: float) -> float:
"""Divides the first number by the second."""
print(f"Dividing {a} by {b}")
if b == 0:
return "Error: Division by zero"
return a / b
# エージェントの初期化
arithmetic_agent = create_react_agent(
model=init_chat_model("openai:gpt-4.1-mini"),
tools=[add, subtract, multiply, divide],
name="ArithmeticAgent",
prompt=(
"You are an Arithmetic Agent.\n\n"
"INSTRUCTIONS:\n"
"- Your task is to perform arithmetic operations such as addition, subtraction, multiplication, and division.\n"
"- Do NOT perform any unit conversions or handle non-arithmetic tasks.\n"
"- Respond ONLY with the numerical result of the calculation. Do NOT include any explanations or additional text.\n"
"- If the input is invalid or cannot be calculated, respond with 'Error: Invalid input'."
),
)
arithmetic_agent のプロンプトの意図
-
役割の固定化
「四則演算のみ」と明記し、範囲外タスクを避ける -
出力形式の統一
数値のみ返す仕様にし、パースやテストを容易にする -
エラー応答の統一
異常時は固定メッセージで返す
# 単位変換ツールの定義
@tool("feet_to_meters", description="Converts feet to meters.")
def feet_to_meters(feet: float) -> float:
"""Converts feet to meters."""
print(f"Converting {feet} feet to meters.")
return feet / 3.28084
@tool("kilograms_to_pounds", description="Converts kilograms to pounds.")
def kilograms_to_pounds(kg: float) -> float:
"""Converts kilograms to pounds."""
print(f"Converting {kg} kilograms to pounds.")
return kg * 2.20462
@tool("meters_to_centimeters", description="Converts meters to centimeters.")
def meters_to_centimeters(meters: float) -> float:
"""Converts meters to centimeters."""
print(f"Converting {meters} meters to centimeters.")
return meters * 100
# エージェントの初期化
unit_conversion_agent = create_react_agent(
model="openai:gpt-4.1-mini",
tools=[meters_to_feet, celsius_to_fahrenheit, feet_to_meters, kilograms_to_pounds, meters_to_centimeters, centimeters_to_meters],
name="UnitConversionAgent",
prompt=(
"You are an Unit Conversion Agent.\n\n"
"INSTRUCTIONS:\n"
"- Your task is to perform unit conversions such as meters to feet, Celsius to Fahrenheit, and so on.\n"
"- Do NOT perform any arithmetic operations or handle non-unit conversion tasks.\n"
"- Respond ONLY with the numerical result of the conversion. Do NOT include any explanations or additional text.\n"
"- If the input is invalid or cannot be calculated, respond with 'Error: Invalid input'."
),
)
unit_conversion_agent のプロンプトの意図
-
機能範囲の限定
単位変換以外は処理しない -
出力形式の統一
ArithmeticAgent と同じルールで出力 -
具体例の提示
meters→feet などの代表例を提示し、モデルの判断基準を明確化する
# スーパーバイザーの初期化
supervisor = create_supervisor(
model=init_chat_model("openai:gpt-4.1-mini"),
agents=[
arithmetic_agent,
unit_conversion_agent,
],
prompt=(
"You are a supervisor managing two agents:\n"
"- ArithmeticAgent: Handles arithmetic operations.\n"
"- UnitConversionAgent: Handles unit conversion tasks.\n"
"Assign work to one agent at a time, do not call agents in parallel.\n"
"Do not do any work yourself.\n"
"Ensure that all calculations, including additions, are performed using the ArithmeticAgent.\n"
"Do not perform any calculations yourself, even if the result seems simple.\n"
"If a task involves both unit conversion and arithmetic, first use the UnitConversionAgent for conversions,\n"
"then delegate all arithmetic operations to the ArithmeticAgent."
),
add_handoff_back_messages=True,
output_mode="full_history",
).compile()
Supervisor のプロンプトの意図
-
単一委任ルール
同時に複数呼び出しせず、一度に1エージェントのみ -
自己計算の禁止
必ず専門エージェントを経由 -
厳格なルーティング規則
変換→計算の順序を固定 -
履歴と引き継ぎの可視化
引き渡し情報を残し、デバッグや監査を容易に
実行例
Task: 3フィートと2メートルをセンチメートルで足す
このタスクは「計算」と「単位変換」の両方が必要です。
Supervisorエージェントが ArithmeticAgent と UnitConversionAgent を適切に呼び出してこの問題を解決します。
-
単位変換:
SupervisorはまずUnitConversionAgentを呼び出します。UnitConversionAgentは、フィートをメートルに、メートルをセンチメートルに変換して、センチメートルに単位を揃えます -
計算:
単位がメートルに揃ったところで、Supervisorは次にArithmeticAgentを呼び出し、2つの数値を足し合わせます -
結果の統合と報告:
全てのステップが完了すると、supervisorは最終結果を統合し、「291.44 centimeters」という最終回答をユーザーに提示します
このように Supervisor は、複雑なタスクをサブタスクに分解し、適切な順序で専門エージェントに処理を依頼する高度な調整能力を持っています。
Task: Add 3 feet and 2 meters in centimeters
Update from node supervisor:
================================= Tool Message =================================
Name: transfer_to_unitconversionagent
Successfully transferred to UnitConversionAgent
Converting 3.0 feet to meters.
Converting 2.0 meters to centimeters.
Converting 0.914399970739201 meters to centimeters.
Update from node UnitConversionAgent:
================================= Tool Message =================================
Name: transfer_back_to_supervisor
Successfully transferred back to supervisor
Update from node supervisor:
================================= Tool Message =================================
Name: transfer_to_arithmeticagent
Successfully transferred to ArithmeticAgent
Adding 91.44 and 200.0
Update from node ArithmeticAgent:
================================= Tool Message =================================
Name: transfer_back_to_supervisor
Successfully transferred back to supervisor
Update from node supervisor:
================================== Ai Message ==================================
Name: supervisor
3 feet plus 2 meters is equal to 291.44 centimeters.
Task: Calculate the total weight of 70kg and 90kg in pounds
このタスクは「2つの異なる重量を足し合わせ、その結果を特定の単位に変換する」という、複数の工程を実行します
-
単位変換:
supervisorは、まずUnitConversionAgentを呼び出します。ここでのUnitConversionAgentの役割は、2つの変換を実行し、結果(ポンドに変換された2つの数値)をsupervisorに返します- 70kgをポンドに変換する
- 90kgをポンドに変換する
-
計算:
Supervisorは、UnitConversionAgentからポンド単位の2つの数値を受け取ります。次に、これらの数値を合計するためにArithmeticAgentを呼び出します。ArithmeticAgentは、受け取った2つのポンド値を単純に足し算します -
最終報告
ArithmeticAgentから最終的な合計値を受け取ったsupervisorは、「The sum of 70 kg and 90 kg converted to pounds is approximately 352.74 pounds.」という形で、ユーザーに最終回答を報告します
Task: 70キログラムと90キログラムの合計をポンドに変換する
Update from node supervisor:
================================= Tool Message =================================
Name: transfer_to_arithmeticagent
Successfully transferred to ArithmeticAgent
Adding 70.0 and 90.0
Update from node ArithmeticAgent:
================================= Tool Message =================================
Name: transfer_back_to_supervisor
Successfully transferred back to supervisor
Update from node supervisor:
================================= Tool Message =================================
Name: transfer_to_unitconversionagent
Successfully transferred to UnitConversionAgent
Converting 160.0 kilograms to pounds.
Update from node UnitConversionAgent:
================================= Tool Message =================================
Name: transfer_back_to_supervisor
Successfully transferred back to supervisor
Update from node supervisor:
================================== Ai Message ==================================
Name: supervisor
The sum of 70 kg and 90 kg converted to pounds is approximately 352.74 pounds.
まとめ
Supervisor(as tool-calling)パターンは、複雑なタスクを複数の専門エージェントに分配し、順序や役割を明確にしながら処理できる強力な手法です。
今回の例では、
- ArithmeticAgent が四則演算のみを担当
- UnitConversionAgent が単位変換のみを担当
- Supervisor が両者を統括し、タスクの分解・順序決定・結果統合を実施
という明確な責務分離を行い、プロンプトで厳格にルール化することで、誤動作や役割の衝突を防いでいます。
この設計により、例えば「3フィートと2メートルをセンチメートルで足す」のような変換+計算が必要なタスクも、Supervisor が適切な順序で専門エージェントを呼び出すことで確実に処理できます。
ただし、マルチエージェント構成はプロンプト設計が不十分だとうまく動作しません。
各エージェントの役割や出力形式を明確に書き分け、Supervisor のルーティングルールも具体的に記述することが、安定して正しい結果を得るための鍵となります。
このパターンは、実務でのワークフロー自動化や複合処理パイプラインの構築にも応用できる、再利用性の高い設計方法だといえるでしょう。
参考文献
Multi-agent supervisor