jolly-eve-3916
07/29/2025, 9:18 PMctx.shutdown(), session.shutdown(), or something
else?tall-airline-16756
07/29/2025, 9:18 PMtall-airline-16756
07/29/2025, 9:19 PMAgentSession automatically handles participant disconnection by default. The session will close automatically when the linked participant disconnects with reasons like CLIENT_INITIATED, ROOM_DELETED, or USER_REJECTED. This behavior is controlled by the close_on_disconnect option:
session = AgentSession(
stt=deepgram.STT(),
llm=openai.LLM(),
tts=cartesia.TTS(),
vad=silero.VAD.load(),
)
# Default behavior - session closes automatically on participant disconnect
await session.start(agent=MyAgent(), room=ctx.room)
You can disable this automatic behavior by setting RoomInputOptions.close_on_disconnect to False.
2. Manual Session Closure:
If you need manual control over session closure, use `session.aclose()`:
# Close the agent session gracefully
await session.aclose()
3. Job Context Shutdown:
Use ctx.shutdown() when you want to disconnect the agent from the room entirely:
# Disconnect from the room
ctx.shutdown(reason="Session ended")
4. Complete Room Termination:
If you want to end the session for everyone, you can delete the room when the session is closed:
ctx.delete_room()
Recommended Approach:
For most use cases, rely on the automatic session closure behavior, which closes the agent session gracefully when the linked participant disconnects. If you need custom cleanup logic, you can add shutdown callbacks using ctx.add_shutdown_callback() to handle any necessary cleanup during shutdown.
Sources: livekit-agents/livekit/agents/ipc/job_proc_lazy_main.py | Job lifecycle | LiveKit Docs