happy-night-10979
09/11/2025, 3:26 PMclass CustomerServiceAgent extends voice.Agent<MySessionInfo> {
constructor() {
super({
instructions: 'You are a friendly customer service representative.',
});
}
async onEnter(): Promise<void> {
const userData = this.session.userData;
this.session.generateReply({
instructions: `Greet ${userData.userName} personally and offer your assistance.`,
});
}
}
I can see onEnter function, do exist also other functions of this type, for example onExit?tall-airline-16756
09/11/2025, 3:26 PMtall-airline-16756
09/11/2025, 3:27 PMonEnter
function, the LiveKit voice.Agent
class also offers some other helpful lifecycle methods that you might find useful:
• onExit
: This method is called just before the agent hands over control to another agent within the same session. It’s a great place to do any cleanup, save important data, or even create a warm farewell message.
• onUserTurnCompleted
: This one triggers right after the user finishes speaking but before the agent starts responding. It gives you the opportunity to adjust the chat context or even cancel the agent’s reply if needed.
You can override these methods in your custom agent subclasses to tailor the behavior exactly how you want. If you have any questions or want to explore this further, I’m here to help!
Sources: agents/src/voice/agent_activity.ts | Docs::Agents | Pipeline nodes and hooks | LiveKit Docs | Pipeline nodes and hooks | LiveKit Docs | basics/exit_message.pyhappy-night-10979
09/11/2025, 3:44 PMtall-airline-16756
09/11/2025, 3:44 PMctx.shutdown()
. This will disconnect the agent but keep the room open for others. Here’s an example:
async def entrypoint(ctx: JobContext):
# Perform work
...
ctx.shutdown(reason="Session ended")
• If you want to delete the room and end the session for everyone: Use ctx.delete_room()
to remove the room and disconnect all participants. Here’s how:
async def entrypoint(ctx: JobContext):
# Perform work
...
await ctx.delete_room()
To ensure everything goes smoothly, it’s a good idea to wait until any ongoing speech finishes before ending the session. You can do it like this:
@function_tool
async def end_call(self, ctx: RunContext):
current_speech = ctx.session.current_speech
if current_speech:
await current_speech.wait_for_playout()
await ctx.job_ctx.delete_room()
If you have any questions or need more help, please don’t hesitate to ask. I’m here to support you!
Sources: Inside a session | LiveKit Docs | livekit-agents/livekit/agents/job.py | Agents telephony integration | LiveKit Docs | mcp/server.py | agent.pytall-airline-16756
09/11/2025, 3:44 PMctx.shutdown()
. This will disconnect the agent but keep the room open for others to continue. Here's a quick example:
async def entrypoint(ctx: JobContext):
# Perform work
...
ctx.shutdown(reason="Session ended")
• If you'd like to delete the entire room (ending the session for everyone): You can use ctx.delete_room()
. This will delete the room and disconnect all participants. Here's how it looks:
async def entrypoint(ctx: JobContext):
# Perform work
...
await ctx.delete_room()
Also, if you prefer, you can use the LiveKitAPI
client to delete the room directly. Please feel free to ask if you'd like me to share more details or examples—I’m here to help!