how to disconnect agent programmatically?
# ask-ai
c
how to disconnect agent programmatically?
t
Thanks for reaching out, ZaidKaraymeh. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
Hey there! If you want to disconnect an agent programmatically in LiveKit, here are a couple of easy ways to do it: 1. Disconnecting Just the Agent: You can use the
ctx.shutdown()
method right inside the agent's entrypoint function like this:
Copy code
async def entrypoint(ctx: JobContext):
    # do some work
    ...
    
    # disconnect from the room
    ctx.shutdown(reason="Session ended")
This will disconnect only that agent, letting everyone else keep going in the room without interruption. 2. Disconnecting Everyone (End Session): If you want to end the session for everyone, you can use the server API
deleteRoom
like this:
Copy code
from livekit import api

async def entrypoint(ctx: JobContext):
    # do some work
    ...
    
    api_client = api.LiveKitAPI(
        os.getenv("LIVEKIT_URL"),
        os.getenv("LIVEKIT_API_KEY"),
        os.getenv("LIVEKIT_API_SECRET"),
    )
    await api_client.room.delete_room(api.DeleteRoomRequest(
        room=ctx.job.room.name,
    ))
This will send a
Disconnected
event to the room and remove it from the server. If you have any questions or want to chat about a specific use case, just let me know—I'm here to help! Sources: Job lifecycle | LiveKit Docs | Inside a session | LiveKit Docs | Agents telephony integration | LiveKit Docs