How OpenAI delivers low-latency voice AI at scale
# How OpenAI Delivers Low-Latency Voice AI at Scale OpenAI rearchitected the WebRTC stack behind ChatGPT voice and the Realtime API to handle three problems that collided at scale: one-port-per-session media termination doesn't fit Kubernetes, ICE and DTLS sessions require stable ownership, and global routing must keep first-hop latency low. The solution is a split design in which a stateless UDP relay forwards packets to a stateful transceiver that owns the WebRTC session, which keeps standard client behavior intact while changing how traffic is routed internally. ## Why Voice AI Needs WebRTC - WebRTC standardizes the hard parts of real-time media: ICE and NAT traversal, DTLS and SRTP encryption, codec negotiation, RTCP quality control, echo cancellation, and jitter buffering. - Building on existing browser and mobile implementations lets the team focus on connecting real-time media to models. - Audio arrives as a continuous stream, so transcription, reasoning, tool calls, and speech generation can begin while the user is still talking. ## Choosing a Media Architecture - An SFU (selective forwarding unit) suits multiparty products such as group calls, but most OpenAI sessions are 1:1, with the user talking to one model. - The team chose a transceiver model: a WebRTC edge service terminates the client connection and converts media into simpler internal protocols for inference, transcription, speech generation, and orchestration. - The transceiver is the only service that owns WebRTC state (ICE checks, DTLS handshake, SRTP keys, session lifecycle), so backend services scale like ordinary services rather than acting as WebRTC peers. ## The Deployment Problem: WebRTC Meets Kubernetes The first implementation was a single Go service built on Pion that handled both signaling (SDP, codecs, ICE credentials) and media termination. Running it on Kubernetes exposed two problems: - **Port exhaustion:** One-port-per-session requires large public UDP port ranges. Cloud load balancers aren't built for tens of thousands of UDP ports per service, large ranges widen the attack surface, and pods that must reserve stable port ranges make autoscaling brittle. - **State stickiness:** ICE and DTLS are stateful, so every packet for a session must reach the process that created it. Otherwise setup fails or media breaks. The goal was a small, fixed public UDP surface that still routes each packet to the correct owning transceiver. ## Evaluated Approaches - **Unique IP:port per session (native direct UDP):** Direct media path, but requires one public port per session and fits Kubernetes and cloud load balancers poorly. - **Unique IP:port per server:** Small public footprint with one shared socket demultiplexing sessions, but across a load-balanced fleet the first packet can land on the wrong instance, so a deterministic steering mechanism is still needed. - **TURN relay (protocol-terminating):** Centralized edge policy, but allocations add setup round trips and are hard to move or recover. - **Stateless forwarder plus stateful terminator (chosen):** Small public UDP footprint and full session ownership in the transceiver, at the cost of one extra forwarding hop and custom coordination between relay and transceiver. ## Relay and Transceiver Architecture - The relay is a lightweight UDP forwarder. It does not decrypt media, run ICE, or take part in codec negotiation. It reads just enough packet metadata to pick a destination. - Signaling still goes to the transceiver. The SDP answer returns a shared relay VIP and port (for example, `203.0.113.10:3478`), giving the client one stable destination. - **First-packet routing on ICE ufrag:** The server-generated ICE username fragment (ufrag) is already exchanged during setup and echoed in STUN binding requests. OpenAI encodes routing hints (destination cluster and owning transceiver) into it, so the relay can route the first packet without an external lookup. - After the first packet, the relay caches a client-to-transceiver flow mapping in memory, and subsequent DTLS, RTP, and RTCP packets are forwarded without re-decoding the ufrag. - If a relay restarts, the next STUN packet rebuilds the session from the ufrag. A Redis cache of the client-to-transceiver mapping allows faster recovery before that packet arrives. - Each transceiver listens on a shared UDP socket rather than one socket per session. ## Global Relay and Geo-Steered Signaling - Once the public UDP surface was reduced to a few stable addresses, the same relay pattern could be deployed globally as "Global Relay" ingress points. - Cloudflare geo and proximity steering routes the initial HTTP or WebSocket signaling request to a nearby transceiver cluster, and the SDP answer advertises a nearby Global Relay address. - Packets enter the network close to the user, which lowers latency and jitter and avoids loss bursts before traffic reaches the backbone. - Together, these shorten signaling round trips and the first ICE connectivity check, reducing how long users wait before speech can start. ## Relay Implementation and Performance - The relay is written in Go and runs in userspace, reading packet headers from sockets and forwarding without terminating WebRTC. - **SO_REUSEPORT** lets multiple relay workers bind the same UDP port, with the kernel distributing packets across them to avoid a single read-loop bottleneck. - **runtime.LockOSThread** pins each UDP-reading goroutine to an OS thread. Combined with SO_REUSEPORT, this tends to keep each flow on one CPU core, improving cache locality and reducing context switches. - **Pre-allocated buffers and minimal copying** keep allocation and garbage-collection overhead low. - Kernel-bypass frameworks were considered but rejected as unnecessary, since this design handled global real-time traffic with a relatively small relay footprint. ## Results and Takeaways - WebRTC media now runs in Kubernetes without exposing thousands of UDP ports, which is easier to secure, load balance, and scale. - Clients still speak standard WebRTC, preserving browser and mobile interoperability. - The team's main lessons: - Preserve protocol semantics at the edge. - Keep hard session state in one place (the transceiver). - Route on information already present in setup, such as the ICE ufrag, rather than adding a hot-path lookup. - Optimize the common case before reaching for kernel bypass. The broader lesson is that complexity belongs in a thin routing layer, not in every backend service or in custom client behavior.