im noticing that sometimes, when using create_sip_...
# ask-ai
i
im noticing that sometimes, when using create_sip_participant with wait_until_answered=True, we never get a sip response (the request hangs) is that normal?
t
Thanks for reaching out, Ed. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
I completely understand how it can be a bit tricky when using
create_sip_participant
with
wait_until_answered=True
, especially if the request seems to hang without receiving a SIP response. This happens because the request is patiently waiting for the call to be answered, which is expected behavior. To help avoid waiting indefinitely, it's a good idea to set a timeout. Here are some friendly tips to guide you through this: • Try setting a
ringing_timeout
in your request to limit how long the system waits for the call to connect. • Make sure to explicitly set a client timeout when making the API call to manage the total request time. • Turning on debug logging can be really helpful—it lets you see detailed error messages and check SIP-specific info like
sip_status_code
and
sip_status
. • Double-check your SIP trunk configuration and confirm that the target phone number is valid and reachable. • You might also want to use
play_dialtone=True
to give users some feedback while the call is dialing. If you'd like, I’m here to help you walk through any of these steps or answer any questions you have. Just let me know! Sources: Docs::Telephony | agent.py | cmd/lk/sip.go | pkg/service/sip.go | livekit-api/livekit/api/sip_service.py
i
well actually, what im noticing is that the call is answered, and sometimes even a conversation can occur, but we still dont get a response. this is problematic because if the call fails before the timeout expires, we'll get a request failure
t
I understand how frustrating it can be when a call is answered but no response comes through, and the request fails before the timeout. This often points to some challenges with SIP response handling or protocol mismatches. Let’s walk through some steps that might help: • Take a look at the
sip_status_code
and
sip_status
in the error metadata to pinpoint what’s causing the failure. • Try enabling detailed logging to catch any SIP-specific errors and keep an eye on participant events like
ringing
and
active
. • Double-check that your SIP trunk is set up correctly, including the authentication credentials and making sure it’s compatible with LiveKit’s SIP headers. • Consider adjusting the client-side timeout to match what the server expects (around 80+ seconds) and set a
ringingTimeout
to control how long the system waits for an answer. • It might also help to test without
wait_until_answered
so you can watch the call’s progress asynchronously and better isolate where things might be going wrong. Please feel free to reach out if you’d like a hand with any of these steps or if anything needs more explanation—I’m here to help! Sources: agent.py | livekit-rtc/livekit/rtc/participant.py | pkg/service/sip.go | cmd/lk/sip.go | Making outbound calls | LiveKit Docs
i
is there a best practice way for triggering session shutdown?
t
Thank you for reaching out! Could you please help me understand a bit more about what you mean by "session shutdown"? Are you looking to end a specific participant's session, close a room, or maybe shut down a server instance? The more details you can share, the better I can assist you. I'm here to help!
i
oh i meant agent session. let me be more specific - there are certain situations in which we want to end the call, is best practice to just delete the room?
t
To gracefully end an agent session or call in LiveKit, here are some friendly best practices to guide you: • Disconnecting the Agent Only: You can use
ctx.shutdown(reason="Session ended")
to gently disconnect the agent while keeping the room open for other participants to continue. • Ending the Session for All Participants: When it’s time to wrap up for everyone, the
delete_room
API is your go-to. Before doing so, consider using
ctx.wait_for_playout()
or
session.drain()
to make sure no audio cuts off abruptly. You might also want to send a warm goodbye message using
session.say()
. • Graceful Cleanup: It’s a great idea to implement the
on_exit
method in your agent class. This helps you perform those final touches, like saying goodbye or logging important session details. • Handling Session Closure Events: Don’t forget to subscribe to the
"close"
event on
AgentSession
. This way, you can log or process session history smoothly once the session ends. • For Egress Recordings: To avoid any unwanted silent audio at the end of your recordings, deleting the room right after the human participant leaves works best. If you’d like, I’m here to help you implement any of these steps or answer any questions you might have. Just let me know! Sources: Inside a session | LiveKit Docs | Agents telephony integration | LiveKit Docs | telephony/sip_lifecycle.py | livekit-agents/livekit/agents/voice/agent_activity.py
i
and that will automatically clean up the agent session task>
t
I'm glad you asked! When you use
delete_room
, it automatically takes care of triggering the necessary server-side events to clean up the agent session task. So, when a room is deleted, all participants—including agents—are removed from the room. This action triggers the
participantLeft()
method on the agent job, and the server sends a
TerminateJob
RPC to stop the agent task. Also, if you have configurations like
delete_room_on_close
set to
True
, the room will be deleted automatically when the agent session closes. Please feel free to reach out if you'd like me to walk you through this process in more detail or if you have any other questions. I'm here to help! Sources: Job lifecycle | LiveKit Docs | Inside a session | LiveKit Docs | pkg/service/roommanager.go | livekit-agents/livekit/agents/voice/agent_session.py | complex-agents/role-playing/agents/combat_agent.py
i
do you need to call ctx.wait_for_playout() or session.drain() along with deleting the room? as in, do you have to do both wait for playout (or drain session) and delete the room?
t
I totally understand where you're coming from! While calling
ctx.wait_for_playout()
or
session.drain()
before
delete_room
isn’t absolutely mandatory, it’s definitely a great habit to get into. These steps help make sure the agent finishes speaking and all messages are fully sent before the room is deleted, so you avoid any sudden audio cutoffs or missed messages. Here’s a simple way to wrap things up smoothly: 1. Start with
await ctx.wait_for_playout()
to let the agent finish its speech. 2. If you want, you can then call
await session.drain()
to make sure all pending actions are handled. 3. Finally, call
delete_room
to end the session. If anything’s unclear or you’d like a hand putting this into practice, just let me know—I’m here to help! Sources: Agents telephony integration | LiveKit Docs | agent.py | tracking_state/npc_character.py | agents/src/voice/agent_session.ts | Agent speech and audio | LiveKit Docs
i
oh ok so the correct order is ctx.wait_for_playout() then session.drain() then delete the room?
t
Absolutely! To gracefully end the session, here’s the best order to follow: 1. Start by calling
ctx.wait_for_playout()
so the agent can finish speaking without any interruptions. 2. Next, call
session.drain()
to make sure all pending actions or messages are properly processed and sent. 3. Finally, call
delete_room
to close the session and clean up everything neatly. Following these steps will help you wrap things up smoothly and professionally. If you have any questions or want me to walk you through it, just let me know—I’m here to help!
i
could calling session.drain() make things hang though?
t
Thanks for reaching out! Could you please help me understand a bit more about what you mean by "hanging"? Are you noticing that the method is taking a really long time to finish, or does it not return at all? Also, are there particular situations or conditions where this seems to happen? I’m here to help you get this sorted out!