All Posts
AIVoice AIPipecatTwilioLLM

How I Built an AI Voice Agent That Makes Real Phone Calls (Pipecat + Twilio + Deepgram + OpenAI + ElevenLabs)

Hardik Devmurari· Full-Stack & AI EngineerNovember 23, 20269 min read

Most "AI voice agent" demos are a browser tab and a microphone. I wanted the real thing: a phone that rings, a human who picks up, and an AI that holds a natural conversation, books an appointment, and hangs up politely.

So I built voicelab, a self-hosted voice agent platform, and tested it with real inbound and outbound phone calls. This post explains how the voice engine works, which open-source pieces make it possible, and what I learned about wiring speech-to-text, an LLM and text-to-speech into something that feels like a conversation.


What voicelab Does

voicelab is a small platform, not a one-off script. In one deployment it can:

  • Answer inbound calls on a Twilio number and route them to the right agent based on the number dialed.
  • Place outbound calls, one at a time or as scheduled campaigns across a contact list.
  • Run multiple agents side by side, each with its own persona, voice, phone number and tools.
  • Call tools mid-conversation, such as checking appointment availability and booking a slot.
  • Record and log every call, with a transcript, a recording, cost inputs and a per-stage latency trace.
  • Show it all in a login-protected dashboard built as a React app served by the same FastAPI backend.

The first agent I shipped was a dental front-desk scheduler. You call, it greets you, you ask "What times are open on Friday?", and it answers from real data.


The Voice Pipeline in One Picture

Every voice agent, no matter the vendor, runs the same loop:

Phone call (Twilio)
      │  8 kHz audio over WebSocket (Media Streams)
      ▼
Speech-to-Text (Deepgram)      "What times are open on Friday?"
      ▼
LLM (OpenAI, gpt-4o-mini)      decides what to say, or calls a tool
      ▼
Text-to-Speech (ElevenLabs)    "We have 10 and 2 o'clock open."
      │
      ▼
Phone call (Twilio)            audio streamed back to the caller

Each stage is a separate service. The hard part is not any single stage. It is streaming all of them at once so the caller never sits through dead air.


Why Pipecat Is the Engine

Pipecat is an open-source Python framework for real-time voice and multimodal agents. Instead of writing the glue yourself (audio buffering, interruption handling, streaming between services), you describe the pipeline as an ordered list of processors. Audio and text frames flow through them.

This is the core of voicelab's pipeline, simplified:

stt = DeepgramSTTService(api_key=DEEPGRAM_API_KEY)
llm = OpenAILLMService(api_key=OPENAI_API_KEY, settings=...)
tts = ElevenLabsTTSService(api_key=ELEVENLABS_API_KEY, voice_id=agent.voice_id)

pipeline = Pipeline([
    transport.input(),        # audio from the caller (Twilio WebSocket)
    stt,                      # speech -> text
    user_aggregator,          # decide when the caller has finished their turn
    llm,                      # text -> reply (or a tool call)
    tts,                      # reply -> speech
    transport.output(),       # audio back to the caller
    assistant_aggregator,     # keep conversation history
])

Because every stage streams, the LLM starts producing words before the full answer exists, and the TTS starts speaking before the LLM has finished. That overlap is what makes a call feel live.

The best voice agents are not the ones with the smartest model. They are the ones where every stage starts working before the previous one has finished.


How a Phone Call Becomes a WebSocket

Twilio handles the actual telephone network. When a call connects, my server answers Twilio's webhook with a bit of TwiML that tells Twilio to open a Media Stream, a bidirectional WebSocket carrying the call audio.

  • Inbound: Twilio hits /twilio/inbound. The server looks up which agent owns the dialed number, then returns TwiML that opens the stream.
  • Outbound: the server asks Twilio to place the call. When the person answers, Twilio hits an answer webhook, which returns the same kind of stream TwiML.
  • Security: both webhooks verify Twilio's request signature, and the dashboard sits behind a login.

Phone audio is narrow-band, so the pipeline runs at 8 kHz in and out. That is a real constraint worth remembering: you are not working with studio-quality audio, and your speech-to-text and voice choices need to cope with it.


The Two Details That Make It Feel Human

Knowing when the caller has finished talking

A naive agent waits for silence. That either cuts people off mid-sentence or leaves long awkward pauses. voicelab combines two Pipecat components:

  • Silero VAD (voice activity detection) notices when speech starts and stops, which also powers barge-in, where the caller interrupts the agent.
  • Smart Turn v3 is a small model that judges whether a pause is the end of a thought or just a breath.

Both are tunable per agent: how quickly the agent notices the caller started speaking, and how long a pause it waits before replying. Different use cases want different feels. A scheduling bot can be snappy, while a sensitive conversation should wait longer.

Handling silence and goodbyes

Real calls are messy. People go quiet, put the phone down, or say "okay bye" without waiting. voicelab supports per-agent idle timeouts with gentle reminder messages, an end_call tool the model can use when the conversation wraps up, and phrase-based hang-up as a fallback.


Giving the Agent Tools

A voice agent that only talks is a toy. The useful ones do things. In voicelab each agent has a list of tools it is allowed to use:

  • check_appointment_availability and book_appointment for scheduling
  • end_call to hang up cleanly
  • transfer_call to hand off to a human number
  • Custom API tools you can define from the dashboard (name, URL, method, headers, parameters) with no Python required

The model decides when to call a tool, the tool runs, and the result goes back into the conversation, all while the caller waits for a spoken answer. Prompts also support {{variable}} templating, so an agent can greet a contact by name or reference today's date.


Seeing Inside Every Call

The part I would not skip in any voice project is observability. Pipecat emits metrics for each stage, and voicelab stores them per call:

  • Time to first byte for the STT, the LLM and the TTS
  • Time to first audio the caller actually hears
  • Usage totals: seconds of audio transcribed, LLM prompt and completion tokens, TTS characters
  • A recording, so you can listen back to what the caller actually experienced

On the dashboard, each call shows a latency trace as a bar chart and an estimated cost broken down by Twilio, STT, LLM and TTS. When a call feels slow, you can see exactly which stage was responsible. I cover those numbers in a follow-up post.


Self-Hosted vs a Hosted Platform

Hosted platforms such as Vapi or Bolna are great for getting started. I built voicelab self-hosted because I wanted:

  • Full code ownership, with no per-minute platform markup on top of the provider costs
  • Single-tenant deployments that run inside a client's own box
  • Control over every knob: the models, the turn-taking, the call rules and the data

The whole thing deploys with Docker Compose behind HTTPS, with call history stored in a local database. The trade-off is honest: you own the operations too. Webhook security, concurrency limits against your provider plans and cost caps all become your responsibility.


What I Would Tell Someone Starting Today

  • Start with a phone call, not a web demo. Telephony audio is the hard mode, so solve it first.
  • Stream everything. Never wait for a full transcript or a full LLM answer.
  • Tune turn-taking early. It matters more to perceived quality than the model you pick.
  • Log latency per stage from day one. You cannot optimise what you cannot see.
  • Put hard limits on calls. A maximum duration per call stops a stuck conversation from quietly running up a bill.

In the next posts I break down what each stage costs and how long it takes, and how the outbound campaign engine decides who to call and when. If you are building something similar, I would love to hear what you are working on.


Hardik Devmurari is a full-stack and AI engineer at JRHMSquare, a software development collective specialising in full-stack, mobile and AI products.

Building something?

We are a team of full-stack, mobile, and AI engineers who ship production products. Let's talk about your project.

Get in touch