How to do inbound calls on livekit?
# ask-ai
c
How to do inbound calls on livekit?
t
Thanks for reaching out, Subham. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
Handling inbound calls on LiveKit is something we can definitely walk through together! To get started, you’ll want to set up a SIP trunking provider and then configure LiveKit SIP. Here’s a simple overview to guide you: • First, set up a SIP trunk with your telephony provider so calls can be routed to LiveKit. • Next, in LiveKit, create an inbound trunk to manage those incoming calls. You can even restrict this to specific IP addresses or phone numbers if you like. • Then, configure dispatch rules in LiveKit to decide how those inbound calls get routed to rooms. For example, you might put all callers in the same room or separate them based on your preferences. • And if you want an extra layer of security, you can set up PIN-based room access. Every inbound caller is treated as a SIP participant in LiveKit, and you can manage them easily using participant APIs. If you’d like, I’m here to help with any specific steps, like setting up your SIP provider or tweaking those dispatch rules. Just let me know! Sources: README.md | Create and configure a Plivo SIP trunk | LiveKit Docs | Using Wavix to accept and make calls | LiveKit Docs | Accepting inbound calls | LiveKit Docs | SIP overview | LiveKit Docs
c
That I understand, how should I configure my agent? What should be it's entrypoint functions?
t
Let's walk through how to configure your agent for inbound calls in LiveKit together. Here’s a simple step-by-step guide to get you started: 1. Enable Explicit Agent Dispatch: First, make sure to assign a name to your agent using
WorkerOptions
. This helps ensure your agent is dispatched correctly. Here’s an example to guide you:
cli.run_app(WorkerOptions(entrypoint_fnc=entrypoint, agent_name="your-agent-name"))
2. Set Up Inbound Trunk and Dispatch Rule: ◦ Begin by configuring a SIP trunk with your provider so calls can route to LiveKit smoothly. ◦ Next, create an inbound trunk within LiveKit and set up a dispatch rule that directs calls to a room and triggers your agent. 1. Define the Entrypoint Function: This function is key—it initializes the agent session and connects it to the call room. Here’s a friendly example:
Copy code
async def entrypoint(ctx: JobContext):
    session = AgentSession()
    await session.start(
        agent=YourCustomAgent(),
        room=ctx.room,
        room_input_options=RoomInputOptions(),
        room_output_options=RoomOutputOptions(transcription_enabled=True),
    )
2. Answer and Interact with Calls: When you’re ready to respond, use
generate_reply
to engage dynamically. For instance:
await session.generate_reply("Hello, how can I assist you today?")
3. Run and Test: Finally, start your agent application and give it a test by dialing the phone number you’ve set up. If you run into any questions or want a hand with specific parts, like setting up your SIP provider or crafting dispatch rules, just reach out—I’m here to help! Sources: README.md | packages/sip--um-actually/README.md | Agents telephony integration | LiveKit Docs | Accepting inbound calls | LiveKit Docs | examples/voice_agents/weather_agent.py
c
Does it mean that, i don't need to write the create sip participant in the agent?
t
You're absolutely right—you don't need to write any code to create the SIP participant in your agent. When an inbound call comes in, LiveKit takes care of automatically creating the SIP participant and placing them into the right room, following the dispatch rules you've set up. From there, your agent's role starts when it's dispatched to the room. At that moment, your agent can engage with the SIP participant and other participants in the room using the APIs we've provided. If you have any questions along the way, I'm here to help!
c
how to upgrade livekit cli
t
Hi there! To help you upgrade your LiveKit CLI smoothly, here are some easy steps depending on your operating system: • macOS: If you used Homebrew to install it, simply run
brew upgrade livekit-cli
. • Linux: You can update by running
curl -sSL https://get.livekit.io/cli | bash
. • Windows: For those who installed via Winget, just run
winget upgrade LiveKit.LiveKitCLI
. If you prefer, you can also grab the latest precompiled binaries directly from the GitHub releases page. Feel free to reach out if you need any help along the way! Sources: LiveKit CLI Setup | LiveKit Docs | README.md
c
I want to include dispatch metadata into my dispatch rule, how to do so?
t
I'm happy to guide you through how to include dispatch metadata in a dispatch rule. Here are some simple methods you can follow: 1. Using the LiveKit Cloud Dashboard: ◦ Start by navigating to Telephony → Configuration in the dashboard. ◦ Then, select Create new → Dispatch rule. ◦ Switch over to the JSON editor tab and add the
metadata
field like this:
Copy code
{
  "metadata": "{\"is_internal\": true}"
}
1. Using the API: ◦ Simply include the
metadata
field in your
CreateSIPDispatchRuleRequest
payload. Here's an example:
Copy code
{
  "dispatch_rule": {
    "name": "My Dispatch Rule",
    "metadata": "{\"is_internal\": true}",
    "rule": { /* your dispatch rule type */ }
  }
}
1. Using SDKs (e.g., Python):
Copy code
await sip_service.update_dispatch_rule_fields(
    rule_id="your-rule-id",
    metadata='{\"is_internal\": true}'
)
Just a quick note: the
metadata
field expects a string, and using JSON is a great way to keep your data structured. This metadata will be passed along to any SIP participants created through the dispatch rule. If you have any questions or need further help, feel free to ask—I'm here for you! Sources: SIP dispatch rule | LiveKit Docs | protobufs/livekit_sip.proto | livekit-api/livekit/api/sip_service.py | Agent dispatch | LiveKit Docs
c
i just want the user's phone number captured, how to do so?
t
I'd love to help you capture the user's phone number during inbound calls! You can do this by configuring your inbound trunk to map SIP headers that contain caller information to participant attributes. Here's a friendly step-by-step guide to get you started: 1. Identify Available SIP Headers: Some common headers you might see are: ◦ `From`: This usually contains the caller's SIP URI (it's pretty standard). ◦ `P-Asserted-Identity`: Often holds the verified caller ID. ◦ `Remote-Party-ID`: Another header that can carry caller ID info. 1. Configure Header Mapping: You'll want to add the
headers_to_attributes
field in your inbound trunk configuration. Here's a simple example:
Copy code
{
  "headers_to_attributes": {
    "From": "caller_number",
    "P-Asserted-Identity": "verified_caller_id"
  }
}
2. Create the Inbound Trunk: Use the LiveKit CLI to create the trunk with your updated configuration. 3. Access Caller Information: The attributes you mapped (like
caller_number
) will then be available as participant attributes in your LiveKit room. If you’d like, I’m here to help you configure this for your specific SIP provider or guide you on how to access these attributes in your app. Just let me know—I'm happy to assist! Sources: rpc/sip.pb.go | SIP inbound trunk | LiveKit Docs | Agent dispatch | LiveKit Docs | Inbound calls with Twilio Voice | LiveKit Docs | Create and configure a Plivo SIP trunk | LiveKit Docs
c
so dispatch rules in cloud are only for inbound calls only?
t
Thanks for reaching out! I’d be happy to clarify this for you. Dispatch rules in LiveKit Cloud are designed specifically for inbound calls—they help decide how incoming SIP calls get routed to LiveKit rooms based on the rules you set up. When it comes to outbound calls, it works a bit differently. These calls are started programmatically using the
CreateSIPParticipant
API, which connects a SIP participant to the room you specify. This involves outbound trunks, which are separate setups dedicated to managing outbound SIP traffic. Sources: SIP overview | LiveKit Docs | SIP dispatch rule | LiveKit Docs | livekit/livekit_sip.pb.go
c
Where is the header added in dispatch rule?
t
Hi there! To add headers in a dispatch rule, you can use the
headers
field in the dispatch rule configuration. This lets you include custom SIP
X-*
headers, which will be added to the 200 OK response during the SIP INVITE process. Here’s a simple example to show you how to include headers in a dispatch rule:
Copy code
{
  "headers": {
    "X-Custom-Header": "value1",
    "X-Another-Header": "value2"
  }
}
You can set this up either through the LiveKit Cloud dashboard using the JSON editor, or programmatically via the API or SDKs. If you’d like, I’m here to help you with any specific implementation or questions you might have! Sources: SIP APIs | LiveKit Docs | rpc/io.pb.go | SIP dispatch rule | LiveKit Docs
c
Copy code
{
  "sipDispatchRuleId": "SDR_kaks6rsafdsdffas9b3",
  "rule": {
    "dispatchRuleIndividual": {
      "roomPrefix": "inbound_"
    }
  },
  "trunkIds": [
    "ST_XguXMasdfsdfQmvy73"
  ],
  "name": "inbound-dispatch-rule",
  "roomConfig": {
    "agents": [
      {
        "agentName": "livekit_master_inbound_agent"
      }
    ]
  }
}
t
Thank you for sharing your dispatch rule configuration! If you'd like to include headers in this rule, you can easily add the
headers
field to your JSON. Here's a friendly example to guide you:
Copy code
{
  "sipDispatchRuleId": "SDR_kaks6rsafdsdffas9b3",
  "rule": {
    "dispatchRuleIndividual": {
      "roomPrefix": "inbound_"
    }
  },
  "trunkIds": [
    "ST_XguXMasdfsdfQmvy73"
  ],
  "name": "inbound-dispatch-rule",
  "roomConfig": {
    "agents": [
      {
        "agentName": "livekit_master_inbound_agent"
      }
    ]
  },
  "headers": {
    "X-Custom-Header": "value1",
    "X-Another-Header": "value2"
  }
}
If you have any questions or need a hand with this setup, please don’t hesitate to reach out—I’m here to help!
c
I see the caller number in the livekit inbound room name configuration,
inbound_<caller-number>
I want to capture this number and use it in my attributes.
t
Hi there! To capture the caller's phone number, you actually don’t need to extract it from the room name. LiveKit thoughtfully captures the caller's phone number during inbound calls and makes it available in the participant's attributes under the key
sip.phoneNumber
. This is the easiest and most reliable way to get the caller’s number. If you prefer to use the room name to store and extract the caller number (for example,
inbound_<caller-number>
), you would need to add some custom application logic outside of LiveKit to parse the room name and pull out the number. Just a heads-up, this method isn’t natively supported by LiveKit. Please feel free to reach out if you’d like a hand accessing
sip.phoneNumber
or if you want to set up custom attributes tailored to your needs—I’m here to help! Sources: SIP participant | LiveKit Docs | livekit-protocol/src/livekit.rs | SIP dispatch rule | LiveKit Docs | livekit/livekit_connector_whatsapp.pb.go
c
Ok got it. But how can I access sip.phoneNumber? What other fields are passed to the agent?
t
I'm happy to help you access
sip.phoneNumber
through the participant attributes in the LiveKit room. This field holds the caller's phone number for inbound calls or the dialed number for outbound calls. Just a quick heads-up—it might not be available if the
HidePhoneNumber
setting is enabled in the dispatch rule. Here are some other useful fields passed to the agent: • SIP Participant Attributes: ◦ `sip.callID`: This is LiveKit’s SIP call ID. ◦ `sip.callStatus`: Shows the current call status, like active or dialing. ◦ `sip.ruleID`: The dispatch rule ID used for the call. ◦ `sip.trunkID`: The SIP trunk ID involved in the call. ◦ `sip.trunkPhoneNumber`: The phone number linked to the SIP trunk. • Job Metadata: You can pass custom data such as
user_id
,
user_name
, or
user_phone
when dispatching to an agent. • Room and Participant Metadata: This includes room metadata, participant name, and any custom attributes you’ve set. If you’d like, I’m here to guide you through accessing these fields or using them in your agent workflows—just let me know! Sources: SIP participant | LiveKit Docs | Job lifecycle | LiveKit Docs | Preserving Caller ID During SIP Transfers in LiveKit Agents
c
Are sip participant attributes sent in room events? What is available in job metadata?
t
I'm happy to help clarify this for you! Yes, SIP participant attributes are indeed sent in room events and can be accessed by agents. These attributes include: • `sip.callID`: This is LiveKit’s internal SIP call ID. • `sip.phoneNumber`: The caller's phone number, as long as it’s not hidden. • `sip.callStatus`: Shows the current call status, like whether it’s active. • `sip.trunkID`: The SIP trunk ID that’s being used for the call. • `sip.h.*`: These are custom SIP headers mapped to attributes, such as
sip.h.x-custom-id
. You’ll find these attributes available in room events like
participant_attributes_changed
, which you can use to tailor the agent’s experience. Regarding job metadata, it’s a flexible string field that you pass during agent dispatch. This usually includes structured data like: • User-specific details (for example, user ID, name, or phone number). • Custom workflow parameters that fit your needs. • Authentication tokens or context IDs. You can access job metadata in the agent’s
entrypoint
function through the
JobContext
. If you’d like, I’m here to guide you through implementing these features step-by-step! Sources: telephony/sip_lifecycle.py | rpc/io.pb.go | livekit-protocol/src/livekit.rs | Job lifecycle | LiveKit Docs | Agent dispatch | LiveKit Docs
c
let's say i want to pass user_name and user_phone, how do I write it the json, where do i extract these variables from?
t
To share
user_name
and
user_phone
with an agent, a great way is to include them in the
job_metadata
field when you dispatch the agent. Here’s a simple example of how your JSON for the dispatch request might look:
Copy code
{
  "agent_name": "your-agent-name",
  "job_metadata": {
    "user_name": "John Doe",
    "user_phone": "+1234567890"
  }
}
Then, in the agent’s
entrypoint
function, you can easily grab these values from the
JobContext
object. Here’s a quick example in Python:
Copy code
async def entrypoint(ctx: JobContext):
    user_name = ctx.job_metadata.get("user_name")
    user_phone = ctx.job_metadata.get("user_phone")
    print(f"User Name: {user_name}, User Phone: {user_phone}")
If you’d like, I’m here to help you walk through this setup or answer any other questions you might have!
c
My ask is where do I get the user_phone from? Like for every new inbound call for us
t
Hi there! For every new inbound call, LiveKit automatically captures the
user_phone
(the caller’s phone number) and stores it in the SIP participant attribute
sip.phoneNumber
. You can easily retrieve it from this attribute as soon as the SIP participant joins the room. If you'd like to pass it explicitly in
job_metadata
, you can simply extract it from
sip.phoneNumber
and include it in your dispatch request. Here’s a quick example to guide you:
Copy code
{
  "agent_name": "your-agent-name",
  "job_metadata": {
    "user_name": "John Doe",
    "user_phone": "{sip.phoneNumber}"
  }
}
If you have any questions or need a hand with implementing this, just let me know—I’m here to help!
c
Copy code
{
  "agent_name": "your-agent-name",
  "job_metadata": {
    "user_name": "John Doe",
    "user_phone": "{sip.phoneNumber}"
  }
}
should the
{sip.phoneNumber}
be in code block?
t
Hi there! Just to clarify, the
{sip.phoneNumber}
placeholder shouldn’t be included exactly like that in the JSON. Instead, you’ll want to dynamically replace it with the real value of
sip.phoneNumber
from the SIP participant attributes when you’re building the dispatch request. Here’s a simple example in Python to help illustrate:
Copy code
user_phone = participant.attributes.get("sip.phoneNumber")
job_metadata = {
    "user_name": "John Doe",
    "user_phone": user_phone
}
This way, the actual phone number gets passed in the
job_metadata
field. If you’d like me to walk you through this further or have any other questions, just let me know—I’m here to help!
c
what details do we get in the attributes?
t
For SIP participants, here are the attributes you can access: • `sip.callID`: This is LiveKit’s internal SIP call ID. • `sip.callIDFull`: The trunk provider’s globally unique SIP call ID. • `sip.phoneNumber`: The caller’s phone number, if it’s not hidden. • `sip.trunkID`: The SIP trunk ID used for the call. • `sip.ruleID`: The SIP Dispatch Rule ID used for the inbound call. • `sip.trunkPhoneNumber`: The phone number associated with the trunk. • `sip.callStatus`: The current status of the SIP call, like “active.” • `sip.hostname`: The hostname from the SIP call. • `sip.h.*`: Custom SIP headers mapped to attributes, for example
sip.h.x-custom-id
. You can find these attributes in room events, and they’re great for customizing your workflows. If you’d like, I’m here to help you access or use any of these attributes—just let me know!
c
does the sip attributes show in the logs?
t
I understand that SIP attributes aren’t automatically included in LiveKit server logs, but don’t worry! You can definitely log them explicitly through your application code. These attributes—like
sip.callID
,
sip.phoneNumber
,
sip.callStatus
, and others—are accessible in the
participant.attributes
field whenever a SIP participant joins a room. To capture these SIP attributes, you can set up an event handler for
on_participant_connected
or
on_participant_attributes_changed
. Here’s a simple example to get you started:
Copy code
def on_participant_attributes_changed_handler(changed_attributes: dict, participant: rtc.Participant):
    if participant.kind == rtc.ParticipantKind.PARTICIPANT_KIND_SIP:
        call_id = participant.attributes.get("sip.callID", "Unknown")
        phone_number = participant.attributes.get("sip.phoneNumber", "Unknown")
        logger.info(f"SIP Call ID: {call_id}, Phone Number: {phone_number}")
If you’d like, I’m here to help you set up this logging in your application—just let me know! Sources: protobufs/rpc/sip.proto | SIP participant | LiveKit Docs | telephony/sip_lifecycle.py
c
how to extract this in entry point?
t
To help you extract SIP attributes in the
entrypoint
function of your agent, you can use the
JobContext
object, which holds all the room and participant details. Here’s a friendly example in Python to guide you:
Copy code
async def entrypoint(ctx: JobContext):
    # Access the room participants
    for participant in ctx.room.participants:
        if participant.kind == "SIP":
            # Extract SIP attributes
            call_id = participant.attributes.get("sip.callID", "Unknown")
            phone_number = participant.attributes.get("sip.phoneNumber", "Unknown")
            call_status = participant.attributes.get("sip.callStatus", "Unknown")

            # Log or use the extracted attributes
            print(f"SIP Call ID: {call_id}")
            print(f"Phone Number: {phone_number}")
            print(f"Call Status: {call_status}")
This snippet goes through each participant in the room, checks if they are of kind
SIP
, and then gently pulls out the SIP attributes from
participant.attributes
. If you have any questions or want to explore this further, please don’t hesitate to reach out—I’m here to help you every step of the way!