コンテンツにスキップ

クイックスタート

  1. プロジェクトを作成し、npm を初期化します。この作業が必要なのは最初の 1 回だけです。

    Terminal window
    mkdir my_project
    cd my_project
    npm init -y
  2. Agents SDK と Zod をインストールします。SDK は、ツールスキーマと structured outputs に Zod v4 を使用します。

    Terminal window
    npm install @openai/agents zod
  3. OpenAI API キーを設定します。キーがない場合は、こちらの手順に従って OpenAI API キーを作成してください。

    Terminal window
    export OPENAI_API_KEY=sk-...

    または、setDefaultOpenAIKey('<api key>') を呼び出してプログラムからキーを設定し、トレーシングには setTracingExportApiKey('<api key>') を使用できます。詳しくは、SDK の設定を参照してください。

はじめてのエージェントの作成

Section titled “はじめてのエージェントの作成”

エージェントは instructions と名前で定義します。

エージェントの作成
import { Agent } from '@openai/agents';
const agent = new Agent({
name: 'History Tutor',
instructions:
'You provide assistance with historical queries. Explain important events and context clearly.',
});

はじめてのエージェントの実行

Section titled “はじめてのエージェントの実行”

run メソッドを使用してエージェントを実行できます。開始するエージェントと渡す入力の両方を指定することで、実行を開始します。

実行すると、最終出力と、その実行中に行われたすべてのアクションを含む実行結果が返されます。

エージェントの実行
import { Agent, run } from '@openai/agents';
const agent = new Agent({
name: 'History Tutor',
instructions:
'You provide assistance with historical queries. Explain important events and context clearly.',
});
const result = await run(agent, 'When did sharks first appear?');
console.log(result.finalOutput);

次のターンでは、result.history を再び run() に渡すか、セッションを関連付けるか、conversationId / previousResponseId を使用して OpenAI のサーバー管理状態を再利用できます。エージェントの実行では、これらのアプローチを比較しています。

エージェントへのツールの追加

Section titled “エージェントへのツールの追加”

情報の検索やアクションの実行に使用できるツールをエージェントに追加できます。

ツールの追加
import { Agent, tool } from '@openai/agents';
import { z } from 'zod';
const historyFunFact = tool({
// The name of the tool will be used by the agent to tell what tool to use.
name: 'history_fun_fact',
// The description is used to describe when to use the tool by telling it what it does.
description: 'Give a fun fact about a historical event',
// This tool takes no parameters, so we provide an empty Zod object.
parameters: z.object({}),
execute: async () => {
// The output will be returned back to the agent to use.
return 'Sharks are older than trees.';
},
});
const agent = new Agent({
name: 'History Tutor',
instructions:
'You provide assistance with historical queries. Explain important events and context clearly.',
// Add the tool to the agent.
tools: [historyFunFact],
});

問題をより小さな部分に分割し、各エージェントを 1 つのタスクに集中させ、問題に応じて異なるモデルを使用できるように、専門エージェントを追加で定義します。

専門エージェントの作成
import { Agent } from '@openai/agents';
const historyTutorAgent = new Agent({
name: 'History Tutor',
instructions:
'You provide assistance with historical queries. Explain important events and context clearly.',
});
const mathTutorAgent = new Agent({
name: 'Math Tutor',
instructions:
'You provide help with math problems. Explain your reasoning at each step and include examples',
});

複数のエージェントをオーケストレーションするには、エージェントの handoffs を定義します。実行中にハンドオフが選択されると、ランナーは会話を対象のエージェントへ自動的に転送します。

ハンドオフの定義
import { Agent } from '@openai/agents';
const historyTutorAgent = new Agent({
name: 'History Tutor',
instructions:
'You provide assistance with historical queries. Explain important events and context clearly.',
});
const mathTutorAgent = new Agent({
name: 'Math Tutor',
instructions:
'You provide help with math problems. Explain your reasoning at each step and include examples',
});
// Use Agent.create() to keep handoff output types aligned.
const triageAgent = Agent.create({
name: 'Triage Agent',
instructions:
"You determine which agent to use based on the user's homework question",
handoffs: [historyTutorAgent, mathTutorAgent],
});

実行後、実行結果の lastAgent プロパティを確認すると、どのエージェントが最終応答を生成したかが分かります。

エージェントオーケストレーションの実行

Section titled “エージェントオーケストレーションの実行”

ランナーは、個々のエージェント、すべてのハンドオフ、すべてのツール呼び出しの実行を処理します。

エージェントオーケストレーションの実行
import { Agent, run } from '@openai/agents';
const historyTutorAgent = new Agent({
name: 'History Tutor',
instructions:
'You provide assistance with historical queries. Explain important events and context clearly.',
});
const mathTutorAgent = new Agent({
name: 'Math Tutor',
instructions:
'You provide help with math problems. Explain your reasoning at each step and include examples',
});
const triageAgent = Agent.create({
name: 'Triage Agent',
instructions:
"You determine which agent to use based on the user's homework question",
handoffs: [historyTutorAgent, mathTutorAgent],
});
async function main() {
const result = await run(triageAgent, 'What is the capital of France?');
console.log(result.finalOutput);
}
main().catch((err) => console.error(err));

すべてを 1 つの完全な例にまとめます。これを index.js ファイルに配置して実行してください。アプリがすでに TypeScript 用にセットアップされている場合は、代わりに index.ts を使用できます。

クイックスタート
import { Agent, run } from '@openai/agents';
const historyTutorAgent = new Agent({
name: 'History Tutor',
instructions:
'You provide assistance with historical queries. Explain important events and context clearly.',
});
const mathTutorAgent = new Agent({
name: 'Math Tutor',
instructions:
'You provide help with math problems. Explain your reasoning at each step and include examples',
});
const triageAgent = Agent.create({
name: 'Triage Agent',
instructions:
"You determine which agent to use based on the user's homework question",
handoffs: [historyTutorAgent, mathTutorAgent],
});
async function main() {
const result = await run(triageAgent, 'What is the capital of France?');
console.log(result.finalOutput);
}
main().catch((err) => console.error(err));

Agents SDK はトレースを自動的に生成します。これらのトレースを使用すると、エージェントがどのように動作しているか、どのツールを呼び出したか、どのエージェントがハンドオフを受けたかを確認できます。

エージェントの実行中に何が起きたかを確認するには、OpenAI Dashboard のトレースビューアーに移動してください。

より複雑なエージェントフローの構築方法を学びます。