miniature-nail-35141
10/24/2025, 4:50 AMtall-airline-16756
10/24/2025, 4:50 AMtall-airline-16756
10/24/2025, 4:50 AMadd_shutdown_callback mechanism in LiveKit is thoughtfully designed to help with cleanup tasks during shutdown, like logging or disconnecting resources. However, it can’t directly trigger a function tool in the LLM sense, since function tools need an active session and interaction with the LLM.
If you want to run the same logic as a function tool during shutdown, a good approach is to put that core logic into a separate async function. Then, you can call this function both from the tool and the shutdown callback. Here’s a simple example to illustrate:
async def perform_cleanup_logic():
# Your actual cleanup logic (e.g., API call, DB update)
logger.info("Performing final cleanup...")
@function_tool
async def cleanup_tool():
"""Allow the LLM to trigger cleanup."""
await perform_cleanup_logic()
return "Cleanup initiated."
async def shutdown_handler(reason: str):
await perform_cleanup_logic()
ctx.add_shutdown_callback(shutdown_handler)
This way, your cleanup logic stays reusable and consistent in both places. If you have any questions or want to explore this further, please don’t hesitate to ask—I’m here to help!
Sources: livekit-plugins/livekit-plugins-aws/livekit/plugins/aws/experimental/realti... | chain-of-thought-tts/agent.pyminiature-nail-35141
10/24/2025, 4:53 AMtall-airline-16756
10/24/2025, 4:53 AMJobContext (which is often used in LiveKit) from within a shutdown function. You can definitely do this by defining your shutdown callback inside the same scope as the JobContext and registering it with add_shutdown_callback. Here's a simple example to guide you:
async def entrypoint(ctx: JobContext):
await ctx.connect()
async def on_shutdown(reason: str):
print(f"Shutting down: {reason}")
# You can access JobContext properties here
await ctx.room.local_participant.publish_data(b"Goodbye!", 1)
ctx.add_shutdown_callback(on_shutdown)
This way, you'll be able to access all the JobContext properties like ctx.room, ctx.job, and ctx.agent during the shutdown process.
If you ever need to access the context globally, you can use get_job_context() in Python or getJobContext() in Node.js, just keep in mind this works only when a job session is active. Feel free to reach out if you'd like me to walk you through this further or if you have any other questions—I’m here to help!
Sources: Node.js Agents Architecture | livekit/livekit_composite | DeepWiki | agents/src/job.ts | livekit-agents/livekit/agents/voice/agent_session.py | Operational Guidance & Best Practices | livekit/livekit_composite | DeepWik...