For livekit agent, when is on_exit node called?
# ask-ai
f
For livekit agent, when is on_exit node called?
t
Thanks for reaching out, Mok. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
Hi there! Just to help you understand better, the
on_exit
node for a LiveKit agent is called right before the agent hands over control to another agent within the same session. This usually happens during an agent handoff or transition, which can be done either manually through workflow logic or programmatically using tools like
llm.handoff()
. It's important to note that this doesn’t get triggered when the session itself ends—only when the agent is being replaced by another agent during the ongoing session. People often use it for tasks like cleanup, saving data, or even generating a friendly final message like "Goodbye!" before exiting. I hope this helps clarify things for you! Sources: Pipeline nodes and hooks | LiveKit Docs | Docs::Agents | README.md | livekit-agents/livekit/agents/voice/agent_session.py | docs/index.yaml
f
is there anyway to use it with the usecase of generating a friendly final message before closing the session room?
t
I understand you're looking to send a final message before closing the session room. The
on_exit
node isn’t quite meant for that since it’s triggered during agent handoffs within the same session. But don’t worry, there are some great alternatives you can try: • Explicitly generate a message before closing the session: You can use
self.session.generate_reply()
or
self.session.say()
to warmly deliver a final message right before calling session termination methods like
self.session.aclose()
or deleting the room. • Use the
session.on("close")
event handler:
By registering a callback for the
"close"
event, you can handle any post-processing, just keep in mind the message needs to be spoken before the session actually closes. • Use shutdown callbacks: For cleanup or other tasks after the session ends,
ctx.add_shutdown_callback()
is a handy tool. If you’d like, I’m more than happy to walk you through an example implementation to make sure your final message is delivered before the room closes. Just let me know! Sources: Docs::Agents | Pipeline nodes and hooks | LiveKit Docs | main.py | livekit-agents/livekit/agents/inference/stt.py | Docs::Agents
f
• Use the
session.on("close")
event handler:
By registering a callback for the
"close"
event, you can handle any post-processing, just keep in mind the message needs to be spoken before the session actually closes. How do i ensure the message is spoken before i close the session?? Currently, how do i enforce such that the agent session is able to close the session after the time limit has reached?
t
I don't have the answer you're looking for. You could also try asking your question: • in one of the other Slack channels or • to https://deepwiki.com/livekit/livekit_composite which is trained on all LiveKit source code If you find the answer, please post it here to help others!