the session closes automatically when the the part...
# ask-ai
b
the session closes automatically when the the participant leaves the room, i want to not let that happen, what code to use here
t
Thanks for reaching out, Mohammed. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
To prevent a session from closing automatically when a participant leaves the room in LiveKit, you can use the
close_on_disconnect
parameter in
RoomInputOptions
when starting an agent session. Here is an example implementation:
Copy code
session = AgentSession(
    llm=your_llm_model,
    # other session configuration
)

await session.start(
    room=ctx.room,
    agent=your_agent,
    room_input_options=RoomInputOptions(close_on_disconnect=False),
    # other options
)
By setting
close_on_disconnect=False
, the session will not automatically close when a participant disconnects. However, note that the room itself will still close after a short delay if the last non-agent participant leaves. To handle this, you can implement custom logic to manage participant disconnections:
Copy code
async def entrypoint(ctx: JobContext):
    session = AgentSession(
        llm=your_llm_model,
        # other configuration
    )
    
    await session.start(
        room=ctx.room,
        agent=your_agent,
        room_input_options=RoomInputOptions(close_on_disconnect=False),
    )
    
    # Handle participant disconnections manually
    def on_participant_disconnected(participant):
        if len(ctx.room.remote_participants) == 0:
            # Decide whether to close session or wait for new participants
            pass
    
    ctx.room.on("participant_disconnected", on_participant_disconnected)
This approach gives you control over when the session closes, allowing you to implement custom logic for handling participant disconnections. Sources: examples/voice_agents/realtime_joke_teller.py | Worker lifecycle | LiveKit Docs | Core API primitives | LiveKit Docs