# A voice agent that shows its work

> A reimplementation of a public-service voice kiosk project using public data. Feed it an FAQ and it becomes a voice agent that shows per-stage latency and per-sentence evidence for every answer.

- Published: 2026-11-11
- Client: Public-service civil kiosk project (reimplemented with public data)
- Role: Design, implementation, measurement, deployment
- Period: 2026-10-21 ~ 2026-10-29
- Stack: Python, FastAPI, Whisper, Supertonic, onnxruntime, BM25, Qwen, SSE, Cloud Run
- Outcome: Time to first audio p50 4986.1 → 871.1 ms (local, LLM stub, 50 questions × 3 runs)
- Tags: voice, rag, latency, tts, stt

**Why**: In voice, how soon the first sound plays decides how fast it feels, yet most builders hide per-stage timing and where the answer came from.

**Principle**: Distrust the numbers that look good first, and publish the ones that got worse in the same table.

## Metrics

| Metrics | Value | Scope | Source |
| --- | --- | --- | --- |
| Time to first audio p50 (LLM stub) | 4986.1 → 871.1 ms | Reproducible measurement | ./bench/run.sh --config before·after, ./bench/summarize.sh (local CPU, 50 questions × 3 runs, 2026-10) |
| Time to first audio p50 / p95 (Qwen summarization) | 4135.6 / 6399.3 → 2121.2 / 3100.2 ms | Reproducible measurement | ./bench/run.sh with LLM_BACKEND=qwen, bench/results/summary.md (API key required) |
| Colloquial question retrieval top1 (BM25) | 0.289 | Reproducible measurement | uv run python scripts/eval_colloquial.py --faq data/faq/faq.jsonl --eval tests/fixtures/faq_eval_colloquial.jsonl |
| Public server synthesis time per answer, speak_ms | 15.1 s → 6.2 s → 4.4~4.7 s | Production measurement | Same question, 1 run, on the Cloud Run public demo, docs/deploy.md section 11 (2026-10-29) |
| Original environment total voice response time | 10.52 s → 7.40 s | Original project | Development log (private). Different hardware and models, not reproducible |

## Links

- Demo: https://voice-agent-1072416881206.asia-northeast3.run.app
- Repository: https://github.com/ilyos-numon/voice-agent

## 1. Situation

The starting point was a civil-service voice kiosk I worked on in practice. A citizen asks a question out loud, and the kiosk answers out loud, grounded in a civil-service FAQ. I rebuilt this from scratch on a public civil-service FAQ, then widened it into "a transparent voice agent builder that turns an FAQ into an agent."

There were three problems to solve. The first was latency. In voice, when the first sound plays decides how the system feels, and typical builders don't show per-stage timing. The second was hallucination. In domains like civil services, hospitals, or internal company rules, an ungrounded answer is unusable. Every sentence should show its evidence, and with no evidence the agent should say it doesn't know. The third was quality checking. I wanted a number that tells whether an agent built from my documents is usable, without speaking to it dozens of times.

The constraints were clear too. It had to run on a CPU machine with no GPU (16 GB RAM), and audio never leaves the machine.

## 2. Scoping

I left out PDF and URL input, barge-in and automatic turn detection, an embeddable widget, sign-up, and multilingual support. Kiosk screen integration was cut as well. Instead, a measurement tool that "runs the same question set before and after, side by side" got the same weight as the features.

Retrieval started with BM25 alone. At 575 entries, retrieval latency was not a measured bottleneck, and the accuracy ceiling was exposed as a number and deferred to the next stage. The public demo enforces per-visitor and per-day limits inside a single process, with no external accounts or Redis.

## 3. Build

| Category | What | Why |
| --- | --- | --- |
| Adopted | Whisper (STT), Supertonic (TTS), onnxruntime, BM25, Qwen summarization, Cloud Run | A combination that runs on CPU and keeps audio local |
| Fixed | Pinned onnxruntime thread count, container host binding, non-root execution permissions | Problems that actually occurred in the public deployment |
| Built myself | Sentence-level SSE streaming, early split of the first chunk, a leak metric and a colloquial eval set, tuning of the don't-know threshold, a report card, dependency-free rate limiting and bot blocking | Nothing off the shelf fit what I needed to measure or how to defend a public service |

Answers are split into sentences (merged under 10 characters, split over 150 characters), and each sentence is sent over SSE as a complete WAV. WebSocket and PCM chunk streaming were compared and rejected. For rate limiting, I compared slowapi and fastapi-limiter, then wrote my own to fit the single-process constraint.

## 4. What broke

**Doubting an accuracy of 1.000.** On a rule-generated eval set, top1 came out at 1.000. My hypothesis was that the questions contained the indexed topic names almost verbatim. I defined a leak metric for how much of the correct topic name's text leaks into the question, and it measured 0.815. I wrote 46 colloquial questions from briefs that hid the topic names, and froze them in a commit before measuring. The result was top1 0.289 (leak 0.210), and I published that number as is. The report card always carries a note that this is not real-world accuracy.

**Sentence streaming alone was not enough.** Once Qwen summarization was added, "start synthesis whenever the LLM finishes a sentence" put time to first audio p50 at 2755 ms, over the 2200 ms budget. For short answers, the first sentence only finished right before the LLM did, leaving nothing to overlap. I made only the first chunk split early, after a comma or connective ending once it reached 15 characters or more, and p50 dropped to 2121.2 ms.

**A bigger CPU was not the answer.** On the public demo, synthesizing one answer took 15.1 s with long gaps between sentences. The cause was onnxruntime's default thread count over-allocating on 2 vCPU. I pinned threads to 2 and switched to gen2. I also measured CPU 4 myself: 4 threads were about 2x slower than 2 threads, and CPU 4 + 2 threads matched CPU 2. I went back to CPU 2. Early splitting made the first sentence sound cut off mid-way, so I turned it off on the public demo and compensated by adding "first sentence around 30 characters" to the prompt.

## 5. Results

### Reimplementation

| Metric | Value | Scope | Source |
| --- | --- | --- | --- |
| Time to first audio p50 / p95 (LLM stub) | 4986.1 → 871.1 / 8220.0 → 2214.4 ms | Local CPU, 50 questions × 3 runs | `./bench/run.sh`, `./bench/summarize.sh` |
| Total response p50 (LLM stub) | 4986.1 → 5402.4 ms | Same as above. Grew because of per-sentence synthesis | Same as above |
| Time to first audio p50 / p95 (Qwen summarization) | 4135.6 / 6399.3 → 2121.2 / 3100.2 ms | Local, 50 questions × 3 runs, errors 0/150 | `bench/results/summary.md` |
| Colloquial retrieval top1 | 0.289 | 575 public FAQ entries, 46 questions | `scripts/eval_colloquial.py` |
| Don't-know threshold 0.20 | Answerable rows kept 11/11, out-of-scope caught 7/8 | civil-kiosk preset | `scripts/tune_unknown.py` |
| Public server speak_ms | 15.1 s → 6.2 s → 4.4~4.7 s | Cloud Run, same question | `docs/deploy.md` section 11 |
| Time to first audio, final public config | 4.4~5.2 s (synthesis ratio 0.41~0.48x) | Cloud Run, CPU 2, threads 2 | Development log (private) |

The longer total response is the price of bringing the first sound forward, and it stays in the same table.

### Original environment

| Metric | Before | After |
| --- | --- | --- |
| Total voice response time | 10.52 s | 7.40 s |
| Average TTS response time | 4.61 s | 2.35 s |

Different hardware, models, and real-user questions, so these cannot be compared with the reimplementation table above.

## 6. If I did it again

Time to first audio in the public environment (4.4~5.2 s) is slower than the local Qwen measurement (p50 2121.2 ms). That is the cost of turning off early splitting for unbroken intonation. I would cut the number of TTS steps and compare faster LLM candidates on the same bench.

Colloquial top1 of 0.289 comes from failing to connect synonyms (like "day off" and "closed"). I would compare morphological analysis and an embedding hybrid before and after on the same set. The eval questions were written by AI and the audio is synthetic too, so the first step is replacing them with a set of consented human utterances.

Evidence checking uses bigram overlap, so it can miss paraphrases with the same meaning. With 1 instance and in-memory state, redeploying wipes the rate-limit counters, and cold start time has not been measured yet. I would move the counters to shared storage and then consider horizontal scaling.
