how to make the agent end the session?
# ask-ai
c
how to make the agent end the session?
t
Thanks for reaching out, ZaidKaraymeh. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
When it’s time to wrap up a session in LiveKit, you’ve got a couple of straightforward options depending on what you need: • Disconnecting the Agent Only: Just call
ctx.shutdown()
inside the agent’s entrypoint function. This will gently disconnect the agent from the room but keep everyone else connected. Here’s a quick example:
Copy code
async def entrypoint(ctx: JobContext):
    # Perform work
    ...

    # Disconnect the agent from the room
    ctx.shutdown(reason="Session ended")
• Ending the Session for Everyone: If you want to close things down for all participants, use the server API
delete_room
. This will disconnect everyone and remove the room. Check out this example:
Copy code
from livekit import api

async def entrypoint(ctx: JobContext):
    # Perform work
    ...

    # End the session for everyone
    api_client = api.LiveKitAPI(
        os.getenv("LIVEKIT_URL"),
        os.getenv("LIVEKIT_API_KEY"),
        os.getenv("LIVEKIT_API_SECRET"),
    )
    await api_client.room.delete_room(
        api.DeleteRoomRequest(room=ctx.job.room.name)
    )
If you have any questions or want me to walk you through anything, just let me know—I’m here to help! Sources: basics/exit_message.py | Job lifecycle | LiveKit Docs | Inside a session | LiveKit Docs | Agents telephony integration | LiveKit Docs | livekit-plugins/livekit-plugins-aws/livekit/plugins/aws/experimental/realti...
c
i want to use a function tool
t
If you want to end a session using a function tool in LiveKit, you’ve got two friendly options: you can either disconnect just the agent or end the session for everyone involved. Here’s how: • Disconnecting the Agent Only: You can use
session.aclose()
inside a function tool to gracefully close the agent’s session while keeping the room open for others. Check out this example:
Copy code
@function_tool\nasync def end_session(self):\n    """End the agent session."""\n    await self.session.drain()\n    await self.session.aclose()
• Ending the Session for Everyone: If you want to wrap things up for all participants, use the server API
delete_room
. Here’s an example of how to do that:
Copy code
@function_tool\nasync def end_call(self, ctx: RunContext):\n    """End the call for all participants."""\n    # Let the current speech finish\n    await ctx.wait_for_playout()\n    # Say goodbye\n    await ctx.session.generate_reply(instructions="Goodbye!")\n    # Delete the room\n    job_ctx = get_job_context()\n    await job_ctx.api.room.delete_room(api.DeleteRoomRequest(room=job_ctx.room.name))
If you have any questions or want a hand with setting this up, just let me know—I’m here to help! Sources: Agents telephony integration | LiveKit Docs | Agent speech and audio | LiveKit Docs | telephony/sip_lifecycle.py | Job lifecycle | LiveKit Docs | agent.py
c
format the code well!
t
Here’s the code with a bit of tidying up to make it clearer for you: 1. Disconnecting the Agent Only:
Copy code
@function_tool
async def end_session(self):
    """End the agent session."""
    # Let any ongoing speech finish
    await self.session.drain()
    # Then close the session
    await self.session.aclose()
2. Ending the Session for Everyone:
Copy code
@function_tool
async def end_call(self, ctx: RunContext):
    """End the call for all participants."""
    # Wait for the current speech to finish
    await ctx.wait_for_playout()
    # Say a friendly goodbye
    await ctx.session.generate_reply(instructions="Goodbye!")
    # Finally, delete the room
    job_ctx = get_job_context()
    await job_ctx.api.room.delete_room(api.DeleteRoomRequest(room=job_ctx.room.name))
If you’d like me to walk you through anything else or need more examples, just let me know—I’m here to help!