Is there a way I can configure an agent to handle ...
# ask-ai
m
Is there a way I can configure an agent to handle both inbound and outbound calls? It should behave different based on the nature of it being inbound or outbound
t
Thanks for reaching out, Alexander. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
I'm here to help you set up an agent that can smoothly handle both inbound and outbound calls with different behaviors. Just follow these steps, and you'll be all set: 1. Set up SIP trunks: You can configure separate or combined trunks to manage inbound and outbound SIP traffic effectively. 2. Create dispatch rules: These rules will help route inbound calls to LiveKit rooms and make sure your agent gets triggered at the right time. 3. Enable explicit agent dispatch: By naming your agent, you can disable automatic dispatch, which helps ensure calls are handled properly. 4. Customize agent behavior: Look out for the
phone_number
in the job metadata — if it’s there, it’s an outbound call; if not, it’s inbound. This way, you can tailor the agent’s logic to fit each call type. 5. Initiate outbound calls: When you’re ready to make outbound calls, use the
CreateSIPParticipant
API to get things started smoothly. If you’d like more detailed examples and handy templates, I highly recommend checking out the Outbound Calling Agent template and the Telephony Integration Guide. They’re great resources to guide you through the process. Sources: docs/index.yaml | SIP Integration & Telephony | livekit/livekit_composite | DeepWiki | Agents telephony integration | LiveKit Docs | src/setup-livekit.ts | livekit/livekit_sip.pb.go
m
I’ve already got the agent configured for outbound. So the only difference is check for the phone number field in entry and supply a different agent classdepending on the case?
t
Absolutely, you’ve got it! The main thing to look out for is whether the
phone_number
field exists in the job metadata. Here’s how it works: • Inbound calls: If the
phone_number
isn’t there, you can set up an agent class that warmly greets the user and offers help right away. • Outbound calls: If the
phone_number
is included, the agent can patiently wait for the other person to speak first before jumping in. This way, you can easily tailor the agent’s behavior depending on whether the call is coming in or going out. If you have any questions or want to explore this further, I’m here to help!
m
Can you please give me a code example where you’re checking if phone_number exists?
t
Let me share a simple example to help you check if the
phone_number
field is present in the job metadata, and then adjust the agent's behavior accordingly. This way, your agent can respond just right for each call:
Copy code
async def handle_call(session, job_metadata):
    phone_number = job_metadata.get("phone_number")

    if phone_number is None:
        # Inbound call logic
        await session.generate_reply(
            instructions="Greet the user and offer your assistance."
        )
    else:
        # Outbound call logic
        await session.wait_for_user_input()
        await session.generate_reply(
            instructions="Respond to the user's input."
        )
Here’s what’s happening: • If
phone_number
is missing, the agent warmly greets the user right away, which is perfect for inbound calls. • If
phone_number
is there, the agent patiently waits for the user to say something first, ideal for outbound calls. You can easily plug this logic into your agent’s main handling function, so it naturally adapts based on whether the call is inbound or outbound. I hope this helps you get the behavior just right!
m
Great! Now how do I set up my inbound trunk for this?
t
Let’s get your inbound trunk set up together! Here’s a simple way to do it: 1. Initialize the SIP client: Start by using your LiveKit credentials to create a SIP client instance. 2. Create the inbound trunk: Next, define the trunk name, specify the phone numbers it will accept calls for, and provide the authentication details. Here’s a helpful example to guide you:
Copy code
const sipClient = new SipClient(
  LIVEKIT_URL,
  LIVEKIT_API_KEY,
  LIVEKIT_API_SECRET,
);

// Create inbound trunk
const trunkName = 'inbound-trunk';
const trunk = await sipClient.createSipInboundTrunk(
  trunkName,
  [TWILIO_PHONE_NUMBER], // Phone numbers this trunk accepts calls for
  {
    auth_username: TWILIO_SIP_USERNAME,
    auth_password: TWILIO_SIP_PASSWORD,
  },
);
After you’ve created the trunk, you can easily link it with dispatch rules. This way, inbound calls will be routed smoothly to your LiveKit rooms and connect the right agent. If you have any questions along the way, I’m here to help!
m
Do I need to do this in js? Can I just do it with the cli?
t
Great news! You can easily set up an inbound trunk using the LiveKit CLI instead of JavaScript. Let me walk you through the steps: 1. Install the LiveKit CLI: First, make sure the CLI is installed and that you’ve configured it with your LiveKit server credentials. This will help everything run smoothly. 2. Create the inbound trunk: Use this command to get started:
Copy code
lk sip inbound create --name "My trunk" --numbers "+15105550100" --auth-user "username" --auth-pass "password"
If you prefer, you can also use a JSON configuration file to make things even simpler:
Copy code
lk sip inbound create inbound-trunk.json
Here’s what your
inbound-trunk.json
should look like:
Copy code
{
  "trunk": {
    "name": "My trunk",
    "numbers": [
      "+15105550100"
    ],
    "auth_username": "username",
    "auth_password": "password"
  }
}
After you’ve created the trunk, the next step is to set up dispatch rules. This will help route inbound calls smoothly to your LiveKit rooms. If you need any help with that, feel free to ask—I’m here to help! Sources: cmd/lk/sip.go | Inbound calls with Twilio Voice | LiveKit docs | Create and configure a Twilio SIP trunk | LiveKit docs | SIP inbound trunk | LiveKit docs | lib/livekit/sip_service_client.rb
m
I read a config with krisp_enabled set. Is that important?
t
Just to let you know, the
krisp_enabled
setting is optional and it helps with noise cancellation during calls handled by the trunk. If you often have background noise on your calls or want to make the audio clearer, turning this feature on can really make a difference. Here’s how you can add it to your configuration:
Copy code
{
  "trunk": {
    "name": "My trunk",
    "numbers": [
      "+15105550100"
    ],
    "auth_username": "username",
    "auth_password": "password",
    "krisp_enabled": true
  }
}
But if noise cancellation isn’t something you need right now, it’s perfectly fine to leave this setting out. I hope this helps! Let me know if you have any other questions.
m
Auth username and password are my Twilio credentials?
t
Hi there! Just to clarify, the
auth_username
and
auth_password
should be your Twilio SIP credentials. You can usually find these in your Twilio Elastic SIP Trunking settings under the "Authentication" section. Here are a couple of things to double-check: • Make sure the username and password exactly match the credentials you set up in Twilio for your SIP trunk. • Confirm that your Twilio trunk is set to use "Credential List" authentication. If your Twilio trunk uses IP-based authentication instead, you won’t need to enter a username and password. Instead, you’ll want to make sure the allowed IP addresses are properly configured both in Twilio and LiveKit.
m
Don’t I need dispatch rules as well for the inbound handling?
t
Absolutely, to make sure your inbound calls reach the right LiveKit rooms and the appropriate agents, you’ll want to set up dispatch rules. These rules help define how calls coming from your inbound trunk are managed. Let me walk you through an example of creating a dispatch rule:
Copy code
lk sip dispatch create --name "inbound-dispatch-rule" \
  --trunk-id "your-inbound-trunk-id" \
  --type "individual" \
  --room-prefix "call"
Here’s what each part means: • `--trunk-id`: This is the ID of your inbound trunk. • `--type`: This sets the rule type. Using "individual" means a new room will be created for each call. • `--room-prefix`: This adds a prefix to the names of rooms created for inbound calls. Once you have your dispatch rule in place, your inbound calls will be smoothly routed to the right rooms, making it easier for your agents to assist your callers. If you have any questions while setting this up, I’m here to help!
m
What did I do wrong here? --type “individual” \ --room-prefix “call” Incorrect Usage: flag provided but not defined: -trunk-id flag provided but not defined: -trunk-id
t
It looks like the error happened because the correct flag to specify trunks is
--trunks
, not
--trunk-id
. No worries though! Here’s the updated command for you:
Copy code
lk sip dispatch create \
  --name "inbound-dispatch-rule" \
  --trunks "your-inbound-trunk-id" \
  --caller "call-"
Just to clarify: • `--trunks`: This is where you list the trunk IDs (you can separate multiple IDs with commas). • `--caller`: This helps create individual caller dispatches using the room prefix you specify. Give this updated command a try, and it should work smoothly for your setup. If you run into any other questions, I’m here to help! Sources: cmd/lk/sip.go | README.md | Inbound calls with Twilio Voice | LiveKit docs
m
And to be clear, this dispatch rule works such that inbound calls get linked correctly to my telephony agent? I do have two agents actually so I want to make sure both don’t join.
t
Absolutely! The dispatch rule is designed to make sure inbound calls go to the right LiveKit room. To keep things running smoothly and prevent both agents from joining the same call, I recommend using explicit agent dispatch. This simply means you assign a specific agent to handle the call by setting the
agent_name
in your dispatch rule or agent configuration. Here’s a friendly guide to help you ensure only one agent joins: 1. Give each agent a unique name: When you’re setting up your agents, use the
agent_name
parameter to give each one a distinct identity. 2. Configure the dispatch rule to target that specific agent: In your agent logic, check the room metadata or job metadata so only the intended agent joins the room. By following these steps, you’ll make sure the right agent handles each inbound call, keeping everything organized and conflict-free. If you need any more help, I’m here for you!
m
I have this weird bug where I dial the Twilio number- which can already place outbound calls- and it says the number doesn’t exist. The exact wording is your mobile number may not be active when I try to dial the Twilio number. Any thoughts here? I set up the inbound trunk and the Dispatch rule.
t
I'm sorry to hear you're encountering the error "your mobile number may not be active." This usually points to a little hiccup in your SIP trunk or Twilio setup, but don’t worry—we can work through it together. Here are some friendly steps to help you get back on track: 1. Verify Twilio Number Configuration: First, let's make sure your Twilio number is active, has voice capabilities enabled, and is linked to the right SIP trunk or TwiML Bin in your Twilio Console. 2. Check SIP Credentials: Double-check that the username and password in your LiveKit inbound trunk exactly match the SIP credentials you’ve set up in Twilio. 3. Inspect Twilio Logs: Head over to Monitor → Logs → Calls in the Twilio Console to see if there are any detailed error messages or SIP status codes like 403 or 404 that can give us clues. 4. Validate SIP URI: Make sure the SIP URI in Twilio is formatted correctly, including
;transport=tcp
, and that it’s pointing to your LiveKit SIP endpoint. 5. Test with a Simple Setup: Try using a basic TwiML Bin to route calls to your SIP trunk. This can help isolate where the issue might be. If you’re still running into trouble, take another look at those Twilio call logs for specific error codes or messages. Feel free to reach out anytime—I’m here to help you through this! Sources: packages/sip--um-actually/README.md | SIP troubleshooting guide | LiveKit docs | README.md | Inbound calls with Twilio Voice | LiveKit docs
m
Getting this error in the Twilio logs. Trunking originating. Direction Trunking Originating Type sip-pstn Trunk SID (SID here) Status Failed SIP PCAP Log Stir/Shaken Status A
t
I see that the "Failed" status in your Twilio logs, along with the STIR/SHAKEN status "A," might be pointing to a little hiccup with your SIP trunk configuration or caller ID verification. Don’t worry, I’m here to help you work through this! Here are some friendly steps to guide you: 1. Verify SIP Credentials: Let’s double-check that the username and password in your LiveKit inbound trunk match the SIP credentials you’ve set up in Twilio. 2. Check Caller ID: Make sure the caller ID (phone number) you’re using is properly verified and registered in your Twilio account to keep things smooth with STIR/SHAKEN compliance. 3. Review Twilio Logs: Take a look for any specific SIP status codes (like 403 or 404) in your Twilio logs—they can give us clues about what’s going on. 4. Validate Trunk Configuration: Confirm that your Twilio phone number is correctly linked to the SIP trunk and that the trunk’s origination URL is pointing to your LiveKit SIP endpoint with
;transport=tcp
. 5. STIR/SHAKEN Compliance: It’s also good to verify that your Twilio account and SIP trunk are set up with the right STIR/SHAKEN attestation level. If you can share any specific SIP status codes or error messages from your Twilio logs, I’d be happy to help you dig deeper and find a solution together. Sources: Agents telephony integration | LiveKit Docs | livekit-protocol/livekit/protocol/sip.pyi | SIP troubleshooting guide | LiveKit docs | README.md | Create and configure a Twilio SIP trunk | LiveKit docs
m
This is a SIP authentication failure. Here’s what’s happening: The Issue The call is being rejected with a
407 Proxy Authentication Required
response from LiveKit, meaning LiveKit is asking for authentication credentials that Twilio isn’t providing (or is providing incorrectly). The Flow 1. INVITE - Twilio sends call setup request to LiveKit 2. 100 Processing - LiveKit acknowledges receipt 3. 407 Unauthorized - LiveKit rejects with authentication challenge: 4. Proxy-Authenticate: Digest realm=“LiveKit”, nonce=“1763354615328833", algorithm=MD5 5. ACK - Twilio acknowledges the rejection What’s Wrong Twilio’s initial INVITE has no authentication credentials. LiveKit expects digest authentication but the request arrives without any
Proxy-Authorization
header. How to Fix You need to configure authentication in your Twilio SIP trunk settings: 1. In Twilio Console → Your SIP Trunk → Authentication ◦ Add LiveKit’s authentication credentials (username/password) ◦ These should match what LiveKit expects for the realm “LiveKit” 2. Or in LiveKit → SIP settings ◦ Configure to accept calls from Twilio’s IP addresses without authentication ◦ Add Twilio’s IP ranges to an allowlist The typical fix is option 1 - configure Twilio with the correct SIP credentials that LiveKit is expecting. Check your LiveKit SIP trunk configuration for the username/password that should be used.
t
It looks like the issue is a SIP authentication failure because Twilio isn’t sending the required credentials in the initial INVITE request. Don’t worry, I’m here to help you through the steps to fix this: 1. Set Up Authentication in Twilio: ◦ Head over to your Twilio Console → Your SIP Trunk → Authentication. ◦ Enter the username and password that match what you have set up in your LiveKit SIP trunk. 1. Or, Adjust LiveKit to Trust Twilio’s IPs: ◦ In your LiveKit SIP settings, you can configure the trunk to accept calls from Twilio’s IP addresses without needing authentication. ◦ Just add Twilio’s IP ranges to the allowlist in LiveKit. Usually, the easiest solution is to make sure Twilio has the correct SIP credentials that LiveKit expects. Take a quick look at your LiveKit SIP trunk configuration to find the right username and password to use.