Skip to main content
Home
Projects
About
Chat
← back to engineering notes
Nov 28, 2026·5 min read

Interruptible Voice Agents with Gemini Live & 25+ Site Tools

Architecture notes on full-duplex 16 kHz PCM streaming, client-side barge-in, and keeping 25+ live UI tools deterministic across route changes.

#voice-ai#gemini-live#audioworklet#websockets#tool-calling#architecture

Why Turn-Based Voice Feels Broken

Most browser voice demos bolt three request-response stages together: record until silence, send audio to STT, wait for an LLM completion, then stream TTS back. Even with fast models, end-to-end turn latency sits around 1.5 to 3 seconds—and worse, you cannot interrupt the assistant mid-sentence without awkwardly hitting a stop button.

For both this site's live voice agent and Jarvis (my voice-controlled freight dispatch demo), the goal was a phone-call feel: full-duplex audio over a single persistent WebSocket, native voice activity detection (VAD), sub-second turn-taking, and real-time UI actions while the conversation is still happening.

The Audio Pipeline: Worklet to WebSocket and Back

The browser captures microphone input inside an AudioWorklet at 16 kHz mono PCM rather than MediaRecorder. Raw PCM frames avoid container framing overhead and let us stream tiny chunks continuously with predictable latency.

text
Mic -> AudioWorklet (16 kHz PCM16) -> WebSocket -> Gemini Live
                                                       |
Speaker <- Web Audio Playback Queue <- PCM24 frames <--+

Three small details mattered most in practice:

  • One long-lived shell: The AudioContext, microphone stream, and WebSocket live above the route tree. Navigating from / to /projects or /notes never drops the call or tears down audio playback.
  • Instant barge-in flush: The moment Gemini Live emits an interruption signal—or local speech starts over active playback—the client immediately stops scheduled AudioBufferSourceNode nodes and clears the playback queue. Waiting for the current buffer to finish makes interruption feel sluggish.
  • Zero audio before consent: No mic stream opens and no audio leaves the browser until the visitor explicitly starts a voice call.
Barge-in is mostly a playback-queue problem, not a model problem. If your client queues 800 ms of audio ahead without a synchronous cancel path, even a 100 ms model interruption feels broken.

Making 25+ Site Tools Safe and Deterministic

This portfolio exposes more than 25 structured tools to the voice agent: route navigation, opening and closing project cards, controlling preview videos, filling form fields, toggling site preferences, running safe terminal commands, and querying RAG facts via lookup_site_facts.

Giving a real-time voice model that many tools fails quickly unless you enforce strict boundaries:

  • Allowlisted schemas at the edge: Every route target, project slug, form field ID, preference key, and terminal command is validated against explicit static allowlists before execution. Arbitrary paths or shell arguments are rejected outright.
  • Stable prompts and tool declarations: System instructions and tool schemas stay byte-stable across turns so prompt caching remains effective, while ephemeral page state (current route, theme, open modal) rides in a compact session snapshot.
  • Queued UI execution: Tool calls execute through a serialized client action queue so overlapping tool responses (for example, navigating to /guestbook and filling a field) land in order instead of racing React transitions.

Context Hygiene Over Long Calls

Live voice sessions accumulate audio tokens quickly. Keeping transcripts bounded with a sliding context window and grounding factual answers through on-demand lookup_site_facts retrieval—instead of stuffing the entire portfolio into the system prompt—keeps both latency and token burn predictable over multi-minute calls.

Trade-Offs Worth Knowing

  • WebSocket lifecycle vs. serverless: Full-duplex voice wants a stateful connection or a short-lived session token minting endpoint; keeping the control plane minimal lets the browser connect cleanly without proxying every audio frame through heavy middleware.
  • Echo cancellation: Browser AEC (echoCancellation: true, noiseSuppression: true) is non-negotiable on laptop speakers; without it, the agent interrupts itself on its own voice.
  • Network jitter: Exposing a low-network preference and graceful reconnect state matters far more on mobile connections than shaving another 20 ms off desktop audio framing.
Ask about this noteChat

RELATED PROJECT

this sketchbook website

open project card →
older note →Sub-400ms LCP Next.js 16 on 1 GB VMs Behind Cloudflare

Loading...