To add voice to an AI agent, put speech-to-text in front of it and text-to-speech behind it, or replace both with one speech-to-speech model. In TypeScript, VoiceAI SDK runs either setup as a session: session.converse hands your agent each finished sentence the user says and speaks whatever the agent returns. Your agent stays a function that takes text and returns text.
VoiceAI SDK is a developer preview. The examples are written against its planned 1.0 API, use the AI SDK's streamText for the model, and have not been run against live provider accounts. Where behavior depends on the provider, the post says so instead of giving numbers.
Two ways to add voice at a glance
| Pipeline | Speech-to-speech | |
|---|---|---|
| Provider argument | { stt: deepgram(), tts: elevenlabs({ voice }) } | openai() |
| Who writes the reply | Your agent, with any model and tools | The provider's model from instructions, or your handler |
| Provider connections | One per half, plus your model | One |
| Voice | Any voice the text-to-speech provider offers | The provider's built-in voices |
| Capabilities | Each flag from the provider that does that half | All from one provider |
| Keys | One per provider, plus your model's | OPENAI_API_KEY |
Both setups expose the same session methods, so the rest of this post works with either. Start with the one that fits, and switch later by changing the first argument to createSession.
Install and check your setup
npm install voiceai-sdk ai
npx voiceai-sdk doctordoctor checks your Node version and which provider keys it can see, such as DEEPGRAM_API_KEY and ELEVENLABS_API_KEY. Keys are read on the server only. The package is ESM only and needs Node 22 or later, or Bun. The examples use await using, which runs natively on Node 24 and Bun, and through npx tsx on Node 22.
Hand each sentence to your agent
session.converse(handler) is the turn loop. Each time the user stops talking, the session produces a final transcript and calls your handler with it and a ctx object. Whatever the handler returns, a string or an AsyncIterable<string>, is spoken.
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 the support line for a bike shop. Answer in one or two short spoken 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({ voice: process.env.VOICE_ID }),
});
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 the model to finish. converse passes the stream to speak, so speech starts once the first words arrive instead of after the whole reply. How soon that is depends on the provider; Streaming text-to-speech covers it in detail.
ctx.history holds the earlier turns as user and assistant messages, so the model sees the whole conversation without storage of your own. ctx.signal aborts when the user talks over the reply, which stops the model too. When the socket closes, converse resolves and await using closes the session.
Or use one speech-to-speech model
openai() connects to the OpenAI Realtime API, which hears audio and answers with audio in one connection. Call converse() without a handler and the model answers on its own, following instructions.
import { createSession } from "voiceai-sdk";
import { openai } from "voiceai-sdk/openai";
import { webSocketAudio } from "voiceai-sdk/ws";
// In the same open(ws) handler as above
await using session = await createSession(openai({ instructions: system }), {
sampleRate: 24000,
encoding: "pcm16",
});
session.connect(webSocketAudio(ws));
await session.converse();The OpenAI Realtime provider page lists sampleRate: 24000 for pcm16, so the client streams at that rate. Pass a handler to converse instead, and the SDK speaks your handler's text: you keep your own agent, tools and model while one connection does the listening and speaking. OpenAI Realtime API in TypeScript walks through both modes.
Let the agent hang up
A voice agent needs a way to end the call. voiceTools(session) from voiceai-sdk/ai returns four tools bound to the session: speak, listen, transcribe and end_call. Inside converse the reply is already spoken, so most agents only need end_call.
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: system + " When the user 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. If your agent is built on another framework, voiceai-sdk/mastra, voiceai-sdk/openai-agents and voiceai-sdk/mcp export the same voiceTools in that framework's tool format.
Write the prompt for the ear
A voice prompt differs from a chat prompt. The user can't scroll back, skim, or click a link.
- Ask for short answers. Long replies are hard to follow by ear, and users talk over them.
- Ask for plain sentences. Markdown, bullet points and URLs don't make sense read aloud.
- Say when to hang up. Tell the model to call
end_callonce the user is done, or the session stays open until a timeout. - Test the handler as a function. It takes text and returns text, so you can test it with
mock()and no microphone. See Testing voice agents without a microphone.
Common questions
Do I need a speech-to-speech model to build a voice agent? No. A pipeline of a streaming speech-to-text provider, your model and a streaming text-to-speech provider runs the same converse loop. A speech-to-speech provider has fewer moving parts. A pipeline lets you choose the model, the tools and the voice separately.
Can the agent run in the browser? The session can't. Provider adapters read API keys, and keys must stay on the server. The browser streams microphone audio to your server over a WebSocket, and webSocketAudio(ws) from voiceai-sdk/ws attaches it to the session.
What happens when the user talks over the agent? If the session supports interruptions, it stops speaking, emits interrupt, and aborts ctx.signal. The next turn starts from what the user just said. See Barge-in: letting callers interrupt a voice agent.
How fast will the agent answer? That depends on the provider, the model, the region and the network, and the SDK publishes no figures during the preview. Measure the time from the final transcript to the speech.start event in your own setup.
Next steps
- Give your agent a voice: the full guide, from the first turn to hanging up.
- Choose a provider: which provider or pipeline fits your agent.
- Agent tools: the inputs and outputs of
speak,listen,transcribeandend_call.
More guides on the VoiceAI SDK blog, or start with the quickstart.