OpenAI Agents SDK 0.3 : クイックスタート

OpenAI AgentKit は Agents SDK 上に構築されています。クイックスタートに進みます。
AgentKit は 10月6日に公開されましたが、Agents SDK は 3月12日にリリースされています。

OpenAI Agents SDK 0.3 : クイックスタート

作成 : クラスキャット・セールスインフォメーション
作成日時 : 10/14/2025
バージョン : v0.3.3

* 本記事は以下のページを参考にしています :

* サンプルコードの動作確認はしておりますが、必要な場合には適宜、追加改変しています。
* ご自由にリンクを張って頂いてかまいませんが、sales-info@classcat.com までご一報いただけると嬉しいです。

 

 

OpenAI Agents SDK 0.3 : クイックスタート

プロジェクトと仮想環境の作成

これは一度だけ行う必要があります。

mkdir my_project
cd my_project
python -m venv .venv

 
仮想環境をアクティブにする

新しい端末セッションを開始するたびにこれを実行します。

source .venv/bin/activate

 
Agents SDK のインストール

pip install openai-agents # or `uv add openai-agents`, etc

 
OpenAI API キーを設定する

持っていない場合は、これらの手順 に従って OpenAI API キーを作成してください。

export OPENAI_API_KEY=sk-...

 

最初のエージェントを作成する

エージェントは、指示 (instructions)、名前、そして (model_config のような) オプションの config で定義されます。

from agents import Agent

agent = Agent(
    name="数学の家庭教師",
    instructions="あなたは数学の問題の手助けを提供します。各ステップであなたの推論を説明し、例を含めてください。",
)

 

更に 2, 3 のエージェントを追加する

追加のエージェントが同じ方法で定義できます。handoff_descriptions はハンドオフ・ルーティングを決定するための追加のコンテキストを提供します。

from agents import Agent

history_tutor_agent = Agent(
    name="History Tutor",
    handoff_description="歴史に関する質問のための専門エージェント",
    instructions="あなたは歴史に関する問い合わせを支援します。重要な出来事と文脈を明確に説明してください。",
)

math_tutor_agent = Agent(
    name="Math Tutor",
    handoff_description="数学の質問のための専門エージェント",
    instructions="あなたは数学の問題を解く手助けをします。各ステップであなたの推論を説明し、例を含めてください。",
)

 

ハンドオフを定義する

各エージェントで、エージェントがタスクを進める方法を決定するためにそこから選択できる、outgoing ハンドオフ・オプションのインベントリ (一覧) を定義できます。

triage_agent = Agent(
    name="Triage Agent",
    instructions="あなたはユーザーの宿題の質問に基づいて、どのエージェントを使用するかを決定します。",
    handoffs=[history_tutor_agent, math_tutor_agent]
)

 

エージェント・オーケストレーションを実行する

ワークフローが実行され、トリアージ・エージェントが 2 つの専門エージェント間で正しくルーティングするかを確認しましょう。

from agents import Runner

async def main():
    result = await Runner.run(triage_agent, "フランスの首都はどこですか?")
    print(result.final_output)

 

ガードレールを追加する

カスタム・ガードレールを定義して入力や出力上で実行できます。

ffrom agents import GuardrailFunctionOutput, Agent, Runner
from pydantic import BaseModel


class HomeworkOutput(BaseModel):
    is_homework: bool
    reasoning: str

guardrail_agent = Agent(
    name="Guardrail check",
    instructions="ユーザーが宿題について尋ねているかどうかを確認します。",
    output_type=HomeworkOutput,
)

async def homework_guardrail(ctx, agent, input_data):
    result = await Runner.run(guardrail_agent, input_data, context=ctx.context)
    final_output = result.final_output_as(HomeworkOutput)
    return GuardrailFunctionOutput(
        output_info=final_output,
        tripwire_triggered=not final_output.is_homework,
    )

 

Put it all together

すべてをまとめて、ハンドオフと入力ガードレールを使用して、ワークフロー全体を実行してみましょう。

from agents import Agent, InputGuardrail, GuardrailFunctionOutput, Runner
from agents.exceptions import InputGuardrailTripwireTriggered
from pydantic import BaseModel
import asyncio

class HomeworkOutput(BaseModel):
    is_homework: bool
    reasoning: str

guardrail_agent = Agent(
    name="Guardrail check",
    instructions="ユーザーが宿題について尋ねているかどうかを確認します。",
    output_type=HomeworkOutput,
)

math_tutor_agent = Agent(
    name="Math Tutor",
    handoff_description="数学の質問のための専門エージェント",
    instructions="あなたは数学の問題を解く手助けをします。各ステップであなたの推論を説明し、例を含めてください。",
)

history_tutor_agent = Agent(
    name="History Tutor",
    handoff_description="歴史に関する質問のための専門エージェント",
    instructions="あなたは歴史に関する問い合わせを支援します。重要な出来事と文脈を明確に説明してください。",
)

async def homework_guardrail(ctx, agent, input_data):
    result = await Runner.run(guardrail_agent, input_data, context=ctx.context)
    final_output = result.final_output_as(HomeworkOutput)
    return GuardrailFunctionOutput(
        output_info=final_output,
        tripwire_triggered=not final_output.is_homework,
    )

triage_agent = Agent(
    name="Triage Agent",
    instructions="あなたはユーザーの宿題の質問に基づいて、どのエージェントを使用するかを決定します。",
    handoffs=[history_tutor_agent, math_tutor_agent],
    input_guardrails=[
        InputGuardrail(guardrail_function=homework_guardrail),
    ],
)

async def main():
    # Example 1: History question
    try:
        result = await Runner.run(triage_agent, "アメリカ合衆国の最初の大統領は誰でしたか?")
        print(result.final_output)
    except InputGuardrailTripwireTriggered as e:
        print("Guardrail blocked this input:", e)

    # Example 2: General/philosophical question
    try:
        result = await Runner.run(triage_agent, "人生の意味は何ですか?")
        print(result.final_output)
    except InputGuardrailTripwireTriggered as e:
        print("Guardrail blocked this input:", e)

if __name__ == "__main__":
    asyncio.run(main())

出力例

アメリカ合衆国の最初の大統領は、ジョージ・ワシントン(George Washington)です。彼は1789年から1797年まで2期にわたり大統領を務めました。

文脈として、アメリカ独立戦争(1775年~1783年)の後、植民地がイギリスから独立してアメリカ合衆国が成立し、1788年にアメリカ合衆国憲法が発効しました。そして、最初の大統領選挙が行われ、ワシントンが満場一致で選出されました。彼は「アメリカの建国の父」の一人として知られており、そのリーダーシップと中立性が高く評価されています。
Guardrail blocked this input: Guardrail InputGuardrail triggered tripwire

 

トレースのレビュー

エージェント実行中に何が起こったかをレビューするには、OpenAI ダッシュボードの Trace ビューアー に進んで、エージェント実行のトレースをご覧ください。

 

以上