Overview
Twilio provides the telephony infrastructure for Agentic AI, enabling both outbound and inbound voice calls. The integration uses two main Twilio services:- Twilio REST API - Initiates outbound calls and manages call state
- Twilio Media Streams - Bidirectional audio streaming via WebSocket
Architecture
The Twilio integration connects phone calls to the AI voice agent:Getting Twilio Credentials
Sign up for Twilio
Go to console.twilio.com and create a free account. You’ll get $15 in trial credit.
Find your credentials
From the Twilio Console dashboard:
- Copy your Account SID (starts with
AC...) - Copy your Auth Token (click to reveal)
- Note these down for your
.envfile
Configuration
Add your Twilio credentials to.env:
.env
config.yaml references these variables:
config.yaml
Setting Up Webhooks
Twilio needs a public URL to reach your server. Use ngrok or another tunnel:Configure incoming calls
In the Twilio Console:
- Go to Phone Numbers → Manage → Active numbers
- Click your phone number
- Under “Voice & Fax” → “A call comes in”:
- Select Webhook
- Enter:
https://your-tunnel-url.ngrok.io/twilio/voice - Method: HTTP POST
- Click Save
Making Outbound Calls
Initiate calls using the CLI or REST API:Using the CLI
Using Python
Media Streams Protocol
Twilio Media Streams uses WebSocket for bidirectional audio:Audio Format
- Codec: μ-law (G.711)
- Sample rate: 8 kHz
- Encoding: Base64
- Chunk size: 20ms (160 bytes)
Event Flow
start
Stream metadata received:
streamSid- Stream identifiercallSid- Call identifiertracks- Audio tracks (inbound/outbound)
Implementation Reference
The WebSocket handler is insrc/agenticai/twilio/websocket.py:36:
Audio Conversion
Agentic AI converts between Twilio’s μ-law format and PCM for AI processing:- Twilio → AI: μ-law 8kHz → PCM 24kHz
- AI → Twilio: PCM 24kHz → μ-law 8kHz
src/agenticai/audio/converter.py:1.
API Reference
TwilioClient
Location:src/agenticai/twilio/client.py:10
TwilioMediaStreamHandler
Location:src/agenticai/twilio/websocket.py:36
Troubleshooting
No audio from caller
Check WebSocket connection
Check WebSocket connection
Verify the Media Stream WebSocket is connecting:You should see:
Verify webhook URL
Verify webhook URL
- Ensure ngrok/tunnel is running
- Check Twilio Console webhook configuration
- Test webhook URL responds:
curl https://your-url.ngrok.io/twilio/voice
No audio to caller
Check audio conversion
Check audio conversion
Audio must be converted to μ-law 8kHz for Twilio:Should see:
Call not connecting
Verify credentials
Verify credentials
- Check Account SID starts with
AC - Verify Auth Token is correct
- Ensure phone number includes country code:
+1for US
Check trial limitations
Check trial limitations
Twilio trial accounts can only call verified numbers:
- Go to Phone Numbers → Verified Caller IDs
- Add the number you want to call
- Complete verification via SMS or voice call
Rate Limits
Free Trial
- $15.50 credit included
- ~$0.0085/min for voice calls
- Can only call verified phone numbers
- Calls include trial disclaimer message
Paid Account
- ~$1/month per phone number
- ~$0.0085/min for voice calls (US)
- No verification required for outbound calls
- No trial disclaimer
Next Steps
OpenAI Realtime
Set up AI voice processing
Making Calls
Learn to initiate outbound calls
Receiving Calls
Configure inbound call handling
Tunnel Setup
Configure ngrok for webhooks