In livekit agents, how to send reason param in shu...
# ask-ai
n
In livekit agents, how to send reason param in shutdown callback?
t
Thanks for reaching out, Tareq. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
To send a reason parameter in the shutdown callback for LiveKit agents, follow these steps: 1. Call
shutdown()
with a reason:
Copy code
ctx.shutdown('Custom shutdown reason');
1. Add a shutdown callback to receive the reason:
Copy code
ctx.addShutdownCallback(async (reason) => {
    console.log('Shutdown reason:', reason);
    // Perform cleanup tasks here
});
The shutdown callbacks are executed in the order they were added, and they must return a
Promise<void>
to ensure proper cleanup. For example:
Copy code
async function main(ctx: JobContext) {
    // Add shutdown callback
    ctx.addShutdownCallback(async (reason) => {
        console.log('Cleaning up with reason:', reason);
        await cleanupOperations();
    });

    // Later, shutdown with a reason
    ctx.shutdown('Normal shutdown');
}
All callbacks are awaited sequentially, ensuring proper execution before the job terminates.
n
how to retrieve it in the callback funtion python
t
Hey there! If you want to grab the
reason
parameter in your shutdown callback function for LiveKit agents using Python, here’s a simple way to do it: 1. First, make sure your shutdown callback function is set up to accept the
reason
parameter:
Copy code
async def my_shutdown_callback(reason: str):
    print(f"Shutdown reason: {reason}")
1. Next, register your callback with
JobContext.add_shutdown_callback
like this:
Copy code
ctx.add_shutdown_callback(my_shutdown_callback)
1. Finally, when you want to shut down, call shutdown with a reason:
Copy code
ctx.shutdown("Session ended")
And that’s it! When the job shuts down, your callback gets triggered and the
reason
string is passed along. This way, you can easily handle whatever shutdown reason you need.