Agent disconnects when room becomes empty - how to prevent?

This question originally came up in our Slack community and the thread has been consolidated here for long-term reference.

Our application uses LiveKit agents for server-side logic (not AI). The agent connects when a room is created, but if all users leave, the agent disconnects before the room is destroyed.

If a user joins during that window, the agent won’t be dispatched and the application breaks.

What’s the best approach for keeping the agent connected?

You have several options:

  1. Delete the room when the last participant leaves: Monitor the participant disconnect event and have your agent delete the room.

  2. Use unique room names each time: This ensures a fresh room and agent dispatch for each session.

  3. Set close_on_disconnect to false: This prevents the agent from disconnecting when participants leave.
    agents/livekit-agents/livekit/agents/voice/room_io/room_io.py at main · livekit/agents · GitHub

  4. Use delete_room_on_close: This deletes the room when the AgentSession is closed (default is False).
    agents/livekit-agents/livekit/agents/voice/room_io/room_io.py at main · livekit/agents · GitHub