- Compare cascaded ASR–LLM–TTS pipelines with speech-to-speech models on latency, control, prosody and cost.
- Build and optimize a voice-to-voice latency budget stage by stage.
- Implement a frame-based endpointer and a clause-level streaming chunker for TTS.
- Explain endpointing, barge-in, echo cancellation and history truncation in turn-taking.
- Describe telephony integration with SIP, RTP, G.711 and WebRTC, and compute basic audio frame sizes.
- Evaluate voice agents with WER, entity accuracy, latency percentiles and conversation metrics, and apply consent and disclosure obligations.
Before this: Speech and Audio AI: Recognition, Synthesis and Real-Time Voice, AI Agents Explained, Tool Use and Function Calling
Voice is the oldest human interface and, for many tasks, still the fastest: customer support lines, appointment booking, in-car assistants, accessibility tools, and hands-busy work such as field service or clinical note-taking. Voice agents combine speech recognition, a language model with tools, and speech synthesis into a system that holds a spoken conversation and gets work done. The engineering is dominated by one constraint text chat never faces: time. In conversation, a pause of a second feels awkward and two seconds feels broken. This lesson explains the architectures, shows how to budget latency millisecond by millisecond, covers telephony basics, and sets out the evaluation and legal and ethical obligations that come with putting a synthetic voice on a phone line.
The intuition: a relay team versus a single runner
The classic voice agent is a relay: one runner (speech recognition) listens and writes down what was said, hands a note to the second runner (the language model), who writes a reply and hands it to the third (speech synthesis), who reads it aloud. Each handoff costs time, and the note loses information: tone, hesitation, sarcasm, background noise.
A speech-to-speech model is a single runner who hears and speaks directly, with no written notes in between. It is faster and catches tone, but you cannot read its notes to check them, and it is harder to coach.
Good conversation also needs turn-taking: knowing when the other person has finished (not just paused for breath), and stopping politely when interrupted. A comparison of conversation in ten languages (Stivers et al., 2009) found that every language studied minimizes both overlap and silence, with typical gaps between turns of a few hundred milliseconds or less. People begin planning their reply before the other speaker finishes. Machines that wait for silence and then start thinking will always feel slow; good voice systems overlap listening, thinking and speaking.
Architecture 1: the cascaded pipeline (ASR → LLM → TTS)
- Voice activity detection (VAD) classifies short audio frames (typically 10 to 30 ms) as speech or non-speech.
- Automatic speech recognition (ASR) converts speech to text. Streaming ASR emits partial hypotheses while the user talks and a final transcript at the end of the utterance.
- Endpointing / turn detection decides the user has finished their turn.
- The LLM (with tools, retrieval and state) generates a text reply, streamed token by token.
- Text-to-speech (TTS) converts text to audio, ideally streaming: it starts speaking from the first phrase while the LLM is still generating.
- Transport carries audio to and from the user (WebRTC in a browser or app, or a telephone network).
Advantages: every component is swappable and the best of its kind; you get a text transcript to log, inspect, moderate and evaluate; tool use and guardrails work exactly as in text agents. Disadvantages: latency stacks up across stages, and paralinguistic information (emotion, emphasis, who is speaking) is mostly lost at the text bottleneck.
Architecture 2: speech-to-speech models
Speech-to-speech (also called end-to-end or native audio) models take audio tokens in and produce audio tokens out, usually with an internal text stream as well. Research systems such as Kyutai's Moshi (Défossez et al., 2024) demonstrated full-duplex dialogue, where the model listens and speaks simultaneously, and commercial providers offer realtime speech-to-speech APIs over WebSocket or WebRTC.
| Aspect | Cascaded pipeline | Speech-to-speech |
|---|---|---|
| Latency | Sum of stages; needs careful streaming | Lower; one model |
| Prosody and emotion | Mostly lost at the text step | Preserved in input and output |
| Control and debugging | High: inspect each stage | Lower: fewer intervention points |
| Component choice | Mix and match ASR, LLM, TTS, voices | Tied to one model and its voices |
| Tool use and reasoning | Strongest text LLMs available | Often behind the best text models |
| Cost | Sum of three services | Often higher per minute for audio tokens |
Many production teams still use cascades for task-oriented agents (support, booking) because control, compliance and tool reliability matter more than prosody; speech-to-speech is attractive for open conversation and companionship-style products. Hybrids exist, such as a speech-to-speech front end that calls a text model for hard reasoning.
Turn-taking: endpointing and barge-in
Endpointing is the decision that the user's turn has ended. The simplest rule is "silence for milliseconds". The trade-off is sharp:
- Short (around 200 ms): fast, but the agent interrupts people who pause mid-sentence ("My account number is ... uh ... 4-7-2...").
- Long (around 800 ms or more): polite, but every turn feels sluggish.
Better systems use semantic endpointing: a small model looks at the partial transcript (and sometimes prosody) and predicts whether the utterance is complete. "I'd like to book a table for" is clearly unfinished; "Yes" is clearly finished. The silence threshold can then adapt: short when the sentence is complete, long when it is not.
Barge-in is the user interrupting the agent. The system must:
- Detect user speech while the agent is talking, which requires acoustic echo cancellation so the agent does not hear its own voice through the user's speaker and interrupt itself.
- Stop TTS playback within roughly 100 to 200 ms.
- Cancel in-flight LLM generation and pending TTS.
- Truncate the agent's last message in the conversation history to what the user actually heard. If the agent generated three sentences but only one was played, the model must not believe it said all three. This detail is easy to miss and causes strange follow-ups.
- Distinguish real interruptions from backchannels ("mm-hm", "right") and noise, which should not stop the agent.
Worked example: a latency budget
Define voice-to-voice latency as the time from the moment the user stops speaking to the moment they hear the first audio of the reply. Here is a realistic first build of a cascaded agent on a phone line:
| Stage | Time (ms) | Notes |
|---|---|---|
| Endpointing silence threshold | 500 | Fixed silence rule |
| ASR finalization | 150 | Final transcript after endpoint |
| LLM time to first token | 400 | Includes network and prompt processing |
| LLM tokens to first speakable chunk | 300 | Waits for a full sentence of ~15 tokens at 50 tokens/s |
| TTS time to first audio byte | 200 | |
| Transport and jitter buffer | 150 | Telephony network plus playback buffering |
| Total | 1,700 |
1.7 seconds feels slow. Now optimize stage by stage:
| Change | Stage | Before → After (ms) | Saving |
|---|---|---|---|
| Semantic endpointing (adaptive threshold) | Endpointing | 500 → 250 | 250 |
| Smaller, faster model; prompt caching | LLM first token | 400 → 250 | 150 |
| Flush to TTS at first clause (~5 tokens) | First chunk | 300 → 100 | 200 |
| Streaming TTS with lower first-byte latency | TTS | 200 → 120 | 80 |
| Co-locate services in one region | Transport | 150 → 120 | 30 |
New total: ms, a saving of 710 ms. The two biggest wins came from not waiting: deciding the turn ended sooner and starting to speak before the sentence was complete. Two further techniques are common:
- Speculative generation: start the LLM on the partial transcript before the endpoint fires, and discard the result if the user keeps talking.
- Filler and acknowledgement: when a tool call will take seconds (a database lookup), have the agent say "Let me check that for you" immediately rather than going silent.
Always report latency as a distribution: medians hide the slow tail that users remember. A system with 900 ms median and 3,000 ms at the 95th percentile feels unreliable.
Streaming in code
Two building blocks: an endpointer over audio frames, and a chunker that releases text to TTS at clause boundaries.
import numpy as np
def frame_energy_db(frame: np.ndarray) -> float:
rms = np.sqrt(np.mean(frame.astype(np.float64) ** 2)) + 1e-12
return 20 * np.log10(rms)
def endpoint_index(frames, threshold_db=-40.0, silence_ms=500, frame_ms=20):
# Return the frame index where the turn ends, or None if it has not ended.
needed = silence_ms // frame_ms
heard_speech, silent_run = False, 0
for i, f in enumerate(frames):
if frame_energy_db(f) > threshold_db:
heard_speech, silent_run = True, 0
elif heard_speech:
silent_run += 1
if silent_run >= needed:
return i
return None
sr = 16_000
t = np.arange(sr) / sr
speech = 0.1 * np.sin(2 * np.pi * 220 * t) # 1 s of "speech"
audio = np.concatenate([speech, np.zeros(sr)]) # then 1 s of silence
frames = audio.reshape(-1, sr * 20 // 1000) # 20 ms frames of 320 samples
print(endpoint_index(frames)) # 74: frame 50 is the first silent one, 25 silent frames needed
This energy-based endpointer is the textbook baseline; real systems use trained VAD models, which are far more robust to background noise, plus semantic turn detection. The example shows the mechanics: 20 ms frames at 16 kHz are 320 samples each; 500 ms of silence is 25 frames; speech ends after frame 49, so the endpoint fires at frame 74.
import re
def speakable_chunks(token_stream, min_chars=20):
# Yield text pieces at clause or sentence boundaries for streaming TTS.
buf = ""
for tok in token_stream:
buf += tok
m = next((m for m in re.finditer(r"[.!?;:,]\s", buf) if m.end() >= min_chars), None)
if m:
yield buf[: m.end()].strip()
buf = buf[m.end():]
if buf.strip():
yield buf.strip()
tokens = ["Sure", ",", " I", " can", " help", " with", " that", ".", " Your",
" order", " ships", " on", " Monday", "."]
print(list(speakable_chunks(tokens)))
# ['Sure, I can help with that.', 'Your order ships on Monday.']
The first chunk is released as soon as a boundary appears after at least 20 characters, so TTS can start speaking while the model is still writing. Too small a minimum produces choppy prosody; too large adds latency. (The trailing space check means the final sentence is emitted by the flush at the end.)
Telephony integration basics
Most commercial voice agents answer phone numbers, so you need to know the plumbing.
- PSTN (public switched telephone network) is the traditional phone network. To reach it from software you go through a carrier or a communications platform that provides phone numbers.
- SIP (Session Initiation Protocol, RFC 3261) is the signalling protocol for setting up, modifying and ending calls (INVITE, ACK, BYE). A SIP trunk connects your system to a carrier.
- RTP (Real-time Transport Protocol) carries the actual audio packets, typically 20 ms per packet.
- Codecs: classic telephony uses G.711 at 8 kHz sampling and 8 bits per sample, giving kbit/s. Each 20 ms frame is samples, which is 160 bytes. Telephone audio is narrowband (roughly 300 to 3,400 Hz), which lowers ASR accuracy compared with wideband microphones; use ASR models tuned for telephony, and resample correctly (for example to 16 kHz) when a model expects it.
- DTMF tones (keypad presses) arrive in-band or as separate events; agents often need them for menus and PIN entry.
- WebRTC is the browser and mobile standard for real-time media. It handles NAT traversal (ICE with STUN and TURN servers), encryption (DTLS-SRTP), and adaptive jitter buffering, and typically uses the Opus codec, which is wideband and far better than G.711 for recognition.
- Call control: transfers to humans (warm transfer, where context is passed to the human agent), hold, recording, and hang-up detection. Design the human handoff from day one.
A typical architecture: carrier → SIP trunk → media server or communications platform → your agent service over a WebSocket or WebRTC stream of audio frames → ASR, LLM and TTS services, with tools calling your business systems.
Evaluating voice agents
Evaluate each layer and the whole.
ASR accuracy: word error rate,
where , and are the numbers of substituted, deleted and inserted words in the best alignment and is the number of words in the reference transcript. Example: reference "please cancel my order for tomorrow morning" (); hypothesis "please cancel the order for tomorrow" has one substitution (my → the) and one deletion (morning), so WER . WER treats all words equally, but "cancel" misheard as "council" matters more than "the" for "a"; also measure entity accuracy for names, numbers, dates and account IDs, the things that break tasks.
Latency: voice-to-voice latency at p50 and p95, measured end to end on real networks, not just component benchmarks.
Conversation quality: task completion rate, turns to completion, transfer-to-human rate, false barge-ins (agent stopped by noise or backchannels), missed barge-ins, cut-offs (agent interrupted the user), and repeated-question rate.
TTS quality: intelligibility, pronunciation of domain terms and numbers, and naturalness, often measured by human ratings such as mean opinion score (MOS) on a 1 to 5 scale.
Simulation at scale: an LLM-driven simulated caller speaks through TTS to your agent, with scripted personas, accents, background noise and interruptions, and an automated judge checks outcomes. Pair it with review of real call recordings (with consent) because simulated callers are more cooperative than real ones.
Safety, consent and the law
Voice adds risks text agents do not have.
Impersonation and voice cloning. Modern TTS can clone a voice from a short sample. Only clone voices with documented, informed consent from the speaker, restrict who can create cloned voices, and consider watermarking or provenance metadata for generated audio. Never build an agent that claims to be a specific real person without authorization.
Disclosure. Tell people they are talking to an AI, at the start of the call. Beyond being good practice, it is increasingly a legal requirement: for example, the EU AI Act includes transparency obligations for AI systems that interact directly with people, and several US states have enacted bot-disclosure rules in some contexts. Requirements differ by jurisdiction and change often, so get legal review for your markets.
Robocall rules (US). In February 2024 the US Federal Communications Commission issued a declaratory ruling (FCC 24-17) that AI-generated voices count as "artificial" voices under the Telephone Consumer Protection Act (TCPA). In practice this means outbound calls using AI voices fall under the same consent requirements as prerecorded-voice robocalls, generally requiring the called party's prior express consent, with stricter written consent for telemarketing. The ruling came shortly after AI-generated robocalls imitating President Biden's voice urged New Hampshire voters to skip the January 2024 primary. Inbound calls that a customer initiates are a different situation, but disclosure and recording rules still apply.
Call recording. Recording and transcription require consent under wiretapping laws. In the US some states require only one party's consent and others require all parties' consent; other countries have their own rules, and data-protection laws such as the GDPR govern storage and use of recordings and transcripts. Announce recording, honour opt-outs, set retention limits, and redact sensitive data (card numbers, health details) from transcripts.
Voice as an authentication factor. Voice cloning undermines "voiceprint" authentication. Your agent should not treat a familiar-sounding voice as proof of identity, and high-risk actions (payments, account changes) need out-of-band verification.
Vulnerable callers. Phone lines reach people in distress. Define escalation paths to humans for emergencies, complaints and anyone asking for a person.
Common pitfalls
- Fixed long silence thresholds. Every turn pays the full timeout. Use adaptive or semantic endpointing.
- Waiting for the full reply before TTS. Stream at clause boundaries.
- No echo cancellation. The agent hears itself and interrupts itself.
- Not truncating history on barge-in. The model believes it said things the user never heard.
- Measuring only median latency. The p95 tail drives user perception.
- Testing only on clean wideband audio. Phone calls are narrowband, noisy and accented.
- Silent tool calls. Several seconds of silence sounds like a dropped call; acknowledge and fill.
- Skipping disclosure and consent. Legal exposure, and a breach of user trust.
In practice
- Choose a cascade when you need control, auditability, strong tool use and compliance; consider speech-to-speech when natural prosody and minimal latency dominate. Prototype both against your latency and quality targets.
- Write a latency budget per stage and track p50 and p95 in production with per-stage timestamps.
- Stream everything: partial ASR, LLM tokens, and TTS audio; flush at clause boundaries; speculate where safe.
- Invest in turn-taking: trained VAD, semantic endpointing, echo cancellation, backchannel-aware barge-in, and history truncation.
- Design human handoff, disclosure, recording consent and data retention before launch, with legal review for each jurisdiction you call into.
- Build a simulation harness with noisy, accented, interrupting callers, and review real calls regularly.
Key takeaways
- Cascaded pipelines trade latency and lost prosody for control and auditability; speech-to-speech models trade the reverse.
- Voice-to-voice latency is a sum of stages, and the biggest wins come from ending turns sooner and speaking before the reply is complete.
- Barge-in requires echo cancellation, fast cancellation of playback and generation, and truncating history to what the user actually heard.
- Telephony audio is narrowband G.711 at 64 kbit/s, so ASR quality and resampling need explicit attention.
- Disclose AI use, obtain consent for recording and for cloned voices, and treat outbound AI-voice calls in the US as subject to TCPA robocall consent rules.
Terms in this lesson
- ASR
- Automatic speech recognition, the conversion of spoken audio into text.
- TTS
- Text-to-speech, the synthesis of spoken audio from text.
- Endpointing
- Deciding that a speaker has finished their conversational turn.
- Barge-in
- A user interrupting a voice agent while it is speaking, which the agent must detect and yield to.
- Voice-to-voice latency
- The time from the end of the user's speech to the first audible sound of the agent's reply.
- SIP
- The Session Initiation Protocol, used to set up, modify and terminate voice and video calls over IP networks.
- WebRTC
- A set of browser and mobile standards for encrypted real-time audio, video and data with built-in NAT traversal.
- Word error rate (WER)
- The number of word substitutions, deletions and insertions needed to turn a transcript into the reference, divided by the reference length.
Check your understanding
- 1.
A G.711 call uses 8 kHz sampling at 8 bits per sample with 20 ms packets. How many bytes of audio payload are in each packet?
- 2.
A pipeline has 600 ms endpointing, 150 ms ASR, 350 ms LLM first token, 250 ms to first speakable chunk, 200 ms TTS first byte and 100 ms transport. Which single change saves the most?
- 3.
After a barge-in, the agent's next reply refers to information it generated but the user never heard. What was missed?
- 4.
Reference: 'transfer five hundred dollars to savings' (6 words). Hypothesis: 'transfer nine hundred dollars to my savings'. What is the WER?
- 5.
A team wants an agent that handles insurance claims with many tool calls, strict audit logging and moderation of every response. Which architecture fits best?
Exercises
Work each problem before opening the solution.
- Exercise 1Core
Your agent's latency budget is: endpointing 400 ms, ASR final 120 ms, LLM first token 300 ms, first speakable chunk 250 ms, TTS first byte 180 ms, transport 100 ms. (a) Compute voice-to-voice latency. (b) Your target is 800 ms. Propose changes with estimated savings that reach it, and say which carries the most user-experience risk.
Show solution
(a) 400 + 120 + 300 + 250 + 180 + 100 = 1,350 ms.
(b) Need to save 550 ms. One plan:
- Semantic endpointing: 400 to 200 ms (saves 200).
- Flush TTS at first clause: 250 to 80 ms (saves 170).
- Faster model with prompt caching: 300 to 180 ms (saves 120).
- Lower-latency streaming TTS: 180 to 120 ms (saves 60).
Total saving 550 ms, giving 200 + 120 + 180 + 80 + 120 + 100 = 800 ms.
Most risk: the endpointing change. Cutting the threshold increases the chance of interrupting users mid-thought, which is worse than slowness. Mitigate by making it adaptive (short only when the transcript looks complete) and tracking the cut-off rate. A smaller model is the second risk: check task success does not drop.
- Exercise 2Core
Implement
wer(ref, hyp)in Python using word-level edit distance, and verify it on: ref = 'please cancel my order for tomorrow morning', hyp = 'please cancel the order for tomorrow'.Show solution
def wer(ref: str, hyp: str) -> float: r, h = ref.split(), hyp.split() d = [[0] * (len(h) + 1) for _ in range(len(r) + 1)] for i in range(len(r) + 1): d[i][0] = i # i deletions for j in range(len(h) + 1): d[0][j] = j # j insertions for i in range(1, len(r) + 1): for j in range(1, len(h) + 1): cost = 0 if r[i - 1] == h[j - 1] else 1 d[i][j] = min(d[i - 1][j] + 1, # deletion d[i][j - 1] + 1, # insertion d[i - 1][j - 1] + cost) # substitution or match return d[len(r)][len(h)] / len(r) print(round(wer("please cancel my order for tomorrow morning", "please cancel the order for tomorrow"), 3)) # 0.286The minimum edit distance is 2 (substitute my to the, delete morning) over 7 reference words: 2/7 = 0.286. Production toolkits also normalize case, punctuation and number formats before scoring, because 'five' versus '5' should usually not count as an error.
- Exercise 3Core
Write the barge-in handling logic as a sequence of steps (or pseudocode) for a cascaded agent. Include what state you need to track so that history truncation is correct.
Show solution
State to track for the current agent turn: the list of text chunks sent to TTS, and for each chunk its audio duration and the playback start time (or a playback-position callback from the audio player).
On detecting user speech while the agent is speaking:
- Check it is real speech, not echo or backchannel: echo-cancelled VAD above threshold for at least ~200 ms, optionally a quick ASR check that the words are not 'mm-hm' or 'yeah'.
- Stop audio playback immediately and record the playback position
t_stop. - Cancel pending TTS requests and the in-flight LLM stream.
- Determine what was heard: sum chunk durations until
t_stop; include fully played chunks, and for the partially played chunk include text up to an estimate based on the fraction played (or word timestamps if the TTS provides them). - Replace the assistant message in history with the heard text, optionally marking it as interrupted, so the model knows it was cut off.
- Switch to listening; feed the user's new utterance to ASR, then continue the normal turn.
The essential data is the mapping from text to audio time; without it you cannot know what the user heard.
- Exercise 4Challenge
A dental clinic wants an AI agent that (1) answers inbound calls to book appointments and (2) places outbound reminder calls the day before appointments in the US. Write a compliance and safety checklist covering disclosure, consent, recording, data handling, voice choice, escalation and testing. Mark which items apply to inbound, outbound or both.
Show solution
Disclosure (both). The agent states at the start that it is an automated AI assistant for the clinic and offers a human option. Verify state-specific bot-disclosure requirements with counsel.
Outbound consent (outbound). Under the FCC's February 2024 ruling, AI-generated voices are 'artificial' voices under the TCPA, so reminder calls need the patient's prior express consent; collect it at registration, record when and how, and honour revocation promptly. Keep calls informational (no marketing content, which needs stricter written consent). Respect calling-time restrictions and do-not-call requests.
Recording and transcription (both). Announce recording; follow all-party-consent rules where callers may be located; allow opt-out; set retention limits.
Health data (both). Appointment details are protected health information under US health-privacy law (HIPAA), so every vendor in the audio, ASR, LLM and TTS chain that handles it needs the appropriate business associate agreement; minimize what is said on reminders (e.g. avoid procedure details on voicemail); verify identity before discussing details on inbound calls.
Voice choice (both). Use a stock synthetic voice or a cloned voice only with written consent from the speaker; never imitate a specific clinician without consent.
Escalation (both). Immediate transfer to staff for emergencies (pain, swelling, bleeding), complaints, billing disputes, or any request for a person; after-hours fallback message with emergency guidance.
Identity and security (inbound). Do not treat voice as authentication; verify with date of birth plus another factor before changing appointments.
Testing (both). Simulated callers with accents, noise and interruptions; scripted tests that the disclosure always plays, opt-outs are honoured, emergencies escalate, and no PHI is left on voicemail; review a sample of real calls weekly with consent.
This is a general engineering checklist, not legal advice; the clinic should have counsel review it for its states.
Further reading
- bookDaniel Jurafsky and James H. Martin, Speech and Language Processing, 3rd ed. draft (chapters on ASR, TTS and dialogue)
- paperAlexandre Défossez et al., Moshi: a speech-text foundation model for real-time dialogue (2024)
- paperTanya Stivers et al., Universals and cultural variation in turn-taking in conversation (2009)
- docsJ. Rosenberg et al., RFC 3261: SIP: Session Initiation Protocol (2002)
Go further
Sources
- Wilson Sonsini, FCC Rules AI-Generated Voices Are 'Artificial' Under the TCPA (FCC 24-17, released February 8, 2024)
- IETF RFC 3261, SIP
- W3C, WebRTC: Real-Time Communication in Browsers
- Stivers et al., Universals and cultural variation in turn-taking in conversation (PNAS)
- Défossez et al., Moshi (arXiv)
Fact-checked and last reviewed 2026-11-05