Updated
A voice agent is a loop: audio in, words out, audio back. The loop is easy to sketch and hard to make feel natural, because people notice a gap of more than about a second, and they notice even more when the agent talks over them or fails to stop when they interrupt. This guide follows the audio through each stage of a WebRTC voice agent and covers the choices that matter at each one.
1. Transport: WebRTC to an SFU
The caller's browser or app opens one RTCPeerConnection and publishes its microphone. You can make the agent a direct peer, but most production setups put a selective forwarding unit (SFU) in the middle. The SFU takes each published track and forwards it to whoever subscribes: the agent, a recorder, other humans on the call, or a bridge into a meeting platform. The agent publishes its own voice, and its face if it has one, as ordinary tracks.
WebRTC is worth the setup over a raw WebSocket for anything a person joins from a phone or flaky Wi-Fi. It runs over UDP, so a lost packet is concealed instead of stalling the stream, and the browser gives you echo cancellation, noise suppression and congestion control for free.
2. Signalling
WebRTC does not say how the two sides exchange offers and answers. You need a small signalling channel, usually a WebSocket, that carries session descriptions, track names, and call events such as captions or "the agent is thinking". Keep it boring. Every extra state in it is a race condition on a bad network.
For reference, LlamaCalls uses Cloudflare Realtime SFU for media and a JSON-over-WebSocket protocol to a Cloudflare Durable Object per call. The caller sends publish with an offer for its tracks and applies the answer; the room sends its own offer when there are agent tracks to receive. Captions and the agent's state (listening, thinking, speaking) travel on the same socket.
3. Voice activity detection and end of turn
Two questions decide how responsive the agent feels: has the caller started speaking, and have they finished? The first is voice activity detection (VAD). A model such as Silero VAD tells speech from noise far better than an energy threshold, which fires on a door closing or a keyboard.
The second is harder. Waiting for, say, 700 ms of silence is simple and always too slow or too eager. Better speech-to-text models now report end-of-turn themselves, including an early "probably done" signal. A good pattern is to start drafting a reply on that early signal, then throw the draft away if the caller keeps talking. That way most of the model's thinking time overlaps the caller's pause.
4. Streaming speech-to-text
Send audio continuously and use interim results for live captions; answer only the final transcript. Resample to what the model expects (often 16 kHz mono) on the agent side. Holding back silence until the VAD sees speech saves STT cost, as long as you replay the few hundred milliseconds before the onset so first words are not clipped.
5. The language model
Stream tokens. Cut the stream into sentences and hand each sentence to text-to-speech as soon as it ends, so the agent starts speaking while the model is still writing. Keep the prompt short enough that time to first token stays low, and run tool calls in a way that lets the agent say something like "let me check" instead of falling silent.
6. Streaming text-to-speech
Open the TTS connection before the first reply so the handshake is not on the critical path, push text sentence by sentence, and play audio as it arrives. Pace the output in real time (20 ms frames is typical for WebRTC) so you can stop it cleanly.
7. Barge-in
When the caller starts talking while the agent speaks, stop the TTS stream, drop any queued audio, and record in the conversation history only what was actually said. Otherwise the model believes the caller heard the whole answer. Decide on purpose whether a cough or a "mm-hm" should interrupt; VAD plus a minimum word count is a reasonable start.
8. A face (optional)
A lip-synced avatar is another renderer between TTS and the SFU: it takes the agent's audio and produces a video track in sync with it. Render it once, next to the agent, and publish it like a camera. Then every client (browser, phone, meeting bridge) just subscribes to a track and needs no avatar SDK of its own.
Where the latency goes
| Stage | What helps |
|---|---|
| End of turn | Model-based end-of-turn, draft on the early signal |
| LLM | Short prompts, a fast model, stream into TTS by sentence |
| TTS | Warm connection, streaming synthesis |
| Network | WebRTC over UDP, an SFU close to the caller |
Build it or rent it
If you want to own this pipeline, LiveKit Agents and Pipecat are the two open-source frameworks most teams start from, and both support phone calls too. We compare them in LiveKit alternatives and Daily and Pipecat alternatives.
If the agent is a feature rather than your product, LlamaCalls runs all of the above as a service: one request from your server describes the agent and returns a call you embed with an iframe or join from iOS. See how to add an AI agent to a video call for the code.