Sub topic 3
Where the Time Goes: A Latency Budget
Assume you know basic Python, file handling, API calls, and environment variables. A voice application usually uses a cascaded pipeline: speech-to-text (STT), a language model, and text-to-speech (TTS). Each stage adds delay.
A useful budget measures each stage separately:
| Stage | Typical source of delay |
|---|---|
| Upload | File size and network speed |
| STT | Audio duration and provider processing |
| LLM | Prompt size and generated token count |
| TTS | Text length and audio generation |
| Playback | Buffering and audio decoding |
Start playback as soon as safe audio data arrives. Streaming TTS reduces perceived latency because the user does not wait for the complete file. Short replies, smaller prompts, nearby regions, and compressed audio can also help. Measure from microphone stop to first playable audio, not only total request time.
Errors, Rate Limits, Retries, and Cost
Speech calls can fail because of timeouts, temporary service errors, or rate limits. Retry only transient failures. Use exponential backoff and a maximum attempt count.
import time
from openai import OpenAI, RateLimitError, APITimeoutError
client = OpenAI(timeout=60.0)
def transcribe_with_retry(path, attempts=4):
for attempt in range(attempts):
try:
with open(path, "rb") as audio:
return client.audio.transcriptions.create(
model="whisper-1", file=audio
).text
except (RateLimitError, APITimeoutError):
if attempt == attempts - 1:
raise
time.sleep(2 ** attempt)Track audio minutes, input tokens, output tokens, and generated speech. Set limits so one user or failed loop cannot create an unexpected bill.
Measuring Accuracy with Word Error Rate
Word error rate (WER) compares a reference transcript with the generated transcript. It counts substitutions, deletions, and insertions, divided by the reference word count.
import jiwer
reference = "schedule the review for Friday morning"
hypothesis = "schedule the review for Friday mourning"
print(f"WER: {jiwer.wer(reference, hypothesis):.3f}")Evaluate a small set that includes accents, noise, names, and domain terms. Lower WER is useful, but task success matters too.
Privacy and Untrusted Transcripts
Obtain consent before recording. Tell users when speech is synthetic, especially when a voice could be mistaken for a person. Minimize retention and protect audio and transcripts as sensitive data.
Treat transcripts as untrusted input. A speaker can say instructions that attempt to override the system prompt. Keep system instructions separate, validate extracted actions, and require confirmation before consequential operations.
Realtime Models or Cascaded Pipelines?
A cascaded pipeline gives separate control over transcripts, prompts, voices, logging, and retries. It is easier to inspect, but each boundary adds latency.
Realtime speech-to-speech models can reduce turn latency and make interruptions more natural. They may provide less control over intermediate text and can require different monitoring. Choose cascaded components when auditability or exact text matters. Choose realtime processing when natural, low-latency conversation matters most.
Exercise
A reply takes too long to begin. Which measurement should you inspect first?
Show answer
Measure each stage and focus on time from microphone stop to first playable TTS audio.