icy-salesclerk-19784
11/14/2025, 2:28 AMtall-airline-16756
11/14/2025, 2:28 AMtall-airline-16756
11/14/2025, 2:29 AMparticipant_disconnected event on the rtc.Room object to catch when participants leave the room.
2. Filter Non-Agent Participants: Check the kind property of each participant to identify non-agent participants. These typically include PARTICIPANT_KIND_STANDARD and PARTICIPANT_KIND_SIP.
3. Count Remaining Non-Agent Participants: Every time someone disconnects, count how many non-agent participants are still in the room.
4. Delete the Room: When there are no non-agent participants left, you can safely call delete_room using the JobContext API.
Here's a complete example to make it clearer:
from livekit import api, rtc
from livekit.agents import JobContext, AgentSession
import asyncio
NON_AGENT_PARTICIPANT_KINDS = [
rtc.ParticipantKind.PARTICIPANT_KIND_STANDARD,
rtc.ParticipantKind.PARTICIPANT_KIND_SIP,
]
def count_non_agent_participants(room: rtc.Room) -> int:
count = 0
for participant in room.remote_participants.values():
if participant.kind in NON_AGENT_PARTICIPANT_KINDS:
count += 1
return count
async def entrypoint(ctx: JobContext):
session = AgentSession()
room_empty_future = asyncio.get_running_loop().create_future()
def _on_participant_disconnected(participant: rtc.RemoteParticipant):
if participant.kind in NON_AGENT_PARTICIPANT_KINDS:
remaining_non_agents = count_non_agent_participants(ctx.room)
if remaining_non_agents == 0 and not room_empty_future.done():
room_empty_future.set_result(None)
ctx.room.on("participant_disconnected", _on_participant_disconnected)
await session.start(agent=your_agent, room=ctx.room)
try:
await room_empty_future
finally:
await ctx.api.room.delete_room(api.DeleteRoomRequest(room=ctx.room.name))
await session.aclose()
This method helps ensure the room is only deleted once all non-agent participants have left, keeping things tidy and efficient. If you have any questions or need more help, please feel free to ask—I'm here for you!
Sources: Agents telephony integration | LiveKit docs | livekit-rtc/livekit/rtc/_proto/participant_pb2.pyi | livekit-agents/livekit/agents/voice/room_io/room_io.py | docs/index.yamlicy-salesclerk-19784
11/14/2025, 9:38 AMtall-airline-16756
11/14/2025, 9:39 AMAgentSession in the Python LiveKit SDK plays a key role in managing real-time voice AI applications. It takes care of media streams, integrates AI models, and coordinates interactions between users and agents. With AgentSession, you can really focus on building your AI logic and agent behavior, without having to worry about the complex infrastructure behind the scenes.
In the example provided, AgentSession helps you to:
• Start and manage the agent's connection to the LiveKit room.
• Handle important events like when participants join or leave.
• Make sure everything is cleaned up properly and that the room and agent session are managed smoothly.
If you have any questions or would like me to walk you through its configuration in more detail, just let me know—I’m here to help!
Sources: livekit-plugins/livekit-plugins-turn-detector/README.md | Speech-to-text (STT) models | LiveKit Docs | agent.py | Speechify TTS plugin guide | LiveKit Docs | Building voice agents | LiveKit Docs