WebRTC for Voice Agents: Audio Streams, Barge-In and Turn-Taking

WebRTC voice agents need low-latency audio streams, clean barge-in and tuned turn-taking. Build one with the OpenAI Realtime API in five steps.

By Galactic News Editorial · Published 2026-11-23T16:31:59+00:00 · Updated 2026-11-23T16:31:59+00:00

WebRTC voice agents audio streams with barge-in and turn-taking

WebRTC for Voice Agents: Audio Streams, Barge-In and Turn-Taking

WebRTC voice agents audio streams carry a live conversation between a caller and a speech-to-speech model. WebRTC moves the audio. The model listens, reasons and speaks back in the same session. This tutorial explains the architecture, shows how to build it with the OpenAI Realtime API, and covers barge-in and turn-taking, the two behaviours that make or break a voice agent.

How WebRTC voice agents audio streams work

A voice agent has three layers. Transport moves audio between the caller and your system. Processing turns speech into a response. Orchestration runs your business logic, tools and guardrails. WebRTC handles the transport layer for browsers and mobile apps. It gives you low-latency, encrypted audio streams with built-in handling for network jitter, packet loss and echo.

The OpenAI Realtime API is the processing layer in this tutorial. It is a low-latency interface for speech-to-speech models. One model listens to audio, reasons, calls tools and speaks back in a single session. The model works directly with audio, so there is no separate transcription step and no separate synthesis step. That removes two hops from the pipeline and keeps tone and interruptions natural.

The Realtime API went generally available in August 2025. As of November 2026 the current models are gpt-realtime-2.1 and gpt-realtime-2.1-mini. The API connects over WebRTC, WebSocket or SIP. Use WebRTC for browsers and mobile apps. Use WebSocket for servers and telephony bridges. Use SIP for phone calls.

Choose the right transport

ClientTransportAuthWhen to use it
Browser or mobile appWebRTC to /v1/realtime/callsEphemeral client secret minted by your serverDirect voice sessions in the browser
Your backend or call-centre bridgeWebSocket to wss://api.openai.com/v1/realtimeStandard API key, server onlyServer-side orchestration and telephony
Phone networkSIPServer-side credentialsInbound and outbound phone calls
Browser, config must stay server-sideUnified WebRTC with a sideband WebSocketEphemeral secret plus server controlBackend needs to watch events and answer tool calls

The standard API key must never reach a browser. Your server mints a short-lived ephemeral client secret and hands it to the client. The client uses it to open the WebRTC session. A sideband WebSocket can attach to the same call so your backend watches events, updates instructions, answers tool calls, transfers or hangs up.

Build a WebRTC voice agent in five steps

  1. Mint an ephemeral client secret on your server. Call POST /v1/realtime/client_secrets with your API key and the session config. Return the short-lived ek_ token to the browser. Never send the standard key to the client.
  2. Connect from the browser over WebRTC. Create a RealtimeSession with your agent definition and the model, then connect with the ephemeral key. The session opens a WebRTC connection to /v1/realtime/calls. Keep audio transport concerns in the session layer and business logic in the agent definition.
  3. Configure the session. Send a session.update with instructions, voice, turn detection and tools. Set session.type and put output audio configuration under session.audio.output. If you are migrating from the beta interface, remove the OpenAI-Beta: realtime=v1 header first.
  4. Handle barge-in. When the user starts speaking while the agent is talking, stop playback immediately. Then send conversation.item.truncate so the model forgets the audio the user never heard. Skipping the truncate step is the most common bug: the model then thinks the user heard an answer they never did.
  5. Close the tool-call loop. When the model calls a function, run it on your server and return the result with response.create. Keep tool calls fast or acknowledge them out loud so the caller is not left in silence.

Measure end-of-speech to first-audio latency instead of guessing. One production deployment measured p50 voice-to-voice latency of 353ms and p95 of 686ms across its pipeline. Human conversational expectations need response initiation under roughly 500ms. Test with recorded utterances and check for false barge-ins and premature turn ends.

Barge-in and turn-taking

Turn-taking decides when the agent stops listening and starts speaking. The Realtime API offers two automatic turn detection modes. server_vad uses voice activity detection with tunable settings. semantic_vad detects the end of a thought rather than a silence gap. Pick the mode from how your users actually speak.

Tune server_vad with four settings. threshold sets how loud speech must be to count. prefix_padding_ms keeps a little audio from before speech started. silence_duration_ms sets how long a pause must last before the turn ends. Start with the defaults, then test against real call recordings. Noisy phone lines need a higher threshold and a longer silence duration. Short, clipped answers need a shorter silence duration.

Watch for two failure modes. False barge-ins happen when background noise or the agent's own voice triggers the detector. Echo cancellation and a higher threshold help. Premature turn ends happen when a user pauses mid-thought and the agent jumps in. A longer silence duration and semantic_vad help here. Test thresholds and idle timeouts on real recordings, not on clean demo audio.

Keep responses short. Long monologues invite interruptions and hide latency problems. If a tool call takes time, say so out loud. A short acknowledgement keeps the turn natural while the backend works.

Common failure modes

SymptomLikely causeFix
Agent talks over the userTurn detection too eagerRaise silence_duration_ms, try semantic_vad
Agent repeats unheard answersMissing conversation.item.truncate on barge-inStop playback, then truncate the unheard audio
Long awkward silencesSlow tool calls with no acknowledgementAcknowledge out loud, then return the result
Agent interrupts itselfEcho or self-voice triggering VADCheck echo cancellation, raise threshold
Session fails in the browserStandard API key used client-sideMint ephemeral secrets on the server

Bottom line

Use WebRTC for browser and mobile voice agents and WebSocket for server-side work. Mint ephemeral secrets on your server. Tune turn detection against real recordings. Always truncate unheard audio on barge-in. Get those four things right and the agent feels conversational. Skip any one of them and users notice within seconds.