OpenAI Agents
Give an agent from the OpenAI Agents SDK speak, listen, transcribe and end_call tools with voiceTools from voiceai-sdk/openai-agents.
The OpenAI Agents SDK takes an array of tools on each Agent. voiceTools(session) from voiceai-sdk/openai-agents returns that array for one session. run() then loops through model calls and tool calls until the agent is done.
Install
npm install voiceai-sdk @openai/agents zodpnpm add voiceai-sdk @openai/agents zodyarn add voiceai-sdk @openai/agents zodbun add voiceai-sdk @openai/agents zodThe voiceai-sdk/openai-agents subpath ships inside voiceai-sdk. @openai/agents provides Agent and run, and zod defines the parameters of any tools you add yourself.
Quickstart
This server uses a pipeline, Deepgram to hear and ElevenLabs to speak, and lets the agent run the call.
import { Agent, run } from "@openai/agents";
import { createSession } from "voiceai-sdk";
import { deepgram } from "voiceai-sdk/deepgram";
import { elevenlabs } from "voiceai-sdk/elevenlabs";
import { voiceTools } from "voiceai-sdk/openai-agents";
import { webSocketAudio } from "voiceai-sdk/ws";
Bun.serve({
port: 3000,
fetch: (req, server) => (server.upgrade(req) ? undefined : new Response("upgrade required", { status: 426 })),
websocket: {
async open(ws) {
await using session = await createSession({ stt: deepgram(), tts: elevenlabs() });
session.connect(webSocketAudio(ws));
const agent = new Agent({
name: "Support",
instructions:
"You answer support calls. Use speak to talk and listen to hear the caller. Call end_call when they are done.",
tools: voiceTools(session),
});
await run(agent, "A caller just connected. Greet them.", { maxTurns: 50 });
},
},
});maxTurns caps how many times one run calls the model. Every exchange with the caller takes at least one, so raise it above the default for a conversation.
How a run goes
- The agent calls
speakwith a greeting. The session says it into the transport. - The agent calls
listen. The tool returns the caller's next final transcript. - The agent answers with
speakand callslistenagain, as many times as the conversation needs. - When the caller is done, the agent calls
end_call. The session closes, the agent gives its final output, andrun()resolves.
If the caller disconnects first, the session closes, and a voice tool called after that fails with code closed. await using still closes the session when the handler ends, so nothing is left open either way.
Add your own tools
voiceTools(session) returns a plain array. Put your own tools next to it:
import { Agent, tool } from "@openai/agents";
import { z } from "zod";
import { voiceTools } from "voiceai-sdk/openai-agents";
const orderStatus = tool({
name: "order_status",
description: "Look up the status of an order by its number.",
parameters: z.object({ orderId: z.string() }),
execute: async ({ orderId }) => `Order ${orderId} shipped yesterday.`,
});
// Inside the open handler, after session.connect(...)
const agent = new Agent({
name: "Support",
instructions: "You answer support calls. Use order_status when the caller asks about an order.",
tools: [...voiceTools(session), orderStatus],
});The agent is built per session because its voice tools belong to one caller. Build it inside the connection handler, not at module scope.
Where keys stay
| Variable | Read by |
|---|---|
OPENAI_API_KEY | @openai/agents, for the model. Also openai() if you use OpenAI Realtime for voice. |
DEEPGRAM_API_KEY | deepgram(), for speech-to-text |
ELEVENLABS_API_KEY | elevenlabs(), for text-to-speech |
All three are read on the server. The browser only streams audio over the WebSocket. If you switch the session to openai(), one OPENAI_API_KEY covers both the agent and the voice.
Next steps
- Give your agent a voice: the full walkthrough, from the first turn to hanging up.
- Interruptions and barge-in: what happens when the caller talks over the agent.
- Agent tools: each tool's input and output.