Streaming Voice Agent Architecture with Barge-In Support
System DesignModel answerLast updated
Design the backend for a conversational voice agent that listens, reasons and speaks in near real time, serves thousands of simultaneous callers, and handles users cutting in mid-reply.
Build a Voice Agent Backend
You are asked to design the infrastructure for a spoken conversational assistant. A user talks into a browser, phone app or telephone line. The assistant must understand the speech, decide on a reply with a language model, and speak that reply back, all fast enough that the exchange feels like a natural conversation (roughly half a second from the moment the user stops talking to the moment they hear the assistant).
What the system must do
- Detect speech boundaries. Work out when the user begins and finishes speaking.
- Transcribe. Convert the incoming audio stream to text as it arrives.
- Decide and respond. Interpret the request and produce a reply using a language model.
- Speak. Turn the reply text into audio and play it to the user.
- Handle interruptions. If the user starts talking while the assistant is still speaking, playback must stop right away and the system should react sensibly.
Qualities it must have
- Latency: responses must feel conversational.
- Scale: thousands of simultaneous sessions.
- Memory: the assistant keeps context across turns.
Assumptions
- A streaming language model API already exists and can be called as a black box.
- Your job is the infrastructure that connects the speech components, the model and the clients.
What to cover in your answer
- A high-level architecture, described in words (components and how they connect).
- How data moves through the system, including how audio buffering works.
- Your approach to cutting latency across the speech recognition and speech synthesis path.
- How the architecture handles a user interrupting ("barge-in").
State any assumptions about scale, latency targets and transport that you make along the way.
Hints
Hint 1
Define latency precisely (end of user speech to first audible audio), then split the target into a per-stage budget and check whether the stages add up. If they do not, you need overlap between stages.
Hint 2
Make every stage streaming and connected by long-lived connections, and consider starting speech synthesis on the first clause of the model output rather than the full reply.
Hint 3
Treat interruption as cancellation that must travel through every stage, fastest at the client. Think about discarding stale audio, and about recording only what the user actually heard in the conversation history.
Model answer
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Framing
This is a streaming-systems problem under a hard latency budget, more than a modelling problem. A strong answer (1) defines and budgets latency, (2) makes every stage streaming, (3) treats interruption as cancellation propagation, and (4) is explicit about where state lives.
1. Requirements and assumptions
- Latency definition: time from the user's last spoken word to the first audio the user hears. Target p50 under 500 ms, p95 under 800 ms.
- Scale: assume about 10k concurrent sessions at peak, average call about 5 minutes.
- Transports: WebRTC or WebSocket for browser/mobile; a media gateway for telephony (SIP/PSTN).
- Bandwidth: raw 16 kHz 16-bit mono is about 256 kbps per direction; Opus at about 24 kbps is roughly 10x smaller. 10k sessions is about 240 Mbps each way, which is manageable.
- Compute: a GPU can serve very roughly 50-100 streaming STT or TTS sessions, so expect on the order of 100-200 GPUs per model type at peak. TTS is typically the main cost driver.
2. Latency budget
| Stage | Approx. budget |
|---|---|
| Endpointing (silence threshold) | ~200 ms |
| STT finalization | 50-100 ms |
| LLM time to first token | 150-250 ms |
| TTS time to first audio chunk | 80-120 ms |
| Network + jitter buffer | ~50 ms |
These add up to roughly 600 ms, already over target. Fast components alone are not enough; the design needs overlap (speculation and pipelining). Saying this explicitly is the key insight.
3. Architecture
Client (mic, echo cancellation, local VAD, jitter buffer, player)
| full-duplex WebSocket/WebRTC: Opus frames + JSON control
v
Edge gateway (TLS, auth, sticky routing by session_id)
v
Session orchestrator (one actor per session, in-memory state)
|-- streaming STT (gRPC bidirectional) + turn detector
|-- LLM API (token streaming) + tools/RAG
|-- streaming TTS (gRPC bidirectional)
'-- Redis snapshot -> Kafka -> Postgres/S3 (transcripts, audio, analytics)
The client connection carries binary 20 ms Opus frames in both directions plus JSON control events such as speech_start, speech_end, interrupt, and playback_cursor{mark_id, ms}.
Session record: id, user_id, state (LISTENING / THINKING / SPEAKING), turns[{role, text, played_ms, ts}], current_response_id, tool_state.
4. Data flow and buffering
- Capture. The client sends 20 ms frames continuously, with echo cancellation on so the assistant's own voice does not trigger VAD.
- Ring buffer. The orchestrator keeps about 300 ms of recent audio. When speech starts, this pre-roll is flushed to STT so the first syllable is not clipped.
- Streaming STT. Emits partial hypotheses about every 100 ms and a final one at the endpoint.
- Turn detection. Combine an acoustic silence threshold (200-500 ms) with a semantic end-of-turn signal from the partial transcript. "My account number is..." deserves a longer wait than "Cancel my order."
- LLM. At end of turn, send context plus the final transcript and stream tokens back.
- Chunking. Cut the token stream at clause boundaries (or after about 8-15 tokens for the first chunk) and feed each chunk to streaming TTS. Later chunks can be longer for better prosody.
- Playback. TTS audio goes to the client tagged with mark IDs, passes through a 40-80 ms jitter buffer, and the client reports playback progress.
5. Reducing latency
- Warm, persistent connections: long-lived WebSocket and gRPC streams, no per-turn handshakes or model loads.
- Speculative LLM calls: start generation when the partial transcript stabilizes and silence begins; cancel if the user resumes. Saves around 200 ms at the cost of some wasted LLM calls.
- Small first TTS chunk: synthesize the first clause immediately and run TTS concurrently with generation.
- Prompt/prefix caching: keep the system prompt and history prefix stable so the provider's cache hits and TTFT drops.
- Filler audio: for slow tool calls, play a cached "let me check that" clip.
- Co-location: STT, TTS, orchestrator and LLM endpoint in the same region and zone; route users to the nearest region. Each cross-region hop costs 50-100 ms.
- Pre-synthesized phrases: cache greetings and common confirmations.
6. Barge-in
Model it as cancellation that propagates through every stage, starting at the edge.
- Client (under 50 ms): local VAD sees speech while the state is SPEAKING; the client fades out and clears its playback buffer without waiting for the server.
- Orchestrator: on speech start during SPEAKING, cancel the LLM stream (stop paying for tokens), cancel the TTS stream and drop queued chunks, and increment
current_response_idso late chunks carrying the old ID are discarded. This avoids race conditions. - Truncate history to what was heard: use the playback cursor to store only the played portion, e.g. "Your balance is $4 [interrupted]". Otherwise the model believes it said things the user never heard.
- False barge-in: coughs and backchannels ("uh-huh") should not kill the reply. Duck the volume immediately, confirm the interruption only after about 200-300 ms of speech or a non-backchannel partial, and restore volume if it is not confirmed. This tunes responsiveness against robustness.
7. State and reliability
- Each session is an actor pinned to one orchestrator pod via consistent hashing on
session_id; context lives in memory, so there are no database reads on the hot path. - After each turn, asynchronously snapshot to Redis so a reconnect or pod failure can resume on another pod within about a second.
- Stream transcripts and audio through Kafka to durable storage for analytics and compliance, off the critical path.
- Autoscale STT/TTS pools on concurrent streams, and apply admission control and graceful degradation (smaller models, shorter replies) under overload.
- Monitor per-stage latency percentiles, interruption rate, false-barge-in rate and word error rate.
Common mistakes
- Summing component latencies and declaring success without pipelining or speculation.
- Waiting for the full LLM reply before starting TTS.
- Handling barge-in only on the server, adding a network round trip to the stop.
- Leaving the interrupted reply in history in full.
- Not discarding stale audio chunks after cancellation.
- Using a fixed silence threshold for every utterance.
- Storing session state in a remote database on every frame.