Can I disconnect the agent as soon as the particip...
# ask-ai
b
Can I disconnect the agent as soon as the participant disconnects?
t
Thanks for reaching out, Martín. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
Yes, you can configure the agent to disconnect automatically when a participant disconnects in LiveKit. This behavior is managed through several mechanisms: • Automatic Session Closure: By default, an agent session will automatically close when the linked participant disconnects for specific reasons, such as
CLIENT_INITIATED
,
ROOM_DELETED
, or
USER_REJECTED
. This is controlled by the
close_on_disconnect
option in
RoomInputOptions
, which is set to
True
by default. You can disable this by setting
RoomInputOptions.close_on_disconnect=False
. • Auto-Disconnect Configuration: When accepting job requests, you can use the
auto_disconnect
parameter to configure automatic disconnection behavior. For example:
Copy code
await job_request.accept(
    TextStreamingAgent.create,
    identity="text-streaming-agent",
    name="Text Streaming Agent",
    auto_disconnect=agents.AutoDisconnect.DEFAULT,
)
• Room-Level Automatic Closure: The room will automatically close when the last non-agent participant leaves, and any remaining agents will be disconnected. This ensures agents do not remain in empty rooms. These features help manage agent sessions efficiently and ensure proper cleanup when participants leave. Sources: livekit-agents/livekit/agents/voice/room_io/room_io.py | server/agent.py | Anatomy of an Agent | LiveKit Docs | Anatomy of an Agent | LiveKit Docs | Anatomy of an Agent | LiveKit Docs
b
I am seeing that the room or agent keeps open for 20 seconds before closing it (which add connection minutes):
Copy code
2025-07-31 10:52:34,380 - DEBUG livekit.agents - stream closed {"participant": "participant_session_1753218125818120_2025-07-31T10:52:18.545638", "source": "SOURCE_MICROPHONE", "pid": 4802, "job_id": "AJ_yZoEeF5myT2x"}
2025-07-31 10:52:58,120 - WARNING livekit - livekit::rtc_engine:453:livekit::rtc_engine - received session close: "signal client closed: \"stream closed\"" UnknownReason Resume {"pid": 4802, "job_id": "AJ_yZoEeF5myT2x"}
2025-07-31 10:52:58,123 - INFO livekit.agents - process exiting {"reason": "", "pid": 4802, "job_id": "AJ_yZoEeF5myT2x"}
t
Hey there! The 20-second delay you’re noticing before the room or agent closes is actually because of the departure timeout setting in LiveKit. Think of it as a little grace period that gives participants a chance to jump back into the room if they had a brief network hiccup or a quick reconnection. Here’s a quick rundown: • Once the last person leaves the room, LiveKit starts a countdown based on the
departure_timeout
setting, which is usually set to 20 seconds by default. • If no one comes back during that time, the room closes and the agents get disconnected. Because of this, you might see some extra connection minutes being counted since the room stays open during that timeout. If you’d rather skip that, you can always delete the room manually when the call ends instead of waiting for the automatic timeout. If you want, I’m here to help you figure out how to set this up or manage the departure timeout settings—just let me know! Sources: pkg/rtc/room.go | packages/livekit-server-sdk/src/RoomServiceClient.ts | livekit-rtc/livekit/rtc/room.py | livekit-protocol/src/livekit.rs | protobufs/livekit_room.proto