How to finish the call via livekit with 2 SIP part...
# ask-ai
f
How to finish the call via livekit with 2 SIP participants, when only 1 participant is left (agent already left)
t
Thanks for reaching out, Dmitry. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
Hey there! When you want to finish a call in LiveKit and there’s only one SIP participant left, a handy way is to use the
delete_room
API. This will end the call for everyone and tidy up the room, making sure the last participant disconnects smoothly. Here’s a little example to show you how it works:
Copy code
from livekit import api

async def hangup_call(ctx):
    await ctx.api.room.delete_room(
        api.DeleteRoomRequest(
            room=ctx.room.name,
        )
    )
Another option is to keep an eye on when participants leave and automatically wrap up the call once only one is left. If you want, I’m happy to walk you through that too or help with any part of the setup! Sources: Job lifecycle | LiveKit Docs | examples/voice_agents/error_callback.py | Agents telephony integration | LiveKit Docs | telephony/sip_lifecycle.py
f
how do I know when 1 participant is left ?
t
Hey there! You can easily tell when only one participant is left in a LiveKit room by keeping an eye on participant disconnections. Here’s a simple way to do it using event handlers: Python SDK:
Copy code
room_empty_future: asyncio.Future[None] = asyncio.get_running_loop().create_future()

def _on_participant_disconnected(_: rtc.Participant, fut=room_empty_future) -> None:
    if len(ctx.room.remote_participants) == 0 and not fut.done():
        fut.set_result(None)

ctx.room.on("participant_disconnected", _on_participant_disconnected)
Other SDKs: You’ll find similar event handlers in the Swift and Flutter SDKs that help you track when participants leave. Another handy option is using webhooks to get "Participant Left" events on your server in real-time. If you want, I’d be happy to walk you through any specific setup or answer any questions you have! Sources: livekit-agents/livekit/agents/voice/room_io/room_io.py | Sources/LiveKit/Protocols/RoomDelegate.swift | lib/src/events.dart | Webhooks | LiveKit Docs | Anatomy of an Agent | LiveKit Docs
f
how to finish the call on participant_disconnected, when <=1 SIP participant is left
Copy code
room_empty_future: asyncio.Future[None] = asyncio.get_running_loop().create_future()

def _on_participant_disconnected(_: rtc.Participant, fut=room_empty_future) -> None:

ctx.room.on("participant_disconnected", _on_participant_disconnected)
t
I don't have the answer you're looking for. You could also try asking your question: • in one of the other Slack channels or • to https://deepwiki.com/livekit/livekit_composite which is trained on all LiveKit source code If you find the answer, please post it here to help others!
f
it should be room delete