Latency and audio formats
Choose a sample rate and encoding, stream speech in chunks, and measure where the time goes in each turn.
A voice agent feels slow when the caller waits in silence. Most of that wait is the time from the end of their sentence to the first sound of the reply. This page covers the settings that affect it and how to measure it in your own setup.
Pick a sample rate and encoding
sampleRate and encoding describe the audio the session reads and writes. Set them to match what your transport carries.
| Option | Value | Typical use |
|---|---|---|
sampleRate | 8000 | Phone calls, such as Twilio Media Streams |
sampleRate | 16000 | Speech from microphones and browsers |
sampleRate | 24000, 48000 | Higher-fidelity output, such as audio played in an app |
encoding | "pcm16" | Uncompressed 16-bit samples, the simplest to process |
encoding | "mulaw" | Phone audio. Twilio sends and expects 8 kHz mu-law |
encoding | "opus" | Compressed audio, for links where bandwidth matters |
import { createSession } from "voiceai-sdk";
import { cartesia } from "voiceai-sdk/cartesia";
import { deepgram } from "voiceai-sdk/deepgram";
await using session = await createSession(
{ stt: deepgram(), tts: cartesia() },
{ sampleRate: 16000, encoding: "pcm16" },
);If a provider can't work in the format you ask for, createSession throws VoiceError with code unsupported. Which formats each provider accepts depends on the provider; see Capabilities.
Stream speech in chunks
Awaiting speak waits for the whole clip. Iterating it gives you { data, ms } chunks as they arrive: data is audio bytes in the session's format, and ms is how much audio the chunk holds. Start playback with the first chunk instead of the last.
const speech = session.speak("Your order shipped this morning and should arrive on Thursday.");
for await (const chunk of speech) {
player.send(chunk.data); // a WebSocket to whatever plays the audio
console.log(`sent ${chunk.ms} ms of audio`);
}Whether chunks arrive while the provider is still synthesizing depends on the provider; check session.capabilities.streamingTts. When the session is connected to a transport, say and converse send the audio for you.
Start speaking before the reply is done
speak also accepts an AsyncIterable<string>. It sends text to the TTS provider as it arrives, so the first audio can come back while the model is still writing.
import { streamText } from "ai";
const result = streamText({ model: "openai/gpt-5", prompt: "Explain our return policy in two sentences." });
for await (const chunk of session.speak(result.textStream)) {
player.send(chunk.data);
}converse does the same when your handler returns a stream. A handler that awaits the full text and returns a string makes the caller wait for the whole reply before hearing any of it.
Measure where the time goes
Your handler runs when the final transcript is ready, and speech.start fires when the reply starts playing. The time between them is your part of the wait.
import { streamText } from "ai";
let heardAt = 0;
session.on("speech.start", () => {
if (heardAt) console.log(`first audio ${Math.round(performance.now() - heardAt)} ms after the transcript`);
});
await session.converse((text, ctx) => {
heardAt = performance.now();
const result = streamText({
model: "openai/gpt-5",
messages: [...ctx.history, { role: "user", content: text }],
abortSignal: ctx.signal,
});
return result.textStream;
});That interval covers:
- Your handler, including any lookups it awaits before calling the model.
- The model's time to its first words.
- The TTS provider's time to its first audio.
It doesn't cover the vad.silenceMs wait before the transcript is final, or the network between your server and the listener. Add more performance.now() marks inside the handler to split it further.
What to change first
- Return a stream, not a string, from your
conversehandler. - Don't await slow work before the model call if the model can start without it.
- Ask for a short first sentence. Speech can only start once the model has written something to say.
- Tune
vad.silenceMs. Lower it if replies start late; raise it if the agent cuts callers off. See Interruptions and barge-in. - Compare providers on your own audio. Response times depend on the provider and the region. See Benchmarks.
Next steps
- Audio streaming: how audio moves through a session.
- Capabilities: which formats and streaming modes each provider supports.