Give your agent a voice
Connect a language model to a voice session so it hears the caller, answers out loud, and can end the call.
A voice session turns the caller's speech into text and your text into speech. Put a model between the two and you have a voice agent. This guide uses the AI SDK's streamText for the model, session.converse for the turn loop, and voiceTools so the model can act on the call.
Install
npm install voiceai-sdk aipnpm add voiceai-sdk aiyarn add voiceai-sdk aibun add voiceai-sdk aistreamText comes from the ai package, not from VoiceAI SDK. Set up its model the way the AI SDK documents. The examples use the model string "openai/gpt-5", but any model that streams text works.
How a turn works
- The session listens until the caller stops talking, then produces a final transcript.
session.conversecalls your handler with that text and actxobject.- Your handler starts the model and returns its text stream, an
AsyncIterable<string>. - The session passes the stream to
speak, so it starts talking as soon as the first words arrive. - If the caller talks over the reply, the session stops speaking and aborts
ctx.signal.
ctx holds three things: signal, which aborts when the turn is interrupted, history, the turns so far, and session, the session itself.
Stream the reply into speech
This server takes audio over a WebSocket, transcribes it with Deepgram, and speaks with ElevenLabs. The model sits in the handler.
import { streamText } from "ai";
import { createSession } from "voiceai-sdk";
import { deepgram } from "voiceai-sdk/deepgram";
import { elevenlabs } from "voiceai-sdk/elevenlabs";
import { webSocketAudio } from "voiceai-sdk/ws";
const system = "You are a helpful voice assistant. Answer in one or two short sentences.";
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));
await session.converse((text, ctx) => {
const result = streamText({
model: "openai/gpt-5",
system,
messages: [...ctx.history, { role: "user", content: text }],
abortSignal: ctx.signal,
});
return result.textStream;
});
},
},
});The handler returns result.textStream without waiting for it to finish. converse hands the stream to speak, which sends text on to the TTS provider as it arrives. The caller can hear the first sentence while the model is still writing the rest. How soon audio starts depends on the provider; see Latency and audio formats.
ctx.history holds the earlier turns as user and assistant messages, so the model sees the whole conversation. Passing ctx.signal as abortSignal stops the model when the caller interrupts.
Give the model voice tools
voiceTools(session) from voiceai-sdk/ai returns four tools bound to the session, in the AI SDK's tool format:
| Tool | What the model can do with it |
|---|---|
speak | Say something outside the normal reply, such as "One moment" before a slow lookup |
listen | Wait for the caller's next words and get them as text |
transcribe | Turn a piece of audio into text |
end_call | Hang up. The session closes and converse resolves |
converse already speaks whatever the model writes, so most agents only need end_call. Pass all four with tools: voiceTools(session), or pick the ones you need:
import { streamText } from "ai";
import { voiceTools } from "voiceai-sdk/ai";
// Inside open(ws), after session.connect(...)
const { end_call } = voiceTools(session);
await session.converse((text, ctx) => {
const result = streamText({
model: "openai/gpt-5",
system: "You take table bookings for Luigi's. When the caller has what they need, say goodbye and call end_call.",
messages: [...ctx.history, { role: "user", content: text }],
tools: { end_call },
abortSignal: ctx.signal,
});
return result.textStream;
});When the model calls end_call, the session closes and converse resolves. With await using, the scope then ends and nothing is left open. See Agent tools for each tool's input and output.
Write for the ear
A voice agent's prompt differs from a chat prompt. The caller can't scroll back or skim.
- Ask for short answers. Long replies are hard to follow by ear, and callers talk over them.
- Ask for plain sentences. Markdown, bullet points and URLs don't make sense when they are read aloud.
- Say when to hang up. Tell the model to call
end_callonce the caller is done, or the call stays open until a timeout. - Keep the system prompt fixed. The conversation itself belongs in
ctx.history, which the session keeps for you.
Next steps
- Build a phone agent: put this agent on a Twilio number.
- Interruptions and barge-in: what happens when the caller talks over a reply.
- AI SDK: the AI SDK integration in detail.