miniature-nail-35141
10/24/2025, 11:25 AMtall-airline-16756
10/24/2025, 11:25 AMtall-airline-16756
10/24/2025, 11:26 AMfunction_tool in LiveKit might feel a bit tricky since the system is primarily designed for LLM-driven tool invocation. But don’t worry, there are some alternative ways you can try:
• Call the underlying function directly: Since the @function_tool decorator wraps a regular function, you can simply call that function directly in your code without going through the tool system.
• Force tool usage: You can use the tool_choice parameter to encourage the LLM to invoke specific tools during your session.
• Dynamic tool updates: It’s possible to modify the available tools on the fly by using agent.update_tools() or session.update_tools().
• Event-driven handling: You might find it helpful to use the function_tools_executed event to respond to tool calls or even trigger additional actions programmatically.
If you’d like, I’m here to walk you through any of these methods in more detail or provide some examples. Just let me know!
Sources: Docs::Agents | livekit-plugins/livekit-plugins-google/livekit/plugins/google/realtime/real... | tool_calling/call_function_tool.pyminiature-nail-35141
10/24/2025, 11:28 AMtall-airline-16756
10/24/2025, 11:30 AMminiature-nail-35141
10/24/2025, 11:32 AMtall-airline-16756
10/24/2025, 11:32 AMon_exit method, it doesn't automatically trigger a function_tool. Usually, tool execution in LiveKit happens during active conversation turns and LLM inference, rather than from changes made to the chat context outside of those active moments.
If you're looking to trigger a tool right when the session ends, a good approach might be to explicitly call the tool logic inside the on_exit method or handle it during an active turn, like in on_user_turn_completed. I'm here to help if you'd like some more detailed guidance on how to set this up!
Sources: tests/test_agent_session.pyminiature-nail-35141
10/24/2025, 11:39 AMend_call function tool) or when the user unexpectedly disconnects or interrupts the session.
What is the best way to detect these unexpected disconnections in a LiveKit Agent and trigger a cleanup or logging function (e.g., saving call summary and sentiment analysis) when that happens?tall-airline-16756
10/24/2025, 11:40 AMclose event: This event fires when the session ends—whether it’s because the user left, an error occurred, or there was a disconnection. It’s a great spot to handle any cleanup or logging you might need.
• Handle the error event: This captures any errors during the session, including ones that can’t be recovered from. You can log important details here and prepare for cleanup.
• Use shutdown hooks: By registering a shutdown callback with ctx.add_shutdown_callback, you can make sure final cleanup tasks—like saving call summaries, sentiment scores, or freeing up resources—are done right after the session ends.
Here’s a simple example of how you might use the close event:
@session.on("close")
def on_close(event):
if event.error:
logger.error(f"Session closed due to error: {event.error}")
# Trigger error-specific cleanup
else:
logger.info("Session closed normally")
# Trigger normal cleanup
And for wrapping things up nicely, you can add a shutdown hook like this:
async def entrypoint(ctx):
ctx.add_shutdown_callback(on_shutdown)
async def on_shutdown():
logger.info("Performing cleanup operations...")
# Save session data, close connections, etc.
If you’d like, I’m here to help you with more details or guide you through implementing this step-by-step. Just let me know!
Sources: Events and error handling | LiveKit Docs | livekit-agents/livekit/agents/worker.py | Job lifecycle | LiveKit Docs | Operational Guidance & Best Practices | livekit/livekit_composite | DeepWik... | Node.js Agents Architecture | livekit/livekit_composite | DeepWiki