Dev tools

How to Build an AI Voice Agent With Twilio and n8n

Build an AI voice agent that answers phone calls with Twilio and n8n: the call flow, the n8n workflow, memory, human handoff, costs and testing.

By OmniAI Editorial · Nov 19, 2026 · 10 min read

You can build an AI voice agent that answers your business phone line without writing a full application. Twilio gives you a phone number and turns speech into text and text back into speech. n8n runs the workflow in between: it receives what the caller said, asks a language model for a reply, checks your data and sends the answer back to the call. The result is a phone AI agent that can answer common questions, take messages and pass difficult calls to a person.

This guide walks through two ways to connect the pieces: a simple turn-by-turn setup that works entirely inside n8n, and a faster, more natural setup using Twilio ConversationRelay. If you want the bigger picture first, our guide to AI voice agents explains what they are and where they work well. If you've never used n8n, start with our n8n tutorial.

What You Need to Build an AI Voice Agent

Before you start, set up:

  • A Twilio account and a phone number that can receive voice calls.
  • An n8n instance reachable from the internet over HTTPS, either n8n Cloud or a self-hosted server with a public address.
  • An API key for a language model provider, such as OpenAI, Anthropic or Google, connected in n8n's credentials.
  • Your business knowledge: opening hours, services, prices, policies and the questions callers ask most.
  • A clear job for the agent, for example "answer questions about bookings and take a message for anything else".

A narrow job is the most important item on that list. An agent that does three things well is far more useful than one that tries to handle every possible call.

How the Call Flow Works

Every phone AI agent follows the same loop:

  1. The caller speaks. Twilio captures the audio.
  2. Speech-to-text turns the audio into text.
  3. Your workflow decides what to say. n8n sends the text, the conversation so far and your instructions to the language model, which may also look things up through tools.
  4. Text-to-speech turns the reply into audio, which Twilio plays to the caller.
  5. Repeat until the call ends or is handed to a person.

The two approaches in this guide differ in how steps 1, 2 and 4 happen, and that difference decides how natural the conversation feels.

A grey phone handset on the left, an arrow to a grey cloud, an arrow to a row of grey workflow boxes with one orange box, and an arrow back to the handset.
Twilio handles the phone call and speech; n8n decides what the agent says.

Option 1: Build an AI Voice Agent With Twilio Gather and n8n

The simplest approach uses Twilio's <Gather> instruction with speech input. Twilio listens until the caller pauses, transcribes what they said and sends the text to your n8n webhook as a normal web request. n8n replies with instructions telling Twilio what to say next and to listen again. Everything happens over plain HTTP, so you don't need any extra servers.

Step 1: Create the incoming-call workflow

In n8n, create a workflow that starts with a Webhook node using the POST method, and set it to respond with a Respond to Webhook node. This first workflow greets the caller and starts listening. The Respond to Webhook node returns TwiML, Twilio's XML format for call instructions, with the content type text/xml:

<Response>
  <Gather input="speech" speechTimeout="auto" action="https://your-n8n.example.com/webhook/voice-turn" method="POST">
    <Say>Thanks for calling. How can I help you today?</Say>
  </Gather>
  <Say>Sorry, I didn't hear anything. Please call back any time. Goodbye.</Say>
</Response>

speechTimeout="auto" tells Twilio to stop listening at the first natural pause. The action URL is where Twilio sends the transcription. If the caller says nothing, Twilio moves on to the next instruction after <Gather>, here a polite goodbye.

Step 2: Point your Twilio number at n8n

In the Twilio console, open your phone number's voice settings and set the incoming call webhook to the production URL of your first n8n workflow, using HTTP POST. Activate the workflow in n8n so the production URL works.

Step 3: Handle each turn of the conversation

Create a second workflow with a Webhook node at the voice-turn path. When Twilio calls it, the request includes:

  • SpeechResult: the text of what the caller said.
  • CallSid: a unique ID for the call, which you'll use to keep the conversation together.
  • From: the caller's number, useful for looking up existing customers.

Connect the webhook to an AI Agent node. Give it a system prompt that describes the job, the tone and the rules, for example:

You answer phone calls for a dental clinic. Keep every answer under two sentences, because the caller is listening, not reading. Answer questions about opening hours, services and booking. If someone describes an emergency, tell them to call emergency services. If you can't help, offer to take a message.

Attach a chat model, and add a Simple Memory node with the session key set to the CallSid, so the agent remembers earlier turns in the same call.

Step 4: Send the reply back to the caller

Finish the turn workflow with a Respond to Webhook node that returns the agent's answer inside a new <Gather>, so Twilio speaks the reply and then listens for the next thing the caller says:

<Response>
  <Gather input="speech" speechTimeout="auto" action="https://your-n8n.example.com/webhook/voice-turn" method="POST">
    <Say>{{ $json.output }}</Say>
  </Gather>
</Response>

The reply is inserted into XML, so make sure characters such as & and < in the model's answer are escaped, or ask the model to avoid them. A short Code node can do the escaping.

Strengths and limits of this approach

The Gather approach is quick to build and easy to debug, because every turn is a visible execution in n8n. The trade-off is speed: each turn waits for the caller to pause, then for transcription, the model and speech generation. Callers notice the gaps, and they can't easily interrupt the agent mid-sentence. It's ideal for prototypes, internal lines and simple after-hours answering.

A grey phone handset and a grey workflow box connected by two curved arrows forming a loop, with an orange dot on the top arrow.
Each turn is a loop: the caller speaks, n8n answers, and Twilio listens again.

Option 2: Twilio ConversationRelay for Natural Conversations

Twilio ConversationRelay is built for real-time voice AI. You connect the call to it with a TwiML instruction like this:

<Response>
  <Connect>
    <ConversationRelay url="wss://your-relay.example.com/websocket" welcomeGreeting="Hi! How can I help?" />
  </Connect>
</Response>

Twilio then handles speech-to-text, text-to-speech, turn detection and interruptions, and keeps a WebSocket connection open to your server. Your server receives the caller's words as text messages and sends reply text back, which Twilio speaks. Because replies can be streamed piece by piece, the agent starts talking sooner, and callers can interrupt it naturally. You can choose speech providers and voices, and set languages per call.

Where n8n fits

n8n's Webhook node handles ordinary web requests, not long-lived WebSocket connections. To use n8n with ConversationRelay, add a small relay service: it holds the WebSocket connection with Twilio, forwards each caller message to an n8n webhook, and passes n8n's reply back to Twilio. That keeps your conversation logic, tools and integrations in n8n, while the relay deals with the real-time connection.

The relay is a short program in a language such as Node.js or Python, and it needs hosting with a secure wss:// address. If you'd rather not run code, dedicated voice agent platforms offer a similar experience with less setup, at the cost of less control.

Which option should you choose?

  • Start with Gather to prove the idea, test your prompts and see what callers actually ask.
  • Move to ConversationRelay when the agent handles real customers and speed and interruptions matter.

Giving Your n8n Voice Agent Useful Tools

An n8n voice agent becomes genuinely helpful when it can do things, not just talk. In n8n, connect tools to the AI Agent node, for example:

  • Look up a customer by the caller's number in your CRM.
  • Check availability in a calendar and offer free slots.
  • Create a booking or a support ticket.
  • Take a message and post it to a team chat or email.
  • Search a knowledge base of policies and frequently asked questions.

Describe each tool clearly so the model knows when to use it, and ask for confirmation before anything changes data, such as "Shall I book you in for Tuesday at 10?". Our guide to building an AI receptionist covers which tasks are worth automating first.

Handing Calls to a Human

Every phone AI agent needs a way out. Common patterns:

  • Transfer the call to a person using Twilio's <Dial> instruction when the caller asks for one or the agent can't help.
  • Take a message with the caller's name, number and reason, and send it to the right person.
  • Escalate by topic: complaints, emergencies, payments or anything sensitive go straight to a human.

Write these rules into the system prompt, and also enforce them in the workflow, for example with an IF node that checks for a transfer request, rather than relying on the model alone.

A grey workflow box with an arrow splitting in two: one branch leads to a grey speech bubble, the other to a grey headset with an orange microphone.
Good voice agents know when to hand the call to a person.

Writing Prompts for the Phone

Voice changes how you write prompts:

  • Keep answers short. One or two sentences per turn; callers can't skim.
  • Avoid lists, links and symbols. They sound strange when read aloud.
  • Spell out numbers and times naturally, such as "half past nine" instead of "09:30".
  • Confirm important details by repeating them back: names, dates and phone numbers.
  • Plan for mishearing. Speech-to-text makes mistakes, so tell the agent to ask again when something is unclear.

Costs to Expect

Running a voice agent involves several separate charges:

  • Twilio: a monthly fee for the phone number and per-minute charges for calls, plus charges for speech recognition or ConversationRelay usage.
  • Language model: charged per token, so long calls and long system prompts cost more.
  • n8n: your n8n Cloud plan, or your server costs if you self-host.

Check each provider's current pricing and test with realistic calls to estimate your cost per minute. Keeping the system prompt short and choosing a fast, smaller model often reduces both cost and delay.

Testing and Going Live

  • Call it yourself many times, with different accents, background noise and unusual questions.
  • Read the transcripts in n8n's execution history to see where the agent misunderstands or rambles.
  • Tell callers they are speaking to an AI at the start of the call, and offer a person.
  • Check the rules where you operate on recording calls, storing transcripts and using AI with customers.
  • Start with limited hours, such as after-hours only, before handling all calls.

FAQ

Can I build an AI voice agent without coding?

Yes, for a basic version. Using Twilio's Gather instruction with speech input and n8n's webhook and AI Agent nodes, you can build a working agent without writing code, apart from a little XML. A more natural real-time agent with ConversationRelay needs a small relay service.

What is Twilio ConversationRelay?

ConversationRelay is a Twilio feature that connects a phone call to your application over a WebSocket. Twilio handles speech recognition, speech generation, turn detection and interruptions, and your server only exchanges text with it.

How do I keep conversation memory during a call?

Use the call's unique CallSid as the session key in your memory node. Each turn of the same call then shares the conversation history, while different calls stay separate.

Why does my voice agent feel slow?

Each turn waits for the caller to finish, then for transcription, the language model and speech generation. Use short prompts, a fast model and brief replies, and consider ConversationRelay, which streams replies and supports interruptions.

Can the AI voice agent transfer calls to a person?

Yes. When the agent decides a person is needed, your workflow can return TwiML with a Dial instruction to forward the call, or take a message and notify your team.

All articles

Read next

Language

EnglishEnglish · en TürkçeTurkish · tr DeutschGerman · de العربيةArabic · ar

Account

Enter your email. We’ll send you a sign-in link and a 6-digit code. No password needed.

Sign in

Theme

Language

Show our email address

To keep spam out of our inbox, please confirm your own email first. We will send you a 6-digit code.

We send codes only to these providers: gmail.com, googlemail.com, outlook.com, hotmail.com, live.com, msn.com, proton.me, protonmail.com, pm.me, icloud.com, me.com, mac.com, yahoo.com, aol.com, gmx.de, gmx.net, web.de, t-online.de, yandex.com, omniai.blog