WebRTC for Voice AI: A Practical Primer

CallMissed
·6 min readGuide

WebRTC is the transport that almost every browser-based voice AI runs on. It is also the layer that most application teams treat as a black box until something breaks at 3am. This primer is the minimum viable understanding of WebRTC you need to ship voice agents in 2026 — enough to design well, debug usefully, and ask the right questions of your media-server vendor.

Why WebRTC at all

For real-time voice AI, you have three transport options:

  • WebSocket. Easy to build, but no congestion control, no jitter buffer, no codec negotiation. Fine for prototypes, painful in production over real networks.
  • HLS / DASH. Latencies measured in seconds. Wrong tool for the job.
  • WebRTC. Built for sub-second media. Codec negotiation, jitter buffering, congestion control, NAT traversal, and ubiquitous browser support all included.
  • WebRTC is the answer for any voice agent that needs to feel real-time. The trade-off is operational complexity.

    P2P vs SFU: pick SFU

    Vanilla WebRTC is peer-to-peer. For two-party calls in an old-school videoconferencing context, P2P is fine. For voice AI, it is the wrong shape:

  • The agent is a server-side process, not a peer.
  • You need recording, monitoring, and observability — server-side is the place for those.
  • Multi-region scaling demands a central media routing point.
  • The standard architecture in 2026 is a Selective Forwarding Unit (SFU): the user's browser connects to the SFU, the AI agent process also connects to the same room, and the SFU forwards media between them. Per Stream's WebRTC voice AI guide, this lets you scale SFU and agent independently — an SFU node might handle hundreds of connections while a GPU-bound agent node handles ten.

    MCUs (Multipoint Control Units) — which decode, mix, and re-encode media centrally — are mostly relegated to legacy conferencing today. The transcoding cost rarely pays off for AI use cases.

    NAT, STUN, and TURN: the part everyone skips

    If your test environment is "two laptops on the same WiFi" your NAT story works. The moment one user is behind a hotel firewall and the other is on a corporate network, things break.

  • STUN servers help peers discover their public IP/port pair. Cheap, stateless, often bundled with media services.
  • TURN servers relay media when STUN can't establish direct connectivity. Bandwidth-intensive, charged per GB, but the only thing that works on restrictive networks.
  • In 2026, plan on roughly 10–25% of production sessions falling back to TURN, depending on your user demographics. [Inference] Budget for it. Hosted media providers (LiveKit Cloud, Daily, Twilio) include managed TURN; self-hosters need to run their own.

    Opus: the only audio codec that matters

    Per Stream's WebRTC codec guide, Opus is the gold-standard audio codec for WebRTC. It adapts to bandwidth from 6 kbps up to 510 kbps, handles voice and music well, and recovers gracefully from packet loss with FEC and PLC.

    For voice AI, you usually run Opus at 16–32 kbps voice mode with discontinuous transmission (DTX) on. DTX saves bandwidth during silence and is fine because your VAD ignores silence anyway.

    The codec choices that show up in legacy systems — G.711, G.722, iLBC — are mostly there for telephony interop. Inside your WebRTC pipeline, stick with Opus.

    What the agent side actually does

    When an AI agent joins a WebRTC room, it is not running a browser. It is a server process that speaks WebRTC. The minimal pipeline:

  • Inbound audio arrives as Opus RTP packets via the SFU.
  • Decode to PCM (typically 16 kHz, 16-bit mono for STT).
  • VAD runs on PCM frames to detect speech.
  • STT consumes speech segments, emits transcripts.
  • LLM processes transcripts, emits text responses.
  • TTS converts text to PCM.
  • Encode to Opus.
  • Outbound RTP is published to the SFU room.
  • LiveKit Agents, Pipecat, and managed voice platforms wrap most of this for you. Building it from scratch is doable but rarely the right ROI.

    Latency budget across the WebRTC layer

    Network and codec latency is real but small relative to model latency:

  • Mic capture to encode: ~20ms.
  • Network RTT (good case): 30–80ms.
  • SFU forwarding: <10ms.
  • Decode and jitter buffer: 20–60ms.
  • Total transport latency on a healthy connection is typically 100–200ms one-way. [Inference] On a TURN-relayed connection, add another 50–150ms. This is your floor; nothing in the model stack can compensate for a bad network.

    Common WebRTC failure modes

    Three patterns to expect in production:

  • Asymmetric NAT. Some networks (especially corporate, mobile hotspots) drop direct peer connectivity. TURN is your only escape.
  • Packet loss. 1–5% packet loss is common over mobile data. Opus FEC and jitter buffering help; setting your jitter buffer too low makes everything worse.
  • Bandwidth crunch. Opus at 16 kbps degrades gracefully but eventually intelligibility suffers. Bandwidth estimation in modern WebRTC stacks adapts automatically; you rarely need to tune it.
  • Hamming's WebRTC debugging guide is a useful reference for production triage.

    Hosted vs self-hosted

    In 2026 the trade-offs are clear:

  • Hosted (LiveKit Cloud, Daily, Twilio Voice, Vonage): turnkey TURN, multi-region SFU, observability dashboards. Pay per minute or per concurrent connection.
  • Self-hosted (LiveKit OSS, mediasoup, Janus, Jitsi): full control, lower marginal cost at scale, but you own NAT issues, capacity planning, and incident response.
  • Most teams ship hosted first and migrate to self-hosted only when scale or compliance forces it. [Inference]

    A pragmatic checklist

    If you're building voice AI on WebRTC:

  • Use an SFU, not P2P.
  • Plan for TURN on 10–25% of sessions; budget bandwidth.
  • Stick with Opus.
  • Use a framework (LiveKit Agents or Pipecat) rather than rolling RTP from scratch.
  • Measure transport latency separately from model latency.
  • Test on real networks — corporate WiFi, mobile data, hotel networks — not just home broadband.
  • The bottom line

    WebRTC is not optional for browser-first voice AI in 2026. Understand SFU vs P2P, plan for TURN, default to Opus, and lean on a framework rather than building media servers yourself. Most pain comes from the network layer, not the model layer; instrument accordingly.

    Frequently Asked Questions

    Do I need WebRTC if I'm building a phone-only voice agent?
    For pure PSTN voice, no — you'll route through SIP or a telephony provider's API instead. WebRTC matters for browser-based clients, mobile apps, and any agent that joins a multimedia room.
    How much does TURN bandwidth typically cost?
    Hosted providers usually charge $0.50–$1.00 per GB on TURN traffic in 2026. A typical 5-minute voice call relayed end-to-end through TURN uses roughly 5–10 MB. [Inference] Budget accordingly if a meaningful fraction of your traffic is relayed.
    SFU vs MCU — when would I ever use an MCU?
    Almost never for new voice AI builds. MCUs make sense only when you need server-side mixing — for example, recording a multi-party conference as a single audio file. For 1:1 user-to-agent voice, SFU is the right shape.

    Related Posts