Problem
Voice has been the fastest-moving AI UX category of 2025-2026. The technical ceiling dropped each quarter. OpenAI Realtime API, Gemini 2.5 Live, Cartesia Sonic-2, ElevenLabs Flash v3, LiveKit Agents 1.0, and Pipecat 0.0.70 all put sub-800ms first-audio-out within reach. The bar is not latency alone. It is the interaction feel: not cutting the user off, not getting cut off, recovering from a mid-sentence interruption, calling a tool mid-conversation without stalling the audio, surviving jittery mobile networks.
You cannot get there by stitching three REST calls. The architecture is pipelined streaming end to end. Build it and the failure modes become visible: a VAD tuned for phone audio firing on background TV, a turn-detector waiting for punctuation that never comes, a TTS that buffers 400ms before emitting. The capstone is to fix these one at a time under load and publish a latency-and-quality report.
Concept
The pipeline has five streaming stages: audio in (WebRTC from browser or PSTN), ASR (streaming partial transcripts from Deepgram Nova-3 or faster-whisper), turn detection (VAD plus a small turn-detector model that reads partial transcripts for completion cues), LLM (streaming tokens as soon as the turn is judged complete), TTS (streaming audio out within ~200ms of the first LLM token).
Three cross-cutting concerns. Barge-in: when the user starts speaking while the agent is speaking, the TTS cancels and the ASR picks up immediately. Tool use: mid-conversation function calls (weather, calendar) must run on a side channel without stalling the audio; the agent pre-fills an acknowledgement token ("one second...") if latency exceeds 300ms. Backpressure: under packet loss, partial transcripts are held, VAD raises the speech-gate threshold, and the agent avoids speaking over an unacknowledged message.
The measurement bar is quantitative. WER under 8% on the Hamming VAD benchmark at 15 dB SNR. First-audio-out p50 under 800ms on 100 measured calls. False-cutoff rate under 3%. MOS above 4.2 on TTS. 50 concurrent calls on a single g5.xlarge. These numbers are the deliverable.
Architecture
browser / Twilio PSTN
|
v
WebRTC / SIP edge
|
v
LiveKit Agents 1.0 (or Pipecat 0.0.70)
|
+----+--------------+--------------+-----------------+
| | | |
v v v v
ASR VAD v5 turn-detector side-channel
(Deepgram (Silero) (LiveKit) tools
Nova-3 / speech-gate completion score (weather,
Whisper-v3) per 20ms on partials calendar)
| | |
+--------+----------+--------------+
v
LLM (streaming)
GPT-4o-realtime / Gemini 2.5 Flash /
cascaded Claude Haiku 4.5
|
v
TTS streaming
Cartesia Sonic-2 / ElevenLabs Flash v3
|
v
audio back to caller
|
v
OpenTelemetry voice traces -> Langfuse
Stack
- Transport: LiveKit Agents 1.0 (WebRTC) plus Twilio PSTN gateway; Pipecat 0.0.70 as the alternate framework
- ASR: Deepgram Nova-3 (streaming, sub-300ms first partial) or faster-whisper Whisper-v3-turbo self-hosted
- VAD: Silero VAD v5 plus the LiveKit turn-detector (small transformer that reads partial transcripts)
- LLM: OpenAI GPT-4o-realtime for tight integration, Gemini 2.5 Flash Live, or cascaded Claude Haiku 4.5 (streaming completions, separate audio path)
- TTS: Cartesia Sonic-2 (lowest first-byte), ElevenLabs Flash v3, or open-source Orpheus for self-host
- Tools: FastMCP side-channel for weather/calendar/booking; agent pre-emits filler if tool takes >300ms
- Observability: OpenTelemetry voice spans, Langfuse voice traces with audio replay
- Deployment: single g5.xlarge (24GB VRAM) for self-hosted Whisper + Orpheus; hosted APIs for lowest latency
Interactive figure coming later.
Build It
-
WebRTC session. Stand up a LiveKit room and a web client that streams microphone audio. On the server, attach an agent worker that joins the room.
-
ASR streaming. Feed 20ms PCM frames to Deepgram Nova-3 (or faster-whisper on GPU). Subscribe to partial and final transcripts. Log per-partial latency.
-
VAD and turn detector. Run Silero VAD v5 on the frame stream. On speech-end event, fire the LiveKit turn-detector against the latest partial transcript. Only commit to "turn complete" when VAD says silence for 500ms and the turn-detector scores completion > 0.6.
-
LLM stream. On turn complete, start the LLM call with the running conversation plus the final transcript. Stream tokens out. At the first token, hand off to TTS.
-
TTS stream. Cartesia Sonic-2 streams audio chunks back. The first chunk must leave the server within 200ms of the first LLM token. Emit chunks to LiveKit room; client plays through WebRTC jitter buffer.
-
Barge-in. When VAD detects new user speech while TTS is playing, cancel the TTS stream immediately, drop the remaining LLM output, and re-arm the ASR. Publish a
tts_canceledspan. -
Tool side channel. Register weather and calendar as function-calling tools. When invoked, fire the call concurrently; if it does not resolve within 300ms, have the LLM emit "one second, let me check" as a filler; resume once the tool returns.
-
Eval harness. Record 100 calls. Compute WER (against a held-out transcript), false-cutoff rate (TTS cancelled while user was mid-sentence), first-audio-out p50, TTS MOS (human or NISQA), and a jitter-loss test (drop 3% of packets).
-
Load test. Drive 50 concurrent calls on a single g5.xlarge with a synthetic caller. Measure sustained first-audio-out p95.
Use It
caller: "what is the weather in tokyo tomorrow"
[asr ] partial @280ms: "what is the"
[asr ] partial @540ms: "what is the weather"
[turn ] completion score 0.82 at @820ms; commit
[llm ] first token @960ms
[tool ] weather.tokyo tomorrow -> 68/52 partly cloudy @1140ms
[tts ] first audio-out @1040ms: "Tokyo tomorrow will be partly cloudy..."
turn latency: 1040ms user-stop -> audio-out
Ship It
outputs/skill-voice-agent.md is the deliverable. Given a domain (customer support, scheduling, or kiosk), it stands up a LiveKit agent with the ASR/VAD/LLM/TTS pipeline tuned to the measurement bar. Rubric:
| Weight | Criterion | How it is measured |
|---|---|---|
| 25 | End-to-end latency | p50 first-audio-out under 800ms across 100 recorded calls |
| 20 | Turn-taking quality | False-cutoff rate under 3% on the Hamming VAD benchmark |
| 20 | Tool-use correctness | Mid-conversation tool calls that return the right data without stalling audio |
| 20 | Reliability under packet loss | WER and turn-taking stability with 3% packet drop injected |
| 15 | Eval harness completeness | Reproducible measurements with public config |
| 100 |
Exercises
-
Swap Deepgram Nova-3 for faster-whisper v3 turbo on a g5.xlarge. Measure the latency and WER gap. Identify where CPU-vs-GPU decisions matter.
-
Add an interruption-arbitration policy: what does the agent do when the user barges in during a tool call? Compare three policies (hard cancel, finish-tool-then-stop, queue next turn).
-
Run an adversarial turn-detector test: give the user long pauses mid-sentence. Tune the VAD silence threshold and the turn-detector score threshold for lowest false-cutoff without blowing past 900ms.
-
Deploy the same agent on PSTN via Twilio. Compare PSTN first-audio-out to WebRTC. Explain the jitter-buffer and codec differences.
-
Add voice activity detection for non-English languages (Japanese, Spanish). Measure the Silero VAD v5 false-trigger rate versus language-specific fine-tunes.
Key Terms
| Term | What people say | What it actually means |
|---|---|---|
| Turn detection | "End of utterance" | Classifier that, given VAD silence and a partial transcript, decides the user is done speaking |
| Barge-in | "Interruption handling" | Canceling TTS mid-playback when VAD detects new user speech |
| First-audio-out | "Latency" | Time from user stops speaking to the first audio packet leaving the server |
| VAD | "Speech gate" | Model classifying audio frames as speech vs silence; Silero VAD v5 is the 2026 default |
| Jitter buffer | "Audio smoothing" | Client-side buffer that holds packets briefly to absorb network variance |
| Filler | "Acknowledgment token" | Short phrase the agent emits to avoid silence when a tool is slow |
| MOS | "Mean opinion score" | Perceptual speech quality rating; NISQA is the automated proxy |
Further Reading
- LiveKit Agents 1.0 — reference WebRTC agent framework
- Pipecat — alternate Python-first streaming agent framework
- OpenAI Realtime API — reference for integrated speech models
- Deepgram Nova-3 documentation — streaming ASR reference
- Silero VAD v5 — VAD reference model
- Cartesia Sonic-2 — low-latency TTS reference
- Retell AI architecture — production voice agent architecture
- Vapi.ai production stack — alternate production reference
Code
ts/package.json
Open on GitHub ↗ts/README.md
Open on GitHub ↗ts/src/index.ts
npx tsx phases/19-capstone-projects/03-realtime-voice-assistant/code/ts/src/index.tsOpen on GitHub ↗ts/src/orchestrator.ts
npx tsx phases/19-capstone-projects/03-realtime-voice-assistant/code/ts/src/orchestrator.tsOpen on GitHub ↗ts/src/protocol.ts
npx tsx phases/19-capstone-projects/03-realtime-voice-assistant/code/ts/src/protocol.tsOpen on GitHub ↗ts/src/server.ts
npx tsx phases/19-capstone-projects/03-realtime-voice-assistant/code/ts/src/server.tsOpen on GitHub ↗ts/src/types.ts
npx tsx phases/19-capstone-projects/03-realtime-voice-assistant/code/ts/src/types.tsOpen on GitHub ↗ts/src/vad.ts
npx tsx phases/19-capstone-projects/03-realtime-voice-assistant/code/ts/src/vad.tsOpen on GitHub ↗ts/tests/orchestrator.test.ts
npx tsx phases/19-capstone-projects/03-realtime-voice-assistant/code/ts/tests/orchestrator.test.tsOpen on GitHub ↗ts/tests/protocol.test.ts
npx tsx phases/19-capstone-projects/03-realtime-voice-assistant/code/ts/tests/protocol.test.tsOpen on GitHub ↗ts/tests/vad.test.ts
npx tsx phases/19-capstone-projects/03-realtime-voice-assistant/code/ts/tests/vad.test.tsOpen on GitHub ↗ts/tsconfig.json
Open on GitHub ↗
Take-away files
skill-voice-agent.md
--- name: voice-agent description: Build a real-time voice agent with sub-800ms first-audio-out, barge-in handling, and mid-conversation tool use. version: 1.0.0 phase: 19 lesson: 03 tags: [capstone, voice, webrtc, livekit, pipecat, asr, tts, streaming] --- Given a domain (customer support, scheduling, retail assistant), deploy a WebRTC voice agent that keeps end-to-end first-audio-out under 800ms while handling barge-in, tool calls, and packet loss. Build plan: 1. Stand up a LiveKit Agents 1.0 room with a web client that streams microphone audio. Add a Twilio PSTN gateway for phone coverage. 2. Run streaming ASR (Deepgram Nova-3 hosted or faster-whisper Whisper-v3-turbo on a g5.xlarge). Subscribe to partial and final transcripts. 3. Run Silero VAD v5 on 20ms frames. On speech-end, score the latest partial with the LiveKit turn-detector; commit to turn-complete only when VAD silence >= 500ms and completion score >= 0.6. 4. Stream the LLM (GPT-4o-realtime, Gemini 2.5 Flash Live, or cascaded Claude Haiku 4.5). Hand the first token to TTS within 200ms. 5. Stream TTS (Cartesia Sonic-2 or ElevenLabs Flash v3). First audio chunk must leave the server within 200ms of first LLM token. 6. Barge-in: when VAD detects new user speech during SPEAKING or THINKING, cancel TTS, drop remaining LLM output, re-arm ASR. Publish a `tts_canceled` span. 7. Tool side-channel: run function calls concurrently; if latency > 300ms, emit an acknowledgment filler so the audio stream never stalls. 8. Record 100 calls. Measure WER against held-out transcripts, false-cutoff rate on the Hamming VAD benchmark, first-audio-out p50, NISQA MOS, and behavior under 3% packet drop. 9. Load-test 50 concurrent calls on a single g5.xlarge with a synthetic caller; report sustained first-audio-out p95. Assessment rubric: | Weight | Criterion | Measurement | |:-:|---|---| | 25 | End-to-end latency | p50 first-audio-out under 800ms across 100 recorded calls | | 20 | Turn-taking quality | False-cutoff rate under 3% on the Hamming VAD benchmark | | 20 | Tool-use correctness | Mid-conversation tool calls return correct data without stalling audio | | 20 | Reliability under packet loss | WER and turn-taking stability with 3% packet drop injected | | 15 | Eval harness completeness | Reproducible measurements with public config | Hard rejects: - Non-streaming pipelines (batch ASR, batch TTS) cannot hit the latency target. - Any barge-in policy that does not cancel the TTS buffer immediately. Delayed cancellation produces the worst user-experience regressions. - Tool calls that synchronously block the LLM stream. They must run on a side channel. Refusal rules: - Refuse to deploy without a VAD or a turn-detector. Fixed-timeout turn-taking produces unacceptable cutoff rates. - Refuse to report MOS without documenting whether it is human-rated or NISQA-proxied. - Refuse to report "p50 latency under X" without at least 100 recorded calls and publishing the call traces. Output: a repo containing the LiveKit agent worker, the PSTN gateway config, the 100-call eval harness, a public Langfuse voice dashboard, a side-by-side comparison with one hosted competitor (Retell, Vapi, or OpenAI Realtime API directly), and a write-up on the three largest turn-taking failures you observed and the detector tuning that fixed each.