GEMINI LABJP
NANOLITE — Nano Banana 2 Lite is here: Google's fastest and most cost-efficient Gemini Image model, made for running lightweight image generation cheaplyOMNIFLASH — Gemini Omni Flash is in public preview, a natively multimodal model that lets enterprises and developers build custom, dynamic video workflowsAGENTS — Managed Agents expand with background: true for async server-side runs and polling, remote MCP server integration, and refreshing credentials across interactionsMEMORY — The Memory Bank IngestEvents API is generally available, decoupling event ingestion from memory generation so you can stream content continuouslyTHROUGHPUT — Provisioned Throughput now lets you submit up to seven pending orders for the same model and regionDEPRECATE — Image generation models shut down on August 17, and the Grok 4.1 family on the Gemini Enterprise Agent Platform on August 20NANOLITE — Nano Banana 2 Lite is here: Google's fastest and most cost-efficient Gemini Image model, made for running lightweight image generation cheaplyOMNIFLASH — Gemini Omni Flash is in public preview, a natively multimodal model that lets enterprises and developers build custom, dynamic video workflowsAGENTS — Managed Agents expand with background: true for async server-side runs and polling, remote MCP server integration, and refreshing credentials across interactionsMEMORY — The Memory Bank IngestEvents API is generally available, decoupling event ingestion from memory generation so you can stream content continuouslyTHROUGHPUT — Provisioned Throughput now lets you submit up to seven pending orders for the same model and regionDEPRECATE — Image generation models shut down on August 17, and the Grok 4.1 family on the Gemini Enterprise Agent Platform on August 20
Articles/API / SDK
API / SDK/2026-04-30Advanced

Putting an AI That Answers Phones Into Production: Building a Phone Voice Agent With Gemini Live API and Twilio Media Streams

Bridge Twilio Voice and Gemini Live API over WebSocket to build a phone-answering AI agent that holds up in production. Full code, interruption handling, function calling, deployment notes, and per-minute cost math.

gemini-live-apitwiliovoice-agent2websocket4fastapi5production140media-streams

Premium Article

When most people hear "an AI that answers phones," they picture an evolved IVR — read a menu, press a digit, branch on input. With Gemini Live API in the picture, the next level becomes feasible for indie developers: a voice agent that takes natural reservations, writes to the back office through Function Calling, and escalates to a human when it should.

I tried this for a small shop that wanted after-hours calls handled by AI, and quickly learned that the browser WebSocket demo and a real phone line are practically different problems. The wall between 8kHz μ-law and 16kHz PCM, the base64-wrapped JSON frames Twilio Media Streams demands, and the awkward art of interruption — none of that is solved by the official examples alone.

This guide takes that experience and walks through bridging Twilio Voice into Gemini Live API to build a phone-answering agent that holds up in production. Code is given in full working form, with error handling, interruption control, common pitfalls, and per-minute cost math. It assumes you are an intermediate-to-advanced developer comfortable with the basics of Gemini Live API.

Prerequisite reading: see Gemini Live API: WebSocket and Ephemeral Tokens for Production for token mechanics, and The Complete Gemini Live API Guide for the basics.

Why a Browser Voice Demo Doesn't Just "Drop In" to a Phone Line

Before any code, it helps to know why phone lines are special. Skipping this step leads to the most common dead end: "the mic worked but the phone returns nothing but noise."

The PSTN is effectively standardised on 8kHz sampling, 8-bit μ-law (u-law) compression, mono. That spec dates to the 1970s and is tuned to deliver intelligible speech in a remarkably narrow band. Gemini Live API, on the other hand, expects 16kHz sampling, 16-bit linear PCM, mono. Sample rate is doubled, bit depth is doubled, and the compression scheme is different too.

Twilio Media Streams ships audio not as raw bytes but as base64-wrapped JSON frames over WebSocket. Each frame carries an event field, with start, media, mark, stop states you must handle. The reverse path (AI to user) uses the same shape.

So your relay's real job is roughly four things:

  • Decode Twilio's base64+μ-law and upsample it to 16kHz PCM
  • Stream that into Gemini Live API as binary
  • Take Gemini's 24kHz PCM responses, downsample to 8kHz μ-law, and base64-encode it
  • Send those frames back to Twilio over the WebSocket

This is not "a transparent WebSocket relay." Building the shortest path to a working version of all of that is the point of this article.

Architecture at a Glance — Three Components

Three pieces make up the system in this guide:

  • Twilio Voice: rents the phone number and routes the inbound call to your WebSocket via Media Streams. Behaviour at call time is controlled by a small XML format called TwiML.
  • Relay server (FastAPI + WebSocket): bridges Twilio and Gemini. It does the format conversion, interruption control, and Function Calling glue. We'll target Cloud Run for deployment.
  • Gemini Live API: the brain — takes audio in, produces audio out, and signals interruptions and tool calls.

The data flow is:

  1. User dials in. Twilio hits your Voice URL webhook.
  2. The relay returns TwiML that says <Connect><Stream url="wss://.../media">.
  3. Twilio opens a WebSocket to your relay and starts streaming the caller's audio.
  4. The relay opens a WebSocket to Gemini Live API and starts a two-way bridge.
  5. Gemini's audio replies flow back through the relay to Twilio and into the caller's ear.
  6. On hangup Twilio sends stop; the relay closes the Gemini session.

That's the lay of the land. We'll implement it piece by piece.

Thank you for reading this far.

Continue Reading

What follows includes implementation code, benchmarks, and practical content we hope you'll find useful. This site runs without ads — server and development costs are supported entirely by members like you. If it's been helpful, we'd be truly grateful for your support.

WHAT YOU'LL LEARN
Engineers stuck moving a browser voice demo onto real phone lines will leave with a working bridge that handles μ-law 8kHz to PCM 16kHz in both directions
You'll learn how to bridge Twilio Media Streams and Gemini Live API in a FastAPI WebSocket relay, with concrete patterns for interruption and hangup
Indie builders aiming to automate phone reception for bookings or first-line support can leave with a deployment plan, cost estimates, and a path to production on Cloud Run
Secure payment via Stripe · Cancel anytime

Unlock This Article

Get full access to the rest of this article. Buy once, read anytime. This site is ad-free — your support goes directly toward keeping it running.

or
Unlock all articles with Membership →
Share

Thank You for Reading

Gemini Lab is ad-free, supported entirely by members like you. We publish practical guides daily with implementation code, benchmarks, and production-ready patterns. If you've found it useful, we'd love to have you on board.

  • Copy-paste ready implementation code
  • New advanced guides published daily
  • $5/mo or $10 for lifetime access
View Membership →

Related Articles

API / SDK2026-04-15
Designing a Production Prompt Management System for Gemini API — Versioning, A/B Testing, and Canary Rollouts
A complete implementation guide for solving the prompt versioning, attribution, and safety challenges in production Gemini API deployments — using FastAPI, PostgreSQL, Redis, A/B testing, and canary rollouts.
API / SDK2026-03-19
Gemini 2.5 Pro × FastAPI: Building a Production-Ready AI Backend
Learn how to build a production-ready AI backend by combining Gemini 2.5 Pro with FastAPI, covering streaming, rate limiting, Function Calling, cost optimization, and Docker deployment.
Advanced2026-03-17
Production-Grade Voice AI Agent with Gemini Live API & Google ADK [2026]
Build and deploy a production-grade voice AI agent by combining Gemini Live API with Google ADK, Function Calling, WebSocket management, and Cloud Run. Covers architecture design, connection stability, parallel tool execution, and cost optimization.
📚RECOMMENDED BOOKS
Build a Large Language Model (From Scratch)
Sebastian Raschka
LLM Dev
Prompt Engineering for LLMs
Berryman & Ziegler
Prompting
AI Engineering
Chip Huyen
AI Eng
* Contains affiliate links
See all →