コンテンツにスキップ

クイックスタート

  1. プロジェクトを作成し、 npm を初期化します。これは一度だけ行えば十分です。

    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 の設定 を参照してください。

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

Create an agent
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.',
});

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

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

Run an agent
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);

2 回目のターンでは、 result.historyrun() に戻して渡すか、 セッション をアタッチするか、 conversationId / previousResponseId を使って OpenAI サーバー管理の状態を再利用できます。エージェントの実行 ガイドでは、これらのアプローチを比較しています。

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

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

エージェントには、情報を調べたりアクションを実行したりするためのツールを与えることができます。

Add a tool
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],
});

追加のエージェントも同様に定義でき、問題をより小さな部分に分解し、対象のタスクにより集中したエージェントにできます。また、エージェント上でモデルを定義することで、問題ごとに異なるモデルを使うことも可能です。

Create specialist agents
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 を定義できます。これにより、エージェントは会話を次のエージェントに引き継げるようになります。これは実行の過程で自動的に行われます。

Define 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 “エージェントオーケストレーションの実行”

runner は、個々のエージェントの実行、あらゆるハンドオフ、およびあらゆるツール呼び出しを処理します。

Run agent orchestration
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 を使用できます。

Quickstart
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 の Trace viewer にアクセスしてください。

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