General

Connect Inbound Calls to a Voice AI Agent with Twilio and LiveKit

May 30, 2025

Starting and Growing a Career in Web Design
Starting and Growing a Career in Web Design
Starting and Growing a Career in Web Design

1.Introduction

Imagine every phone call to your business being answered instantly, not by a human, but by a smart Voice AI agent. These agents never sleep and always know what to say, transforming customer service, appointment booking, and information sharing. But how do you get a real phone call connected to a Voice AI agent in real time?

In this post, you’ll learn how to directly route real inbound phone calls from Twilio to a Voice AI agent using LiveKit’s SIP capabilities. We’ll walk through how Twilio manages incoming calls, how LiveKit bridges them into real-time audio rooms, and how to connect it so your voice AI agent can start talking to callers instantly.

By the end of this guide, you’ll have a clear workflow and practical setup for integrating Voice AI into your phone system using industry-standard tools.


2.Understanding SIP Trunking

Before you can connect a phone call to a Voice AI agent, it’s important to understand how voice communication moves from the traditional phone network into your application. This is where SIP trunking comes in.

Session Initiation Protocol (SIP) trunking is a modern, digital method of making and receiving phone calls over the Internet. An SIP trunk acts as a virtual phone line, allowing your application to place and receive calls without relying on legacy phone infrastructure.

Unlike traditional analog or ISDN phone lines, SIP trunks can handle not only voice calls but also multimedia communication. This includes voice, video, and messaging all over a single IP connection. SIP trunking is quickly becoming the industry standard, with many countries actively phasing out older ISDN networks.

In the workflow described in this guide:

  • Twilio acts as your telephony provider, receiving calls from the public phone network and routing them using SIP.

  • LiveKit receives the SIP call, bridges it into a real-time audio room, and enables your Voice AI agent to participate in the conversation.

Alternatives to SIP: H.323, WebRTC (Web Real-Time Communication), XMPP (Extensible Messaging and Presence Protocol), MGCP (Media Gateway Control Protocol), Proprietary Protocols.


3.Behind the Ring: The Inbound Call Workflow


Let’s break down the typical workflow for routing inbound calls to a Voice AI agent:

  • A customer dials your Twilio phone number.

  • Twilio receives the call and routes it through an Elastic SIP Trunk based on your SIP configuration.

  • Twilio forwards the call via SIP (Session Initiation Protocol) to LiveKit’s SIP endpoint.

  • LiveKit authenticates the SIP trunk credentials and searches for a matching dispatch rule.

  • LiveKit creates a SIP participant for the caller and places the caller into a LiveKit room based on the dispatch rule.

  • The Voice AI agent answers the call, processes the caller’s speech, and responds conversationally.

4. Let’s dive into practical implementation

Follow the GitHub repo for a smoother implementation.

Before your Voice AI agent can start handling calls, you’ll need to configure the essential components that connect Twilio and LiveKit. Here’s a step-by-step guide to set up the pipeline and get your real-time voice integration up and running.

4.1 Set Up Twilio Account
  • Create a Twilio Account: Sign up for a Twilio account if you haven’t already.


Purchase a Phone Number: In the Twilio Console, buy a phone number that will receive inbound calls.


4.2 Set Up LiveKit Account
  • Create a LiveKit Account and Project: Register for LiveKit and create a new project to manage your real-time audio interactions.

  • Get Project URL and SIP URI: In your LiveKit project settings, find the Project URL and SIP URI parameters. These are necessary for SIP trunk configuration.

4.3 Install Twilio CLI

To install with Advanced Package Tool (apt), run the following commands in your terminal:

  wget -qO- https://twilio-cli-prod.s3.amazonaws.com/twilio_pub.asc \
    | sudo apt-key add -
  sudo touch /etc/apt/sources.list.d/twilio.list
  echo 'deb https://twilio-cli-prod.s3.amazonaws.com/apt/ /' \
    | sudo tee /etc/apt/sources.list.d/twilio.list
  sudo apt update
  sudo apt install -y twilio
4.4 Create a SIP trunk

Using the TWILIO UI

  • Search Elastic SIP OR Select Elastic SIP Trunking >> Manage >> Trunks.

  • Create a SIP trunk.

Using Twilio CLI

Make sure the domain name of your SIP trunk ends with [pstn.twilio.com]

twilio api trunking v1 trunks create \
  --friendly-name "My test trunk" \
  --domain-name "my-test-trunk.pstn.twilio.com"

After running the above command, you will receive TWILIO-TRUNK-ID. Copy your TWILIO-TRUNK-ID and save it

4.5 Configure Twilio trunk for inbound calls

Using the TWILIO UI

  • Navigate to Voice >> Manage >> Origination connection policy.

  • Select the created origination policy.

  • Add the SIP-URI, weight, and priority.

Using Twilio CLI

twilio api trunking v1 trunks origination-urls create \
    --trunk-sid <TWILIO-TRUNK-ID> \
    --friendly-name "LiveKit SIP URI" \
    --sip-url <Livekit-SIP-URI> \
    --weight 1 --priority 1 --enabled



4.6 Associate phone Number and trunk

For this, you need Twilio-Trunk-SID and Twilio-Phone-Number-SID; you can obtain them using the following methods.

# To list phone numbers:
twilio phone-numbers list

# To list trunks:
twilio api trunking v1 trunks list

Then, to associate both

twilio api trunking v1 trunks phone-numbers create \
  --trunk-sid <twilio_trunk_sid> \
  --phone-number-sid <twilio_phone_number_sid

4.7 Install LiveKit CLI
# For Linux:
curl -sSL https://get.livekit.io/cli | bash
4.8 Configure LiveKit SIP Trunk
  • Create an Inbound Trunk: Prepare an inbound-trunk.json file.

{
    "trunk": {
      "name": "My inbound trunk",
      "numbers": ["+1234567890"],  
      "krisp_enabled": true
    }
 }
  • Run the CLI Command: Use the LiveKit CLI to create an inbound trunk.

 lk sip inbound create inbound-trunk.json
4.9 Create Dispatcher Rule
  • At least one rule is required to accept incoming calls into livekit rooms: Prepare an inbound-trunk.json file.

{
    "rule": {
      "dispatchRuleIndividual": {
        "roomPrefix": "call-"
      }
    },
    "roomConfig": {
          "agents": [{
              "agentName": "my-telephony-agent"
         }]
     }
 }
  • Run the CLI Command: Use the LiveKit CLI to create a dispatch rule.

lk sip dispatch create dispatch-rule.json
4.10 Installing Dependencies for AI Agent
pip install \
  "livekit-agents[deepgram,openai,cartesia,silero,turn-detector]~=1.0" \
  "python-dotenv"
4.11 Environment Configuration

Create an environment file .env

DEEPGRAM_API_KEY=<Your Deepgram API Key>
OPENAI_API_KEY=<Your OpenAI API Key>
CARTESIA_API_KEY=<Your Cartesia API Key>
LIVEKIT_API_KEY=<your API Key>
LIVEKIT_API_SECRET=<your API Secret>
LIVEKIT_URL=<YOUR LIVEKIT URI>
4.12 Create LiveKit Inbound Caller Voice Agent

Create a main.py file and paste the following code:

from dotenv import load_dotenv

from livekit import agents
from livekit.agents import AgentSession, Agent, RoomInputOptions
from livekit.plugins import (
    openai,
    cartesia,
    deepgram,
    
    silero,
)
from livekit.plugins.turn_detector.multilingual import MultilingualModel

load_dotenv()

class Assistant(Agent):
    def __init__(self) -> None:
        super().__init__(instructions="You are a helpful voice AI assistant.")

async def entrypoint(ctx: agents.JobContext):
    await ctx.connect()

    session = AgentSession(
        stt=deepgram.STT(model="nova-3", language="multi"),
        llm=openai.LLM(model="gpt-4.1-nano"),
        tts=cartesia.TTS(),
        vad=silero.VAD.load(),
        turn_detection=MultilingualModel(),
    )

    await session.start(
        room=ctx.room,
        agent=Assistant(),
        room_input_options=RoomInputOptions(
          #  noise_cancellation=noise_cancellation.BVC(),
        ),
    )

    await session.generate_reply(
        instructions="Greet the user and offer your assistance."
    )

if __name__ == "__main__":
    agents.cli.run_app(agents.WorkerOptions(
        entrypoint_fnc=entrypoint,

        # agent_name is required for explicit dispatch
        agent_name="my-telephony-agent"
    ))
4.13 Download Model Files
python main.py download-files
4.14 Speak to your agent

Start your agent in console mode to run inside your terminal:

python main.py console
4.15 Connect to the playground

Start your agent in dev mode to connect it to LiveKit and make it available from anywhere on the internet:

 python main.py dev
4.16 Now test Connection from Twilio UI

Select Elastic SIP trunk >> Select Your trunk >> Origination >> Make test call.

You will be able to see the session being created with LiveKit on your dev terminal.

5. Conclusion

Connecting Twilio and LiveKit lets you bring your Voice AI agent to life, so when someone calls your phone number, your AI can answer and talk to them in real time, just like a human!
With just a few setup steps, getting Twilio and LiveKit talking via SIP, setting up your AI agent, and following the guide, you can automate phone calls for customer support, or anything else you dream up. No more waiting on hold or manual call handling!
In short: This setup turns your phone number into a smart, always-on assistant. It’s modern, flexible, and surprisingly easy to build. Now, you can let your AI do the talking!

If you run into any issues or need help with any step, feel free to open an issue or follow along on the GitHub repo. I’m happy to help!

We engineer reliable, scalable, and intelligent digital systems that help businesses modernize, automate, and grow

A40, ITHUM Towers, B-308,

Sector 62 Noida-201301

+91 8750701919

I Cube Systems • All Rights Reserved 2025

We engineer reliable, scalable, and intelligent digital systems that help businesses modernize, automate, and grow

A40, ITHUM Towers, B-308,

Sector 62 Noida-201301

+91 8750701919

I Cube Systems • All Rights Reserved 2025

We engineer reliable, scalable, and intelligent digital systems that help businesses modernize, automate, and grow

A40, ITHUM Towers, B-308,

Sector 62 Noida-201301

+91 8750701919

I Cube Systems • All Rights Reserved 2025