if we get a twirperror when calling create_sip_par...
# ask-ai
i
if we get a twirperror when calling create_sip_participant, is it recommended to call ctx.shutdown() manually?
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.
Absolutely, it’s a good idea to call
ctx.shutdown()
manually after you come across a
TwirpError
when using
create_sip_participant
. This helps make sure the agent context closes properly and prevents any hiccups with managing its lifecycle. If you’d like, I’m here to help clarify anything or assist you further—just let me know! Sources: Docs::Agents | agent.py | tests/test_ipc.py
i
will that conflict with livekit shutting down things by itself when participant disconnects?
t
Hey there! Just to reassure you, calling
ctx.shutdown()
manually won’t interfere with LiveKit’s automatic shutdown when a participant disconnects. They’re both designed to work smoothly together, so the framework takes care of everything properly no matter which one triggers the shutdown. Also, feel free to call
ctx.shutdown()
multiple times—it’s completely safe because the framework handles it gracefully. If you have any more questions or need a hand with anything else, just let me know—I’m here to help! Sources: Inside a session | LiveKit Docs | Making calls using SIP | LiveKit Docs | livekit-plugins/livekit-plugins-aws/livekit/plugins/aws/experimental/realti...
i
does livekit automatically shutdown the call/room when the last remote participant leaves?
t
Hey there! Just to let you know, LiveKit automatically closes a room when the last participant leaves, but don’t worry—it waits a little bit first. This waiting period is controlled by the
departure_timeout
setting, which is set to 20 seconds by default. This gives a nice little buffer in case someone’s connection drops temporarily and they want to come back. Also, if a room is created but no one joins within the
empty_timeout
period (which is 300 seconds by default), the room will close automatically too. You can always tweak these settings in the LiveKit server settings to fit your needs. If you have any questions about this, I’m here to help! Sources: config-sample.yaml
i
ok so what we do is we make an outbound sip call with create_sip_participant, if we receive a sip response thats something temporary, like a 480 or 486, we will retry and call create_sip_participant again. however, in the first attempt of create_sip_participant, a participant will connect and disconnect even though the sip call doesnt go through - should we be concerned that the disconnect event will mess with further retries of create_sip_participant?
t
I completely understand how these technical details can feel a bit tricky! When a
create_sip_participant
call fails and causes a disconnect event, don’t worry—this won’t affect your next retries. Each call is handled independently, and the disconnect is just part of tidying things up after a failure. To keep everything running smoothly and avoid any hiccups like duplicate participants, it’s a good idea to use unique participant identities for each retry or make sure any leftover participant data from earlier attempts is cleared out before trying again. If you’d like, I’m here to walk you through this step-by-step or answer any questions you have—just let me know! Sources: Making outbound calls | LiveKit Docs | test/integration/sip_test.go | livekit-agents/livekit/agents/job.py | sip-refer/agent.py | pkg/service/sip.go
i
sure but i guess im concerned that livekit will start shutting down the room after the first disconnect event, even though were trying new outbound calls, does that make sense?
t
I totally understand how important it is to keep things running smoothly! Just so you know, LiveKit won’t shut down the room after a disconnect event from a failed
create_sip_participant
attempt, as long as there are other active participants still connected. The room stays open as long as at least one participant (like an agent session or another connected user) is there. This way, you can comfortably retry outbound calls without worrying about the room closing too soon. If you’d like me to explain anything further or have any other questions, please don’t hesitate to ask—I’m here to help! Sources: telephony/sip_lifecycle.py | agent.py | Making outbound calls | LiveKit Docs | Docs::Telephony | examples/warm-transfer/warm_transfer.py
i
gotcha, so im a bit confused - i thought that livekit would shut down automatically after the last remote participant disconnected? the agent session isnt a remote participant is it?
t
You're absolutely right! LiveKit will automatically close the room once the last non-agent participant, like a remote or SIP participant, disconnects. Agent sessions don't count as participants to keep the room open. So, when all non-agent participants leave, the room will close, and any agents still connected will be disconnected as well. If you have any more questions or need a hand with anything else, just let me know—I'm here to help! Sources: Anatomy of an Agent | LiveKit Docs | Job lifecycle | LiveKit Docs | livekit-ffi/src/livekit.proto.rs
i
exactly so back to my original question: ok so what we do is we make an outbound sip call with create_sip_participant, if we receive a sip response thats something temporary, like a 480 or 486, we will retry and call create_sip_participant again. however, in the first attempt of create_sip_participant, a participant will connect and disconnect even though the sip call doesnt go through - should we be concerned that the disconnect event will mess with further retries of create_sip_participant?
t
I understand how important it is to keep your room running smoothly, so here’s some helpful info: when the last remote participant disconnects during a failed
create_sip_participant
attempt, the room won’t automatically shut down or interfere with retries—this holds true as long as the room itself isn’t explicitly deleted. LiveKit only shuts down a room when all Standard and Ingress participants leave, and SIP participant disconnections during call setup don’t trigger room closure by default. To make sure your retries go smoothly, try to avoid calling
delete_room
too early, keep an eye on
sip.callStatus
to track call outcomes, and handle exceptions like
TwirpError
to figure out when retries are appropriate. If you’d like, I’d be happy to share an example of retry logic or clarify anything further—just let me know! Sources: livekit-agents/livekit/agents/job.py | Docs::Telephony | Docs::Telephony | agent.py | telephony/sip_lifecycle.py