Twilio is the leading cloud communications platform, powering phone calls for millions of businesses worldwide. Integrating AI voice assistants with Twilio enables intelligent, automated phone interactions at scale.
This guide covers everything you need to know about building AI-powered voice solutions on Twilio.
Why Twilio for Voice AI?
Twilio's Strengths
| Feature | Benefit |
|---|---|
| Global Reach | Phone numbers in 100+ countries |
| Reliability | 99.95% uptime SLA |
| Scalability | Handle millions of concurrent calls |
| Developer-Friendly | Excellent APIs and documentation |
| Compliance | HIPAA, SOC 2, PCI DSS ready |
Common Use Cases
- Intelligent IVR: Replace "Press 1 for sales" with natural conversation
- Outbound Campaigns: Automated appointment reminders, collections
- Customer Service: 24/7 automated support line
- Lead Qualification: AI-powered sales qualification calls
- Surveys: Voice-based customer feedback collection
Architecture Overview
How It Works
Customer Call → Twilio → Webhook → AI Voice Assistant → Response → Twilio → Customer
Flow:
- Customer dials your Twilio number
- Twilio sends webhook to your AI endpoint
- AI processes speech, generates response
- TTS audio streams back through Twilio
- Conversation continues bidirectionally
Key Components
| Component | Role |
|---|---|
| Twilio Phone Number | Entry point for calls |
| Twilio Voice API | Handles call routing and media |
| Webhooks | Connect Twilio to AI backend |
| AI Voice Engine | Speech recognition + NLU + TTS |
| Integration Layer | Connect to CRM, databases, APIs |
Setting Up Twilio for AI Voice
Step 1: Get a Twilio Phone Number
# Using Twilio CLI
twilio phone-numbers:buy:mobile --country-code US
Or through Twilio Console:
- Go to Phone Numbers > Buy a Number
- Select country and capabilities (Voice)
- Purchase number
Step 2: Configure Voice Webhook
Point your Twilio number to your AI voice endpoint:
// Twilio Console or via API
const accountSid = 'your_account_sid';
const authToken = 'your_auth_token';
const client = require('twilio')(accountSid, authToken);
client.incomingPhoneNumbers('PN...')
.update({
voiceUrl: 'https://your-ai-endpoint.com/voice/incoming',
voiceMethod: 'POST'
});
Step 3: Handle Incoming Calls
Your endpoint receives call data and returns TwiML:
// Express.js example
app.post('/voice/incoming', (req, res) => {
const { CallSid, From, To } = req.body;
// Initialize AI conversation
const twiml = new twilio.twiml.VoiceResponse();
// Stream to AI voice assistant
twiml.connect().stream({
url: 'wss://your-ai-endpoint.com/voice/stream'
});
res.type('text/xml');
res.send(twiml.toString());
});
Twilio Media Streams for Real-Time AI
Why Media Streams?
Traditional Twilio webhooks work for simple IVR, but AI voice assistants need real-time audio:
| Approach | Latency | Best For |
|---|---|---|
| TwiML Gather | 1-3 seconds | Simple IVR |
| Media Streams | <300ms | AI conversation |
Setting Up Media Streams
// Server-side WebSocket handler
const WebSocket = require('ws');
const wss = new WebSocket.Server({ port: 8080 });
wss.on('connection', (ws) => {
console.log('Twilio connected');
ws.on('message', (message) => {
const data = JSON.parse(message);
if (data.event === 'media') {
// Audio payload (base64 mulaw)
const audioChunk = Buffer.from(data.media.payload, 'base64');
// Send to AI for processing
processAudioWithAI(audioChunk);
}
});
});
function sendAudioToTwilio(ws, audioBuffer) {
ws.send(JSON.stringify({
event: 'media',
streamSid: currentStreamSid,
media: {
payload: audioBuffer.toString('base64')
}
}));
}
Audio Format Handling
Twilio uses specific audio formats:
| Direction | Format | Sample Rate |
|---|---|---|
| From Twilio | mulaw | 8000 Hz |
| To Twilio | mulaw | 8000 Hz |
Your AI pipeline must handle conversion:
// Convert AI output to Twilio format
function convertToMulaw(pcmAudio) {
// 16-bit PCM at 16kHz → 8-bit mulaw at 8kHz
const resampled = resample(pcmAudio, 16000, 8000);
return linearToMulaw(resampled);
}
Building Intelligent IVR
Traditional IVR vs AI Voice
Traditional:
"Press 1 for billing. Press 2 for support. Press 3 for sales."
AI-Powered:
AI: "Hi! How can I help you today?"
Customer: "I have a question about my last bill"
AI: "I'd be happy to help with billing. Let me pull up your account..."
Intent Detection
Your AI should recognize common intents:
| Intent | Example Phrases |
|---|---|
| Billing | "bill", "charge", "payment", "invoice" |
| Support | "help", "issue", "problem", "not working" |
| Sales | "buy", "purchase", "pricing", "plans" |
| Account | "account", "login", "password", "profile" |
Routing Logic
async function handleIntent(intent, context) {
switch (intent) {
case 'billing':
return await handleBillingInquiry(context);
case 'support':
return await handleSupportRequest(context);
case 'sales':
return await transferToSales(context);
case 'human':
return await transferToAgent(context);
default:
return await clarifyIntent(context);
}
}
Outbound Calling with AI
Initiating Outbound Calls
// Make an AI-powered outbound call
const call = await client.calls.create({
url: 'https://your-ai-endpoint.com/voice/outbound',
to: '+15551234567',
from: '+15559876543'
});
console.log(`Call SID: ${call.sid}`);
Use Cases for Outbound AI
| Use Case | Implementation |
|---|---|
| Appointment Reminders | Confirm or reschedule appointments |
| Payment Collections | Friendly payment reminders |
| Surveys | Post-service feedback collection |
| Lead Qualification | Initial prospect screening |
| Notifications | Important updates and alerts |
Outbound Best Practices
- TCPA Compliance: Get proper consent before calling
- Time Restrictions: Respect local calling hours
- Caller ID: Use recognizable, valid caller ID
- Opt-Out: Provide easy way to stop calls
- Rate Limiting: Don't overwhelm phone networks
Call Transfer to Human Agents
Warm Transfer
Keep AI context when transferring:
async function warmTransfer(context, agentNumber) {
const twiml = new twilio.twiml.VoiceResponse();
// Brief the agent via whisper
const dial = twiml.dial();
dial.number({
url: '/whisper-briefing'
}, agentNumber);
return twiml;
}
// Whisper endpoint - plays only to agent
app.post('/whisper-briefing', (req, res) => {
const twiml = new twilio.twiml.VoiceResponse();
twiml.say(`Incoming transfer about billing issue.
Customer ID 12345. Account has overdue balance.`);
res.type('text/xml').send(twiml.toString());
});
Cold Transfer
Simple transfer without briefing:
function coldTransfer(agentNumber) {
const twiml = new twilio.twiml.VoiceResponse();
twiml.say('Transferring you to an agent now.');
twiml.dial(agentNumber);
return twiml;
}
Queue Management
For call centers with multiple agents:
function addToQueue(queueName) {
const twiml = new twilio.twiml.VoiceResponse();
twiml.say('Please hold while I connect you to the next available agent.');
twiml.enqueue(queueName);
return twiml;
}
Recording and Analytics
Call Recording
// Enable recording
const call = await client.calls.create({
url: 'https://your-endpoint.com/voice',
to: '+15551234567',
from: '+15559876543',
record: true,
recordingStatusCallback: 'https://your-endpoint.com/recording-status'
});
Call Analytics
Track key metrics:
| Metric | Description |
|---|---|
| Call Duration | Total call length |
| AI Handle Time | Time handled by AI |
| Transfer Rate | % transferred to human |
| Resolution Rate | % resolved by AI |
| CSAT | Customer satisfaction score |
Transcription
Get text transcripts of calls:
// Retrieve transcription
const transcript = await client.recordings(recordingSid)
.transcriptions
.create();
Error Handling
Graceful Degradation
Always have fallbacks:
async function handleCallWithFallback(context) {
try {
return await aiVoiceHandler(context);
} catch (error) {
console.error('AI error:', error);
// Fallback to traditional IVR or human
return fallbackHandler(context);
}
}
function fallbackHandler(context) {
const twiml = new twilio.twiml.VoiceResponse();
twiml.say('I apologize, we\'re experiencing technical difficulties.');
twiml.say('Let me connect you to an agent.');
twiml.dial(SUPPORT_NUMBER);
return twiml;
}
Timeout Handling
// Handle caller silence
const gather = twiml.gather({
input: 'speech',
timeout: 5,
action: '/handle-response'
});
gather.say('Are you still there? How can I help?');
// If no response
twiml.redirect('/timeout-handler');
Scaling Considerations
High Volume Handling
| Volume | Architecture |
|---|---|
| <1000 calls/day | Single server |
| 1000-10000 calls/day | Load balanced |
| >10000 calls/day | Distributed, multi-region |
Twilio Capacity
Request capacity increases for high volume:
- Default: 1 concurrent call per phone number
- Can increase to 100+ per number
- Multiple numbers for more capacity
Cost Optimization
Twilio Pricing Components
| Component | Cost (US) |
|---|---|
| Phone number | $1-2/month |
| Inbound calls | $0.0085/minute |
| Outbound calls | $0.014/minute |
| Recording | $0.0025/minute |
| Transcription | $0.05/minute |
Optimization Strategies
- Reduce Call Duration: Efficient AI conversations
- Minimize Transfers: Resolve more with AI
- Batch Outbound: Optimize dial times
- Regional Numbers: Local numbers are cheaper
- Committed Use: Volume discounts available
Edesy + Twilio Integration
Edesy provides pre-built Twilio integration:
Quick Setup
// Edesy handles the complexity
const edesy = require('@edesy/voice-ai');
edesy.configure({
twilioAccountSid: process.env.TWILIO_SID,
twilioAuthToken: process.env.TWILIO_TOKEN,
phoneNumber: '+15551234567'
});
// AI handles all incoming calls
edesy.handleIncoming({
greeting: 'Thanks for calling. How can I help?',
intents: ['billing', 'support', 'sales'],
transferNumber: '+15559876543'
});
Benefits
- No Audio Pipeline: Edesy handles all audio processing
- Built-in ASR/TTS: Speech recognition and synthesis included
- Intent Recognition: Pre-trained for common business intents
- Analytics Dashboard: Track all call metrics
- Compliance Ready: TCPA, HIPAA, PCI handling
Conclusion
Twilio + AI voice assistants enable powerful automated phone experiences. The combination of Twilio's reliable telephony infrastructure with modern AI creates conversational IVR that actually works.
Key Takeaways:
- Use Media Streams for real-time AI (not just TwiML Gather)
- Handle audio format conversion (mulaw 8kHz)
- Implement graceful fallbacks to humans
- Monitor and optimize for cost and quality
- Consider pre-built solutions like Edesy for faster deployment
Ready to add AI to your Twilio phone lines? Get started with Edesy.
Related Resources
Published: January 2026