Design a real-time voice agent
Voice agents are text agents with a brutal latency budget. In a text chat, a two second pause is fine. On a phone call, a two second pause makes the caller say "hello?" and talk over the bot. The design is about two things: shaving every possible millisecond off the loop from "user stops talking" to "bot starts talking," and handling the messy reality of human conversation, where people pause mid-sentence, say "uh huh," and interrupt.
Requirements
Functional
- Handle inbound phone calls and browser or app voice sessions.
- Understand speech, reason with tools (book appointment, check order), and reply with natural speech.
- Barge-in: when the caller interrupts, the bot stops speaking immediately and listens.
- Transfer to a human with context; record and transcribe calls with consent.
Non-functional
- Voice-to-voice latency (end of user speech to first bot audio) ideally under about 800 ms, and consistently under about 1.5 s.
- Robust to background noise, accents, and phone-quality 8 kHz audio.
- Graceful under load: no robotic stutter because a GPU is busy.
- Compliance: call recording consent, PII handling in transcripts.
Scale estimates (assumptions)
| Quantity | Assumption | Result |
|---|---|---|
| Peak concurrent calls | 5,000 | each is a long-lived stateful session |
| Average call length | 4 minutes | |
| User turns per call | 12 | about 250 LLM turns/s at peak |
| Audio in | 16 kHz mono PCM, about 32 KB/s | about 160 MB/s total ingest at peak |
| TTS output | 40% of call time is bot speech | 2,000 concurrent TTS streams |
The unit of scale is concurrent sessions, not requests per second. Each session pins state (audio buffers, conversation, a media connection) to one worker for minutes.
High-level architecture
Rendering diagram…
The media server handles WebRTC (and bridges SIP calls into it), jitter buffers and codecs. The agent worker runs the pipeline for a single conversation. Frameworks like LiveKit Agents and Pipecat implement this worker model, so in practice you build on one rather than writing audio plumbing yourself.
Request flow
Rendering diagram…
Components
| Component | Responsibility | Notes |
|---|---|---|
| Telephony provider | PSTN numbers, SIP trunking, DTMF | Twilio, Telnyx and others; adds network latency you cannot remove |
| Media server | WebRTC transport, echo cancellation, codecs | WebRTC beats raw WebSockets on lossy networks |
| Session dispatcher | Assign each new call to a worker with capacity | Sticky for the whole call; drain on deploy |
| VAD | Detect speech vs silence per frame | Silero VAD is a common default |
| Turn detector | Decide if a pause means the user is done | Semantic models (LiveKit turn detector, Pipecat Smart Turn) beat silence timers |
| Streaming STT | Partial and final transcripts | Deepgram, AssemblyAI, Speechmatics, cloud providers |
| LLM | Respond and call tools | Low TTFT matters more than raw intelligence |
| Streaming TTS | Text to audio, starting before full text exists | ElevenLabs, Cartesia and others; first-byte latency is key |
Data model
CREATE TABLE calls (
id UUID PRIMARY KEY,
channel TEXT, -- pstn | web | app
from_number TEXT, -- stored hashed or masked where required
agent_config TEXT, -- prompt, voice, tools version
started_at TIMESTAMPTZ,
ended_at TIMESTAMPTZ,
outcome TEXT, -- resolved | transferred | abandoned
consent BOOLEAN
);
CREATE TABLE turns (
call_id UUID,
seq INT,
speaker TEXT, -- user | agent
text TEXT,
spoken_text TEXT, -- for agent turns: what was actually played before barge-in
interrupted BOOLEAN,
eou_ms INT, -- end-of-utterance detection delay
llm_ttft_ms INT,
tts_ttfb_ms INT,
PRIMARY KEY (call_id, seq)
);
spoken_text matters: if the bot was interrupted halfway, the history must reflect only what the caller heard, or the bot will reference things it never actually said.
Deep dives
The latency budget
A cascaded pipeline (STT, LLM, TTS) adds up like this. These are ballpark targets, not vendor numbers:
| Stage | Target |
|---|---|
| Network and media in | 50 to 150 ms (more on PSTN) |
| End-of-turn detection | 200 to 400 ms after speech stops |
| STT final transcript | under 100 ms after end of turn (streaming) |
| LLM time to first sentence | 200 to 500 ms |
| TTS time to first audio | 100 to 200 ms |
| Network out | 50 to 150 ms |
Techniques that matter most: colocate workers, STT, LLM and TTS in the same region; stream everything; send TTS the first sentence as soon as it is complete rather than waiting for the full reply; keep prompts short and cached; and pick a model for low TTFT. Measure every stage per turn (the turns table above) because averages hide the slow tail that callers notice.
Turn detection
A pure silence timer is a bad trade: short timeouts cut people off mid-thought ("my account number is... uh..."), long ones feel sluggish. Modern stacks combine VAD with a small model that looks at the transcript (and sometimes audio) to judge whether the utterance is complete. "I want to book a table for" is clearly not done; "Yes, that works" is. Use a short wait when the model is confident the turn is complete and a longer one when it is not.
Barge-in
When the caller speaks during bot playback:
- VAD fires, and an interruption strategy decides whether it is a real interruption or a backchannel ("mm-hmm"). Pipecat, for example, supports minimum word counts and model-based interruption prediction for this.
- Stop TTS playback immediately and flush buffered audio on the client or media server.
- Cancel the in-flight LLM and TTS requests to save cost.
- Truncate the agent's last turn in history to what was actually played.
Echo cancellation is essential, or the bot hears itself and interrupts itself.
Cascaded vs speech-to-speech
Speech-to-speech models (such as OpenAI's Realtime API or Gemini Live) take audio in and produce audio out in one model. They can be lower latency and capture tone, but you get less control: harder to inspect and edit text before it is spoken, fewer voice choices, and tool calling and cost profiles differ. Cascaded pipelines let you mix best-in-class STT, LLM and TTS, inject guardrails on text, and swap vendors. Many teams run cascaded for regulated or tool-heavy flows and speech-to-speech for conversational ones. Frameworks like LiveKit Agents support both behind the same session abstraction.
Tools without dead air
Tool calls that take 2 seconds create silence. Options: have the model say a short filler first ("Let me check that for you"), play a subtle sound, or start the tool call speculatively on a partial transcript when intent is clear. Keep tool APIs fast, and set timeouts with spoken fallbacks.
Trade-offs and pitfalls
- Testing only on clean audio. Phone audio at 8 kHz with background noise performs very differently. Test on real call recordings.
- Long LLM replies. Voice answers must be short. Instruct the model to answer in one or two sentences and ask follow-ups.
- Speaking structured data. Numbers, dates, emails and addresses need normalization for TTS ("$1,250" to "twelve hundred fifty dollars") and confirmation by repeating back.
- Cold starts. Loading models or opening vendor connections at call start adds seconds. Pre-warm workers and connection pools.
- Autoscaling on CPU. Scale on active sessions per worker and stage latencies, and keep headroom since calls cannot be rebalanced mid-session.
- Compliance. Announce recording, honor opt-outs, and redact PII in stored transcripts.
Checklist
- Voice-to-voice latency budget per stage, measured per turn in production.
- Semantic turn detection on top of VAD, not a fixed silence timer.
- Barge-in stops audio, cancels generation and truncates history to what was heard.
- Echo cancellation and noise handling tested on real phone audio.
- Clear choice between cascaded and speech-to-speech, with reasons.
- Filler strategy for slow tools; short, spoken-style responses.
- Workers scale on concurrent sessions with pre-warmed connections.