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>',});TypeScript 用 OpenAI Agents SDK は、 抽象化を最小限に抑えた軽量で使いやすいパッケージで、エージェント型の AI アプリを構築できるようにします。これは以前のエージェント向け実験である Swarm の本番運用向けアップグレードであり、Python 版 もあります。Agents SDK にはごく少数の基本コンポーネントがあります:
- エージェント: instructions と tools を備えた LLMs
- ハンドオフ: 特定のタスクでエージェントが他のエージェントに委譲できる機能
- ガードレール: エージェントへの入力を検証できる機能
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-...