Can I close the room from agent's `self.session` o...
# ask-ai
r
Can I close the room from agent's
self.session
or some other way through agent?
t
Thanks for reaching out, Ivan. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
I'm happy to help you with closing a room in LiveKit! There are two ways to do this, depending on what you want to achieve: • Disconnecting the Agent Only: You can use
self.session.shutdown()
to gracefully stop the agent session without affecting other participants in the room. Here’s a simple example:
Copy code
@llm.function_tool
async def close_session(self):
    await self.session.generate_reply(instructions="say goodbye to the user")
    self.session.shutdown()
• Closing the Entire Room: If you want to disconnect everyone, you can use the LiveKit Server API to call
delete_room
. Here’s how you might do it:
Copy code
from livekit.api import LiveKitAPI, DeleteRoomRequest

lkapi = LiveKitAPI(
    url="https://your-livekit-server-url",
    api_key="your-api-key",
    api_secret="your-api-secret"
)
await lkapi.room.delete_room(DeleteRoomRequest(room="my-room-name"))
await lkapi.aclose()
Another helpful option is to set
delete_room_on_close=True
in
RoomInputOptions
, which will automatically delete the room when the agent session closes. If you have any questions or need more help, please don’t hesitate to ask. I’m here to support you! Sources: Job lifecycle | LiveKit Docs | Managing rooms | LiveKit docs | livekit-agents/livekit/agents/voice/agent_session.py