OpenAI Agents SDK TypeScript
import { Agent, run } from '@openai/agents';
const agent = new Agent({ name: 'Assistant', instructions: 'You are a helpful assistant.',});
const result = await run( agent, 'Write a haiku about recursion in programming.',);
console.log(result.finalOutput);import { RealtimeAgent, RealtimeSession } from '@openai/agents/realtime';
const agent = new RealtimeAgent({ name: 'Assistant', instructions: 'You are a helpful assistant.',});
// Automatically connects your microphone and audio output in the browser via WebRTC.const session = new RealtimeSession(agent);await session.connect({ apiKey: '<client-api-key>',});OpenAI Agents SDK for TypeScript は、 最小限の抽象化で軽量かつ使いやすいパッケージとして、エージェント型の AI アプリを構築できるようにします。これは、以前の エージェント向け実験である Swarm の本番運用向けアップグレード版で、Python でも利用可能 です。Agents SDK はごく少数の基本コンポーネントで構成されています:
- エージェント: instructions とツールを備えた LLM
- ハンドオフ: 特定のタスクを他のエージェントに委任できる仕組み
- ガードレール: エージェントへの入力を検証できる仕組み
TypeScript と組み合わせることで、これらの基本コンポーネントはツールとエージェント間の複雑な関係を表現でき、急な学習コストなしに 実運用アプリケーションを構築できます。さらに、この SDK には組み込みの トレーシング があり、エージェントのフローを可視化・デバッグし、評価やアプリ向けモデルのファインチューニングまで行えます。
Agents SDK を使う理由
Section titled “Agents SDK を使う理由”この SDK は 2 つの設計原則に基づいています:
- 使う価値があるだけの機能を備えつつ、すぐ学べるよう基本コンポーネントは少数に抑える
- そのままでも十分に動作しつつ、挙動を正確にカスタマイズできる
主な機能は次のとおりです:
- エージェントループ: ツールの呼び出し、結果の LLM への送信、LLM が完了するまでのループを内蔵
- TypeScript ファースト: 新しい抽象を学ぶ必要なく、言語機能でエージェントをオーケストレーション・連鎖
- ハンドオフ: 複数のエージェント間での調整と委任を可能にする強力な機能
- ガードレール: チェックが失敗したら早期に中断しつつ、エージェントと並行で入力検証を実行
- 関数ツール: 任意の TypeScript 関数をツール化し、スキーマ自動生成と Zod ベースの検証を提供
- トレーシング: ワークフローの可視化・デバッグ・監視に加え、OpenAI の評価、ファインチューニング、蒸留ツール群を活用可能
- リアルタイムエージェント: 自動割り込み検知、コンテキスト管理、ガードレールなどを備えた強力な音声エージェントを構築
インストール
Section titled “インストール”npm install @openai/agents zod@3Hello world のコード例
Section titled “Hello world のコード例”import { Agent, run } from '@openai/agents';
const agent = new Agent({ name: 'Assistant', instructions: 'You are a helpful assistant',});
const result = await run( agent, 'Write a haiku about recursion in programming.',);console.log(result.finalOutput);
// Code within the code,// Functions calling themselves,// Infinite loop's dance.(実行する場合は、OPENAI_API_KEY 環境変数を設定してください)
export OPENAI_API_KEY=sk-...