wide-dress-57902
08/16/2025, 9:56 PMtall-airline-16756
08/16/2025, 9:56 PMtall-airline-16756
08/16/2025, 9:57 PMshutdown
method in JobContext
is there to help you wrap up a job smoothly and run any shutdown callbacks you've set up. Here’s what it does for you:
• Graceful Shutdown: Makes sure the job finishes cleanly, taking care of all the necessary cleanup.
• Execution of Shutdown Callbacks: Runs all the callbacks you’ve added with add_shutdown_callback()
, like freeing resources or saving your progress.
• Disconnect from Room: Safely disconnects the agent from the room so others can keep going without a hitch.
• Optional Reason Parameter: Lets you pass in a reason for the shutdown, which is super helpful for logging or debugging.
Here’s a quick example to show you how it works:
async def entrypoint(ctx: JobContext):
try:
# Do some work...
await ctx.connect()
# ...
finally:
ctx.shutdown(reason="Session completed")
This way, even if something goes wrong, the shutdown callbacks still run, and the agent disconnects nicely.wide-dress-57902
08/16/2025, 9:58 PMtall-airline-16756
08/16/2025, 9:58 PMdelete_room
and `shutdown`—they each have their own role depending on what you’re aiming to do:
• `delete_room`: This one wraps up the call for everyone right away. It’s perfect if you want to completely end the session for all participants. For example: await ctx.api.room.delete_room(api.DeleteRoomRequest(room=ctx.room.name))
.
• `shutdown`: This method only disconnects the AI agent from the room, letting everyone else keep chatting. For instance: ctx.shutdown(reason="Session ended")
.
Here’s a little tip:
• If you want to end the call for everyone, go with delete_room
.
• If you just want the AI agent to leave but keep the call going for others, shutdown
is your friend.
Usually, you’ll use one or the other depending on whether you want to close out the whole session or just have the agent step away. Hope that clears things up!
Sources: Agents telephony integration | LiveKit Docs | Inside a session | LiveKit Docs | Job lifecycle | LiveKit Docs | Inside a session | LiveKit Docswide-dress-57902
08/16/2025, 10:01 PMtall-airline-16756
08/16/2025, 10:01 PMshutdown
method in JobContext
isn’t a coroutine, so you don’t need to use await
with it. It’s a regular method you can call synchronously. Here’s a quick example:
ctx.shutdown("Optional shutdown reason")
Even though this method will trigger any shutdown callbacks you’ve registered (and some of those might be asynchronous), the shutdown
method itself doesn’t require an await
. Hope that clears things up!
Sources: livekit-agents/livekit/agents/worker.py | agents/src/ipc/job_proc_lazy_main.ts