OpenAI Agents SDK Skill
Complete skill for building AI applications with OpenAI Agents SDK (JavaScript/TypeScript), covering text agents, realtime voice agents, multi-agent workflows, and production deployment patterns.
Quick Start
Installation
bun add @openai/agents zod@3
bun add @openai/agents-realtime # For voice agents
Set environment variable:
export OPENAI_API_KEY="your-api-key"
Basic Text Agent
import { Agent, run, tool } from '@openai/agents';
import { z } from 'zod';
const agent = new Agent({
name: 'Assistant',
instructions: 'You are helpful.',
tools: [tool({
name: 'get_weather',
parameters: z.object({ city: z.string() }),
execute: async ({ city }) => `Weather in ${city}: sunny`,
})],
model: 'gpt-4o-mini',
});
const result = await run(agent, 'What is the weather in SF?');
Voice Agent & Multi-Agent
// Voice agent
const voiceAgent = new RealtimeAgent({
voice: 'alloy',
model: 'gpt-4o-realtime-preview',
});
// Browser session
const session = new RealtimeSession(voiceAgent, {
apiKey: sessionApiKey, // From backend!
transport: 'webrtc',
});
// Multi-agent handoffs
const triageAgent = Agent.create({
handoffs: [billingAgent, techAgent],
});
17 Templates: templates/ directory has production-ready examples for all patterns.
Top 3 Critical Errors
1. Zod Schema Type Errors
Error: Type errors with tool parameters even when structurally compatible.
Workaround: Define schemas inline.
// ❌ Can cause type errors
parameters: mySchema
// ✅ Works reliably
parameters: z.object({ field: z.string() })
Source: GitHub #188
2. MCP Tracing Errors
Error: "No existing trace found" with MCP servers.
Workaround:
import { initializeTracing } from '@openai/agents/tracing';
await initializeTracing();
Source: GitHub #580
3. MaxTurnsExceededError
Error: Agent loops infinitely.
Solution: Increase maxTurns or improve instructions:
const result = await run(agent, input, {
maxTurns: 20,
});
// Or improve instructions
instructions: `After using tools, provide a final answer.
Do not loop endlessly.`
All 9 Errors: Load references/common-errors.md for complete error catalog with workarounds.
When to Load References
Load reference files when working on specific aspects of agent development:
Agent Patterns (references/agent-patterns.md)
Load when:
- Designing multi-agent orchestration strategies
- Choosing between LLM-based vs code-based orchestration
- Implementing parallel agent execution
- Creating agents-as-tools patterns
- Need to understand when to use which orchestration pattern
Common Errors (references/common-errors.md)
Load when:
- Debugging agent issues beyond the top 3 errors above
- Implementing comprehensive error handling
- Encountering: GuardrailExecutionError, ToolCallError, Schema Mismatch, Ollama integration, webSearchTool failures, Agent Builder export bugs
- Building production error recovery patterns
Realtime Transports (references/realtime-transports.md)
Load when:
- Choosing between WebRTC vs WebSocket for voice agents
- Optimizing voice agent latency
- Debugging voice connection issues
- Understanding network/firewall requirements for voice
- Implementing custom audio sources/sinks
Cloudflare Integration (references/cloudflare-integration.md)
Load when:
- Deploying agents to Cloudflare Workers
- Understanding Workers limitations (CPU, memory, no voice)
- Implementing streaming in Workers
- Debugging Workers-specific issues
- Optimizing for Workers performance and costs
Official Links (references/official-links.md)
Load when:
- Need official documentation links
- Looking for examples or community resources
- Checking latest SDK versions
- Finding pricing information
- Need migration guides
Core Concepts Summary
Agents: LLMs equipped with instructions and tools.
Tools: Functions with Zod schemas that agents can call automatically.
Handoffs: Multi-agent delegation where agents route tasks to specialists.
Guardrails: Input/output validation for safety (content filtering, PII detection).
Structured Outputs: Type-safe responses using Zod schemas.
Streaming: Real-time event streaming for progressive responses.
Human-in-the-Loop: Require approval for specific tool executions (requiresApproval: true).
For detailed examples, see templates in templates/text-agents/ and templates/realtime-agents/.
Text Agents Quick Reference
// Basic
const result = await run(agent, 'Your question');
// Streaming
const stream = await run(agent, input, { stream: true });
// Structured output
const agent = new Agent({
outputType: z.object({ sentiment: z.enum([...]), confidence: z.number() }),
});
Templates: templates/text-agents/ (8 templates)
Realtime Voice Agents Quick Reference
const voiceAgent = new RealtimeAgent({
voice: 'alloy', // alloy, echo, fable, onyx, nova, shimmer
model: 'gpt-4o-realtime-preview',
});
const session = new RealtimeSession(voiceAgent, {
apiKey: sessionApiKey,
transport: 'webrtc', // or 'websocket'
});
Voice handoff constraints: Cannot change voice/model during handoff.
Templates: templates/realtime-agents/ (3 templates) | Details: references/realtime-transports.md
Framework Integration Quick Reference
Cloudflare Workers (Experimental)
export default {
async fetch(request: Request, env: Env) {
const { message } = await request.json();
process.env.OPENAI_API_KEY = env.OPENAI_API_KEY;
const agent = new Agent({
name: 'Assistant',
instructions: 'Be helpful and concise',
model: 'gpt-4o-mini',
});
const result = await run(agent, message, { maxTurns: 5 });
return new Response(JSON.stringify({
response: result.finalOutput,
tokens: result.usage.totalTokens,
}));
},
};
Limitations: No realtime voice, CPU time limits (30s max), memory constraints (128MB).
Templates: templates/cloudflare-workers/ (2 templates)
Details: Load references/cloudflare-integration.md for complete Workers guide.
Next.js App Router
// app/api/agent/route.ts
import { NextRequest, NextResponse } from 'next/server';
import { Agent, run } from '@openai/agents';
export async function POST(request: NextRequest) {
const { message } = await request.json();
const agent = new Agent({ /* ... */ });
const result = await run(agent, message);
return NextResponse.json({ response: result.finalOutput });
}
Templates: templates/nextjs/ (2 templates)
Guardrails & Human-in-the-Loop
// Input/output guardrails
const agent = new Agent({
inputGuardrails: [homeworkDetectorGuardrail],
outputGuardrails: [piiFilterGuardrail],
});
// Human approval
const tool = tool({
requiresApproval: true,
execute: async ({ amount }) => `Refunded $${amount}`,
});
// Handle approval loop
while (result.interruption?.type === 'tool_approval') {
result = (await promptUser(result.interruption))
? await result.state.approve(result.interruption)
: await result.state.reject(result.interruption);
}
Templates: templates/text-agents/agent-guardrails-*.ts, agent-human-approval.ts
Orchestration Patterns Summary
LLM-Based: Agent decides routing autonomously. Use for adaptive workflows.
Code-Based: Explicit control flow. Use for predictable, deterministic workflows.
Parallel: Run multiple agents concurrently. Use for independent tasks.
Agents as Tools: Wrap agents as tools for manager LLM. Use for specialist delegation.
Details: Load `references/agent-patter