Using SIP, a Caller connect to the agent in a room...
# ask-ai
p
Using SIP, a Caller connect to the agent in a room. If the caller hangs up, the session is not immediately cleaned up, but persists, therefore if the same caller comes back in, it picks up where it left off. This is NOT what we want, any hang up should always kill all sessions.
t
Thanks for reaching out, Scott. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
Hey there! To make sure SIP sessions get cleaned up right after a caller hangs up, you’ll want to explicitly delete the room using the
delete_room
API. This will remove all participants, including the SIP participant, and completely end the session. Here’s a simple example to help you get started:
Copy code
from livekit import api

async def hangup_call():
    ctx = get_job_context()  # or use JobContext directly
    if ctx is None:
        return

    await ctx.api.room.delete_room(
        api.DeleteRoomRequest(room=ctx.room.name)
    )
Another way is to handle session closure right in your agent logic by listening for events like
participant_disconnected
and calling
delete_room
when the room is empty:
Copy code
async def entrypoint(ctx: JobContext):
    @ctx.room.on("participant_disconnected")
    def on_participant_left(participant):
        if len(ctx.room.participants) == 0:  # last participant left
            ctx.delete_room()  # immediately terminate room and SIP session

    # wait for room events or shutdown
    ...
Deleting the room explicitly like this helps make sure everything cleans up right away and avoids any surprise lingering sessions. If you want, I’m here to help you get this set up—just let me know! Sources: Agents telephony integration | LiveKit Docs | Inside a session | LiveKit Docs | livekit-agents/livekit/agents/voice/agent_session.py | pkg/sip/inbound.go | Job lifecycle | LiveKit Docs
p
can you provide this for the nodejs sdk
t
Hey there! If you want to delete a room using the Node.js SDK in LiveKit, here’s a simple way to do it: 1. First, set up the
RoomServiceClient
with your LiveKit server URL, API key, and secret key like this:
Copy code
import { RoomServiceClient } from 'livekit-server-sdk';

const livekitHost = <LIVEKIT_SERVER_URL>;
const apiKey = <API_KEY>;
const secretKey = <SECRET_KEY>;
const roomService = new RoomServiceClient(livekitHost, apiKey, secretKey);
1. Next, just call the
deleteRoom
method and pass in the room name you want to delete:
Copy code
await roomService.deleteRoom('myroom');
Keep in mind, deleting a room will disconnect everyone currently in it. If you have any questions or need a hand with this, feel free to ask—I'm here to help! Sources: Managing rooms | LiveKit Docs | Managing rooms | LiveKit Docs | livekit/livekit_room.twirp.go | packages/livekit-server-sdk/README.md
p
im thinking more inside the agent
t
Hey there! If you want to delete a room inside an agent using the Node.js SDK, you can easily do this with the
RoomServiceClient
by calling the
deleteRoom
method and passing in the room's name. Here’s a simple way to get it done: 1. First, set up the
RoomServiceClient
with your LiveKit API credentials:
Copy code
import { RoomServiceClient } from 'livekit-server-sdk';

const roomServiceClient = new RoomServiceClient(
  <LIVEKIT_SERVER_URL>,
  <API_KEY>,
  <SECRET_KEY>
);
1. Then, inside your agent’s logic, just call
deleteRoom
and pass the room name from the agent’s context like this:
Copy code
await roomServiceClient.deleteRoom(ctx.room.name!);
Here’s an example you can add to your agent’s execution code:
Copy code
execute: async () => {
  console.log('Waiting for 20 seconds before deleting room...');
  setTimeout(async () => {
    console.log('Deleting room...');
    await roomServiceClient.deleteRoom(ctx.room.name!);
  }, 20000);
  return 'Game over, thank you for playing. Goodbye!';
}
This way, the room gets properly deleted, all participants are disconnected, and the
Disconnected
event fires with the reason
ROOM_DELETED
. If anything’s unclear or you want a hand with this, just let me know—I’m here to help! Sources: Connecting to LiveKit | LiveKit Docs | packages/sip--um-actually/src/agent.ts
p
are you suggesting that when a caller hangs up, that livekit does not immediately close the session?
t
Yes, LiveKit does immediately close a session when a SIP caller hangs up. It works by canceling the associated context, which then cleans up the media and session resources. This way, the session is properly closed and the SIP participant is disconnected. If you’re seeing something different, it could be due to some specific setup or configuration. Just let me know if you want a hand digging into it—I’m here to help! Sources: pkg/sip/inbound.go
p
when i hang up, and then call back in 10 secs later, the agent picks up where it left off
t
Hey! What you’re noticing—where the session sticks around and picks up again if the caller reconnects quickly after hanging up—is actually thanks to LiveKit’s session resilience features. They’re there to help keep things smooth during brief interruptions, so quick reconnects are seen as just continuing the same session. If you want to stop this and make sure that hanging up truly ends the session, you can delete the room explicitly when the caller disconnects. This clears out all participants and makes sure any new call starts fresh. Just let me know if you want a hand setting this up—I’m happy to help! Sources: SIP participant | LiveKit Docs | SIP participant | LiveKit Docs | Connecting to LiveKit | LiveKit Docs | Connecting to LiveKit | LiveKit Docs | How We Built a Globally Distributed WebRTC Mesh Network
p
i see, this is truly what i want, I want the code to handle this deleting of a room if a user hangs up
t
Hey there! If you want to automatically delete a room when a user hangs up, you can set up some logic to listen for when someone disconnects and then delete the room if it's empty. Here’s a friendly example using the Node.js SDK:
Copy code
import { RoomServiceClient } from 'livekit-server-sdk';

const roomServiceClient = new RoomServiceClient(
  <LIVEKIT_SERVER_URL>,
  <API_KEY>,
  <SECRET_KEY>
);

async function handleParticipantDisconnected(ctx) {
  ctx.room.on('participant_disconnected', async (participant) => {
    if (ctx.room.participants.size === 0) { // Check if all participants have left
      console.log('All participants have left. Deleting room...');
      await roomServiceClient.deleteRoom(ctx.room.name!);
    }
  });
}
This snippet listens for the
participant_disconnected
event and deletes the room once everyone’s gone. If you want a hand setting this up or have any questions, just let me know—I’m here to help!