Build Your First Voice Agent using Twilio
Overview
This guide shows you how to build a real-time voice agent that answers phone calls. It uses Twilio for telephony, Pipecat to orchestrate the real-time audio pipeline, and Sarvam AI for speech-to-text, the LLM, and text-to-speech. It’s a great starting point for IVR replacements, customer support lines, and conversational phone agents in Indian languages.
What You’ll Build
A voice agent that can:
- Answer inbound phone calls on a Twilio number
- Listen to callers speaking, in multiple Indian languages
- Understand and process what they say
- Respond back in a natural-sounding voice, over the phone
Quick Overview
- Get API keys (Twilio, Sarvam)
- Install packages:
pip install "pipecat-ai[websocket,sarvam]" python-dotenv - Create a
.envfile with your API keys - Write about 80 lines of Python code
- Point a Twilio phone number at your agent using TwiML
- Call your Twilio number
Quick Start
1. Prerequisites
- Python 3.9 or higher
- ngrok, to expose your local server during development
- Accounts and keys from:
2. Install Dependencies
macOS/Linux
Windows
3. Create Environment File
Create a file named .env in your project folder:
Then replace each placeholder with your real credentials from the Sarvam dashboard and the Twilio Console.
TWILIO_ACCOUNT_SID and TWILIO_AUTH_TOKEN aren’t optional here. Pipecat’s Twilio transport uses them to authenticate with Twilio’s REST API and to hang up the call cleanly when your agent ends the conversation.
4. Write Your Agent
Create agent.py:
5. Configure Twilio to Reach Your Agent
Twilio needs a public wss:// URL to stream call audio to. For local development, expose your agent with ngrok.
Start ngrok:
ngrok prints a forwarding URL that looks like https://xxxx.ngrok-free.app. You’ll turn this into a wss:// URL in the next step.
The domain suffix ngrok assigns varies per account — you may see ngrok-free.app, ngrok-free.dev, or the older ngrok.io, regardless of whether you’re on macOS, Windows, or Linux. Always use whatever URL ngrok actually prints, not the placeholder shown here.
Free ngrok URLs change every time you restart ngrok. If your agent stops receiving calls, check whether the URL in your TwiML Bin still matches the one ngrok is currently printing.
Create a TwiML Bin:
TwiML Bins aren’t in the Twilio Console’s left sidebar by default. The fastest way to reach them is to click the search bar at the top of the console and type “TwiML Bins”.
- Open the Twilio Console and search for TwiML Bins using the search bar at the top (or find it under Developer Tools in the sidebar)
- Click Create new TwiML Bin, give it a friendly name, and paste the following, replacing the domain with your own ngrok URL:
- Click Create to save the bin
Assign it to your phone number:
- Go to Numbers & Senders, then Phone Numbers, and select your number
- Open its configuration/voice settings tab
- Find the setting for what happens when a call comes in, and set it to use your TwiML Bin (Twilio has labeled this option TwiML, TwiML Bin, or similar depending on your console version)
- Save
Twilio has redesigned this page’s tabs and section names more than once (you may see Configuration, Configure, or Configuration Details; Voice & Fax, Voice Configuration, or Voice and emergency calling). The setting you want is always the one controlling what happens when a call comes in. If you can’t find it, Twilio’s own Getting Started with TwiML Bins guide has current screenshots.
6. Run Your Agent
This starts a local FastAPI server, via Pipecat’s development runner, that accepts Twilio’s Media Streams WebSocket connection at /ws.
7. Test Your Agent
Call your Twilio phone number from any phone. Your voice agent will pick up and start the conversation.
Customization Examples
Example 1: Hindi Voice Agent
language, voice, and target_language_code are Settings fields, not constructor keyword arguments. Passing them directly to SarvamSTTService(...) or SarvamTTSService(...) raises a TypeError on current versions of pipecat-ai. Only mode (STT) and api_key stay outside settings.
Example 2: Tamil Voice Agent
Example 3: Multilingual Agent (Auto-detect)
Use language="unknown" for support lines where callers might speak any of several languages. Sarvam auto-detects the spoken language per utterance, so the same agent can handle a Hindi caller followed by a Tamil caller without any code changes.
Example 4: Speech-to-English Agent (Saaras)
Saarika transcribes speech to text in the same language. Saaras translates speech directly to English text. Use Saaras when the caller speaks an Indian language but you want to process and respond in English.
Saaras auto-detects the source language (Hindi, Tamil, and so on) and translates spoken content directly to English text, which makes Indian-language speech usable by English-based LLMs.
Available Options
Language Codes
Speaker Voices (Bulbul v3)
Male (23): Shubh (default), Aditya, Rahul, Rohan, Amit, Dev, Ratan, Varun, Manan, Sumit, Kabir, Aayan, Ashutosh, Advait, Anand, Tarun, Sunny, Mani, Gokul, Vijay, Mohit, Rehan, Soham
Female (14): Ritu, Priya, Neha, Pooja, Simran, Kavya, Ishita, Shreya, Roopa, Tanya, Shruti, Suhani, Kavitha, Rupali
TTS Additional Parameters
You can customize the TTS service with additional parameters:
Twilio Media Streams always carry 8kHz mono audio. You don’t need to touch sample_rate on SarvamTTSService for this: Pipecat resamples the TTS output to match the audio_out_sample_rate set in PipelineParams automatically.
Understanding the Call Flow
- Caller dials your Twilio number. Twilio answers using the TwiML Bin and opens a WebSocket (Media Stream) to your agent.
- Transport Input: Your Pipecat transport receives the caller’s audio over the WebSocket, and hands it to STT.
- STT (Speech-to-Text): Converts audio to text using Sarvam’s Saarika or Saaras, and the context aggregator adds it to the conversation context.
- LLM: Generates a response using Sarvam.
- TTS (Text-to-Speech): Converts the response to audio using Sarvam’s Bulbul.
- Transport Output: Streams the audio back over the WebSocket, and Twilio plays it to the caller, while the context aggregator saves the assistant’s response to context.
Pro Tips
- Use
language="unknown"to automatically detect the caller’s language. This works well for multilingual support lines, but transcription accuracy is slightly lower than when you specify the exact language code, so prefer an explicit code whenever you already know which language the caller will use. - Sarvam’s models understand code-mixing, so your agent can naturally handle Hinglish, Tanglish, and other mixed languages, which is common on real support calls.
- Use
sarvam-30bfor faster responses, orsarvam-105bfor more complex conversations.
Log the transcript from context_aggregator if you want post-call analytics. Writing it to a file or database is cheap and won’t slow down the live pipeline.
Troubleshooting
Call connects but there’s no audio. Confirm the TwiML <Stream url> uses wss://, not wss:/ or https://, and that it points at your current ngrok URL. Free ngrok URLs change on every restart.
Call drops immediately. Check that TWILIO_ACCOUNT_SID and TWILIO_AUTH_TOKEN are set correctly in .env. The Twilio serializer needs both to manage the call.
API key errors. Make sure all keys are in your .env file and that the file sits in the same directory as agent.py.
Module not found. Re-run the install command for your operating system (see Step 2 above).
Poor transcription. Try language="unknown" for auto-detection, or set the exact language code (en-IN, hi-IN, and so on) if you already know it.
Choppy or robotic audio. Make sure audio_in_sample_rate and audio_out_sample_rate are both set to 8000 in PipelineParams. Twilio Media Streams don’t support other rates, and a mismatch causes distorted playback.
Additional Resources
- Sarvam AI Documentation
- Pipecat Documentation
- Pipecat Sarvam LLM Service
- Pipecat Twilio Serializer Reference
- Twilio Media Streams Documentation
- Twilio Getting Started with TwiML Bins
- Twilio Console
Need Help?
- Sarvam Support: developer@sarvam.ai
- Community: Join the Discord Community
Happy Building!