SysDesignAtlas

Real-Time & Voice AI

Speech pipelines, latency budgets measured against human conversational expectations, and interruption handling — where a 300 ms delay is the difference between natural and broken.

HLDHard11 min readaivoicerealtimestreaming
Confidence

#The budget humans enforce

Text chat tolerates a two-second wait. Speech does not. Human turn-taking has a gap distribution centred around 200 ms, and listeners register anything past roughly 500 ms as a hesitation — meaning something.

   PERCEIVED QUALITY OF RESPONSE DELAY

     < 300 ms     natural
     300-500 ms   slightly slow, acceptable
     500-800 ms   noticeably laggy; users start to interject
     800-1500 ms  broken; users repeat themselves
     > 1500 ms    unusable as conversation

   → The entire pipeline — endpointing, transcription,
     model, speech synthesis, network — must fit in
     roughly 500 ms.

That budget is the constraint that determines every architectural decision below.

#The pipeline

Six stages, and the abbreviations recur below. VAD (voice activity detection) decides whether the microphone is picking up speech or just room noise. WebRTC is the browser standard for carrying live audio. STT turns speech into text, TTS turns text back into speech, and a jitter buffer holds a fraction of a second of audio so that packets arriving unevenly still play smoothly.

Capture mic · VAD Transport WebRTC STT streaming ASR LLM TTFT is what matters TTS streaming synthesis Playback jitter buffer
Six stages, ~500 ms total. Every one must stream.

The key insight: only time-to-first-audio matters. Total generation time is irrelevant if playback starts quickly and never starves — never runs out of audio to play because the next chunk isn't ready yet. This makes TTFT the single metric to optimise on the model side — and it means a long prompt is far more costly here than in a text product, because TTFT scales with prompt length.

#Endpointing

Deciding when the user has finished speaking, and the largest single controllable term in the budget.

   SIMPLE VAD (energy threshold + silence timer)
     wait 700 ms of silence → assume they're done
     ✗ 700 ms of pure latency added to every turn
     ✗ cuts off mid-sentence pauses
     ✗ triggers on background noise

   SEMANTIC ENDPOINTING
     stream the partial transcript to a small model:
     "is this a complete utterance?"
     "What's the weather in—"     → incomplete, keep waiting
     "What's the weather in Delhi" → complete, go now
     ✓ 200-300 ms, and more accurate

   SPECULATIVE EXECUTION
     start the LLM on the partial transcript BEFORE the
     user finishes. If they continue, cancel and restart.
     ✓ can reach near-zero perceived latency
     ✗ wasted compute on cancellations — but compute is
       cheaper than the latency it buys

Speculative execution is the same idea as speculative decoding one level up: spend cheap resources on a guess to avoid paying an expensive wait.

#Transport

OptionLatencyFits
WebRTC50–150 msReal-time audio. Purpose-built: UDP, adaptive jitter buffer, packet loss concealment, echo cancellation
WebSocket100–300 msSimpler; TCP head-of-line blocking hurts under loss
HTTP streaming200 ms+Output only; no good for capture

WebRTC is the right answer for voice, and it's a real infrastructure commitment:

  • STUN/TURN servers for NAT traversal — helping two devices behind home or mobile routers find a path to each other, and relaying the audio when no direct path exists.
  • An SFU (selective forwarding unit) if you have multiple participants — a server that receives each person's stream and forwards it to the others.
  • Codec negotiation — the two ends agreeing on an audio format both support.

Teams routinely start with WebSockets because it's easier, and hit a quality ceiling on mobile networks that they cannot fix. The cause is TCP: when a packet is lost, TCP holds back every later packet until the lost one has been resent (head-of-line blocking). On a lossy link that means the audio freezes and then bursts. That's exactly the wrong behaviour for audio, where a late packet is worthless — you'd rather skip the missing 20 ms and keep playing.

The pieces WebRTC gives you that are easy to underestimate: acoustic echo cancellation (without it the assistant hears itself and interrupts itself), jitter buffering, and packet loss concealment (filling a missing slice of audio with a plausible guess so the gap isn't heard). Reimplementing these is not a weekend project.

#Interruption

Barge-in — the user speaking while the assistant is talking — is what separates a demo from a product, and it is genuinely hard.

   WHEN THE USER STARTS SPEAKING MID-RESPONSE:

     1. detect it, distinguishing speech from
        echo of your own audio  (needs AEC)
     2. stop TTS playback immediately
     3. cancel in-flight LLM generation
     4. cancel queued TTS synthesis
     5. TRUNCATE THE CONVERSATION HISTORY to what was
        actually PLAYED, not what was generated
     6. start listening

   Step 5 is the one everyone gets wrong.

#Cascaded versus end-to-end

Cascaded — STT → LLM → TTS • Each stage swappable and debuggable • Text transcript for logging, moderation, tools • Mature tooling; any LLM works • Loses tone, emotion, and emphasis at the text boundary • Latency accumulates across three models End-to-end speech-to-speech • Audio in, audio out — one model • Preserves prosody (rhythm and intonation), emotion, timing • Lower latency; no intermediate boundaries • Harder to moderate, log, and integrate with tools • Less control over exactly what is said
Speech-to-speech models remove pipeline latency and give up controllability.

Cascaded remains the practical default for anything needing tool use, moderation, or auditable transcripts — which is most business applications. End-to-end wins on naturalness and latency, and is advancing quickly. A common hybrid: end-to-end for the conversational path, with a parallel STT stream feeding logging and moderation.

#Serving characteristics

Voice load differs from text load in ways that matter for capacity planning:

  • Sessions are long-lived and stateful. A ten-minute call holds resources throughout, so you provision for concurrent sessions, not requests per second.
  • Latency variance is the enemy, not average latency. A p99 of 900 ms with a p50 of 300 ms produces a conversation that intermittently feels broken — which is worse than one that's consistently slightly slow. Sacrifice mean latency for a tighter distribution.
  • Batching is constrained. Large batches raise TTFT, and TTFT is the whole product here. Voice traffic should run in a small-batch, latency-optimised lane, separate from bulk text work.
  • Multiple models per session. STT, LLM, and TTS each need capacity, and they scale on different signals — STT on audio minutes, LLM on tokens, TTS on output characters.

#Monitoring

MetricTarget
Time to first audio< 500 ms p95 — the headline number
Endpointing delay< 300 ms
Audio underruns≈ 0; playback starving is very audible
Interruption handling latency< 100 ms from detection to silence
Word error rateTracked by accent, language, and noise condition
Turn-taking anomaliesBoth talking, or long awkward gaps
Session drop rateNetwork and infrastructure health

Audio underruns deserve special attention. An underrun is playback running out of audio. If TTS can't keep ahead of playback, the audio has gaps — and a gap mid-word is far more jarring than a slightly later start. The mitigation is a small output buffer: hold 200 ms of synthesised audio before starting playback, trading a little initial latency for robustness against synthesis jitter. It's the same jitter buffer reasoning as any streaming media system.

#Check yourself

What latency budget does speech impose?Show answer
Human turn gaps centre near 200 ms; past ~500 ms it feels hesitant and past ~800 ms broken, so the whole pipeline must fit in roughly 500 ms.
Which metric matters most for voice, and why?Show answer
Time to first audio: streaming every stage and overlapping them takes a 3.5 s sequential pipeline to ~630 ms, with the rest streaming behind playback. On the model side that means TTFT.
What are the endpointing options?Show answer
A silence timer adds ~700 ms every turn and cuts off pauses; semantic endpointing (a small model judging completeness) takes 200–300 ms; speculative execution starts the LLM on partials and cancels if speech continues.
Why WebRTC rather than WebSockets for voice?Show answer
UDP avoids TCP head-of-line blocking where a late audio packet is useless, and WebRTC brings echo cancellation, adaptive jitter buffering and packet-loss concealment.
What must happen when a user interrupts?Show answer
Detect real speech (with echo cancellation), stop playback, cancel generation and queued TTS, truncate history to the audio actually played (with an "interrupted" marker), then listen.
Cascaded or speech-to-speech?Show answer
Cascaded STT → LLM → TTS is debuggable and gives transcripts for tools and moderation but loses tone and adds latency. Speech-to-speech keeps prosody with lower latency but is harder to control; hybrids run a parallel transcript.
How is voice capacity planned?Show answer
By concurrent sessions, with STT, LLM and TTS scaling on audio minutes, tokens and characters respectively, in a small-batch latency lane — optimising latency variance over the mean.
How are audio underruns prevented?Show answer
Hold a small output buffer (~200 ms) before playback starts, trading a little initial latency for robustness — a gap mid-word is far more jarring than a slightly later start.

#What to carry forward

  • Humans enforce a ~500 ms budget. Past 800 ms, conversation breaks down.
  • Stream and overlap every stage. Only time to first audio matters; the rest streams behind playback.
  • Endpointing is the largest controllable term. Semantic endpointing or speculative execution beats a silence timer.
  • WebRTC, not WebSockets — you need echo cancellation, jitter buffering, and loss concealment.
  • On interruption, truncate history to what was actually played, not what was generated.
  • Provision for concurrent sessions, run voice in a small-batch latency lane, and optimise for tight latency variance rather than low mean.
00:00
/ 11m