When I use this example from current documentation...
# ask-ai
h
When I use this example from current documentation:
Copy code
class 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?
t
Thanks for reaching out, Pavel. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
Along with the
onEnter
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.py
h
I have a function in tools, when that function is finished (agent starts it), I need to close connection and delete room, how to do that
t
I'm glad to help you with closing the connection and deleting the room after your function in tools finishes. Here’s a simple way to do it: • To disconnect just the agent: You can use
ctx.shutdown()
. This will disconnect the agent but keep the room open for others. Here’s an example:
Copy code
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:
Copy code
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:
Copy code
@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.py
I'm glad you're looking to close the connection and delete a room after your function in tools finishes! Here's a simple way to do it: • If you want to disconnect just the agent: You can use
ctx.shutdown()
. This will disconnect the agent but keep the room open for others to continue. Here's a quick example:
Copy code
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:
Copy code
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!