I’ve already built a telephony agent using LiveKit...
# ask-ai
i
I’ve already built a telephony agent using LiveKit that works for outbound calls with Twilio. When creating the room, I pass the prompt and call-related information as metadata, so the agent can use it during the call. But in the case of inbound calls (e.g., via Twilio SIP into LiveKit), the call originates externally, so I don’t directly create the room with metadata beforehand. Question: How can I make sure that my LiveKit agent still gets the prompt and call-related metadata in an inbound call scenario?
t
Thanks for reaching out, Abdul. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
I'd love to help you out! Could you please share a bit more about how your current setup manages room creation for inbound calls? For example: • Are you using a webhook or some server-side logic to create the room dynamically when an inbound call comes in? • Do you have control over the room creation process, or is it handled entirely on the outside? Getting a clearer picture of this will really help me guide you on the best way to pass metadata to your LiveKit agent. Looking forward to your response!
i
my current setup manages room creation for outbound calls. I want to know, how can I do the same for inbound calls. This is my current setup for outbound. async def make_call(self, phone_number: str, room_name: str, user_identity: str, metadata: str, agent_name: str) -> Dict[str, Any]: """Make a call to the specified phone number.""" if not self.livekit_api: raise RuntimeError( "LiveKitAPI client not available. Use as async context manager." ) self.logger.info(f"Making call to phone number: {phone_number}") # Create agent dispatch logger.info(f"Creating dispatch for agent {agent_name} in room {room_name}") dispatch = await self.livekit_api.agent_dispatch.create_dispatch( CreateAgentDispatchRequest( agent_name=agent_name, room=room_name, metadata=metadata ) ) logger.info(f"Created dispatch: {dispatch}") import asyncio await asyncio.sleep(5) # Create SIP participant to make the call if not self.outbound_trunk_id or not self.outbound_trunk_id.startswith("ST_"): logger.error("SIP_OUTBOUND_TRUNK_ID is not set or invalid") return logger.info(f"Dialing {phone_number} to room {room_name}") try: # Create SIP participant to initiate the call sip_participant = await self.livekit_api.sip.create_sip_participant( CreateSIPParticipantRequest( room_name=room_name, sip_trunk_id=self.outbound_trunk_id, sip_call_to=f"+91{phone_number}", participant_identity=user_identity, ) ) logger.info(f"Created SIP participant: {sip_participant}") except Exception as e: logger.error(f"Error creating SIP participant: {e}")
t
I'm happy to help you handle metadata for inbound calls in LiveKit! You can create rooms dynamically and pass metadata using SIP dispatch rules and server-side APIs. Here’s a friendly guide to adapting your setup: • Dynamic Room Creation: Try using the
SIPDispatchRuleIndividual
rule type—it lets you automatically create a new room for each inbound call. You can also set a
roomPrefix
to customize the room names to fit your needs. • Passing Metadata: There are a few ways you can pass metadata: ◦ Agent Metadata: Use
roomConfig.agents.metadata
in the dispatch rule to send job-specific metadata to your LiveKit agent. ◦ Room Metadata: You can set or update room metadata dynamically using the
CreateRoom
or
UpdateRoomMetadata
APIs. ◦ Participant Metadata: Attach metadata to the SIP participant when creating them, or update it later with the
UpdateParticipant
API. ◦ Dispatch Rule Metadata: Use the
metadata
field in your dispatch rule to tag calls with any relevant information. If you’d like, you can check out the SIP Dispatch Rule and Room Metadata API documentation for more details. And please don’t hesitate to reach out if you want help with specific code examples or configurations—I’m here to support you every step of the way! Sources: protobufs/livekit_sip.proto | Room metadata | LiveKit Docs | lib/livekit/sip_service_client.rb | Docs::Telephony | Docs::Telephony
i
In my case I need to create separate room for each inbound call. And the metadata will be different for each.
t
To help you create a separate room for each inbound call and assign unique metadata, here’s a friendly approach you can follow: 1. Dynamic Room Creation: You can set up a
SIPDispatchRuleIndividual
rule that automatically creates a new room for every inbound call. This way, each call gets its own dedicated space. 2. Assign Metadata: On the server side, you can dynamically assign metadata to each room by: ◦ Listening for inbound call events, perhaps through a webhook or SIP trunk integration. ◦ Using the
CreateRoom
API to create the room and add metadata right when the room is created. ◦ Or, if you prefer, you can update the metadata afterward with the
UpdateRoomMetadata
API. 1. Participant Metadata: If you want to attach metadata specific to the caller, you can do so when creating the SIP participant or update it later using the
UpdateParticipant
API. This setup will make sure each inbound call has its own room along with the right metadata. If you’d like, I’m here to help you with any specific API calls or configuration details—just let me know!
i
how do I get the room name, to pass the metadata to, Because the room is created by the inbound call right?
t
When it comes to inbound calls where the room is created dynamically, there are a few ways you can easily retrieve the room name: 1. Webhook or Event Callbacks: You can set up a webhook to listen for room creation events. The great part is that the event payload will include the room name, which you can then use to assign metadata seamlessly. 2. Server SDK: Another handy option is using the
listRooms
API to get a list of active rooms along with their names. This is especially useful if you want to keep an eye on things or manage metadata dynamically after the room has been created. 3. Room Name Format: If you're working with
SIPDispatchRuleIndividual
, the room name usually follows the pattern
roomPrefix + callerNumber + randomSuffix
. So, if you’ve set a custom prefix, you can predict the room name format quite easily. Please feel free to reach out if you’d like some guidance on setting up webhooks, using the
listRooms
API, or applying this in your specific setup. I’m here to help! Sources: packages/livekit-server-sdk/src/RoomServiceClient.ts | lib/src/ui/builder/room/room_name.dart | Avoiding Room ID Reuse When Re-Creating Rooms With the Same Name | SIP dispatch rule | LiveKit Docs | Docs::Telephony
i
is there a way that I can create a room first, then the inbound call joins to that same room?
t
Absolutely, you can pre-create a room and have inbound SIP calls routed directly to it. Let me walk you through the steps to set this up: 1. Create the Room: You can use the LiveKit server SDKs or CLI to create the room ahead of time. For example, with the CLI:
livekit-cli create-room --room my-precreated-room
2. Set Up an Inbound SIP Trunk: Next, configure an inbound SIP trunk in LiveKit so it can authenticate and accept calls from your SIP provider. 3. Create a Dispatch Rule: Use the
dispatchRuleDirect
type within the SIP dispatch rule to route calls straight to your pre-created room. Here's an example:
Copy code
{
  "rule": {
    "dispatchRuleDirect": {
      "roomName": "my-precreated-room"
    }
  },
  "trunk_ids": [
    "your_sip_trunk_id"
  ]
}
After setting this up, any inbound calls that match your dispatch rule will be directed to the room you specified. If you’d like, I’m here to help with any specific configurations or API details—just let me know! Sources: Docs::Telephony | Docs::Telephony | Docs::Telephony | README.md | Using Wavix to accept and make calls | LiveKit Docs
i
so if the room is already created, the inbound call won't create that room again, instead it will just join to that room. Am I right?
t
Yes, you’ve got it! If the room is already created, the inbound call won’t create a new one. Instead, it will join the existing room, as long as the SIP dispatch rule is set up to route the call to that specific room. This way, the room stays consistent, and you can easily manage metadata or participants all within the same room. If you’d like, I’m here to help you with any part of setting this up or to clarify anything further. Just let me know!
i
But my room name will be different for each callee. How do I add a dynamic room name here? in that case will this direct dispatch dispatch rule work? Or should I create a dispatch rule for each callee through api? Will that a viable solution?
t
To help you manage dynamic room names for inbound calls, there are two great options you can consider: 1. Use a Callee Dispatch Rule: You can set up a
SIPDispatchRuleCallee
that creates rooms dynamically based on the callee (the called phone number). By setting a
roomPrefix
and enabling
randomize: true
, you’ll get unique room names for each call, which is super handy. 2. Create Dispatch Rules Dynamically: If you prefer to have specific room names ready for each callee, you can use the
CreateSIPDispatchRule
API to programmatically create dispatch rules. This way, you can dynamically add rules as new phone numbers come in. While dynamically creating dispatch rules is definitely doable, it does involve some backend logic to keep everything running smoothly. If you’d like, I’m here to help you with implementing either method or to chat through your specific needs to find the best fit for your setup. Sources: Docs::Telephony | SIP dispatch rule | LiveKit Docs | protobufs/livekit_sip.proto | README.md