How to Build Your Own AI-Powered Voice Agent with LiveKit and Twillio: Step-by-Step Implementation Guide
Start by signing up for a Twilio account if you haven’t already. Simply visit Twilio’s website and follow the registration process to set up your account.
Once your account is ready, navigate to the Twilio Console and create a phone number. You don’t need to configure any additional settings at this stage—just select a number and you’re good to go. This number will be used to handle incoming and outgoing calls in the later steps.
Next, you’ll need your Twilio API credentials to integrate with LiveKit. These include your Account SID and Auth Token. Follow these steps:
- Go to the Twilio Console.
- Navigate to the Account Info section.
- Copy your Account SID and Auth Token, and Twillio phone number —you’ll use these in the next steps.
- Create a LiveKit Account
Sign up for a LiveKit account if you don’t have one already by visiting LiveKit’s website.
- Create a Project
After signing up, log in and create a new project within LiveKit. This project will be used to handle real-time audio and video interactions.
- Get the Project URL and SIP URI Parameters
Navigate to the Settings section of your newly created project and locate the Project URL and SIP URI parameters. These will be crucial in the later steps when configuring the integration.
To streamline the configuration process for Twilio and LiveKit, use the pre-built script available at the following URL: Twilio & LiveKit Integration Script(scripts/create_inbound_trunk.py).
Here’s what you need to do:
-
Download or clone the script from the link above.
-
Replace the placeholders in the script with the necessary details:
- Account SID
- Auth Token
- Phone Number
- SIP URI (found in previous steps)
- To ensure your environment is ready for Twilio, LiveKit, and OpenAI integration, install the necessary Python packages. Run the following command in your terminal:
pip install -r requirements.txt
- Install the LiveKit SDK
If you’re using macOS, you can install the LiveKit CLI via Homebrew:
brew install livekit-cli # MacOS
winget install LiveKit.LiveKitCLI # Windows
curl -sSL https://get.livekit.io/cli | bash # Linux
- Authenticate with LiveKit
After installation, authenticate to your LiveKit account by running the following command:
lk cloud auth
- Run the script to automatically configure Twilio and LiveKit with the required settings, minimizing the manual setup process. It will automatically create a SIP Trunk in Twilio and make all required configurations.
This script will handle most of the heavy lifting, simplifying the integration between Twilio and LiveKit for real-time communication.
After the script has automatically created the SIP Trunk on Twilio, you’ll need to manually update the Voice Configuration to ensure everything works correctly.
- OpenAI Realtime Voice AI Agent
python scripts/openai_realtime_voice_ai_agent.py
- Run Voice Pipeline AI Agent with Functional Calling and Saving chat message
python scripts/save_chatctx.py
lk sip inbound list
lk sip inbound create inbound_trunk.json
lk sip inbound delete SIP_ID
https://docs.livekit.io/agents/overview/ https://docs.livekit.io/agents/quickstarts/voice-agent/
https://agents-playground.livekit.io/ https://kitt.livekit.io/ https://cartesia-assistant.vercel.app/
https://github.com/livekit/agents/tree/main/examples/voice-pipeline-agent/llamaindex-rag
https://www.datavise.ai/blog/usage-of-realtime-openai-api-with-twillio-and-livekit https://gist.github.com/ShayneP/51eabe243f9e7126929ea7e9db1dc683