Pick a depth. Each prompt opens in your AI pre-loaded with the lesson. Click a row to preview the prompt.
You cannot optimize a voice pipeline's latency by feel — you need timestamps at every stage boundary, recorded on every real turn, not just in a benchmark script you run once. The moment you ship past a demo, network conditions, provider load, and user speech patterns will all vary in ways that make 'it felt fast when I tried it' worthless as evidence. This task builds the single most useful piece of infrastructure in this entire course: a lightweight timing harness that stamps every stage transition and reports the numbers that actually matter — not just an average, but the p95, because your worst 5% of turns are what users remember and complain about. Every later module's latency work assumes you already have this instrumentation in place.
Below, a TurnTimer records stage timestamps for several simulated turns, then reports mean and p95 for each stage — the two numbers you should be pulling from real production logs before you trust any latency claim.
import random
import statistics
class TurnTimer:
def __init__(self):
self.records: list[dict[str, float]] = []
def record_turn(self, asr_ms: float, llm_ms: float, tts_ms: float) -> None:
self.records.append({"asr": asr_ms, "llm": llm_ms, "tts": tts_ms})
def stats(self, stage: str) -> dict[str, float]:
values = sorted(r[stage] for r in self.records)
mean = statistics.mean(values)
p95_index = min(len(values) - 1, int(len(values) * 0.95))
return {"mean": mean, "p95": values[p95_index], "n": len(values)}
random.seed(7)
timer = TurnTimer()
for _ in range(200):
# simulate realistic jitter: mostly fast, occasional slow tail
asr = random.gauss(180, 30) if random.random() > 0.05 else random.gauss(500, 60)
llm = random.gauss(220, 40) if random.random() > 0.08 else random.gauss(900, 100)
tts = random.gauss(150, 25) if random.random() > 0.05 else random.gauss(400, 50)
timer.record_turn(asr, llm, tts)
for stage in ("asr", "llm", "tts"):
s = timer.stats(stage)
print(f"{stage:<4} mean={s['mean']:.0f}ms p95={s['p95']:.0f}ms n={s['n']}")python3 main.py