I want to remove the agent from the room without d...
# ask-ai
c
I want to remove the agent from the room without deleting it. I want to do it from a function call. Write the code.
t
Thanks for reaching out, Yousri. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
I'm happy to help you with removing an agent from a room without deleting it! You can use the
removeParticipant
method from the
RoomServiceClient
. This method will gently disconnect the specified participant (including agents) from the room, while keeping the room itself safe and sound. Here’s a friendly example in Node.js/TypeScript to guide you:
Copy code
import { RoomServiceClient } from 'livekit-server-sdk';

const roomService = new RoomServiceClient(
  process.env.LIVEKIT_URL,
  process.env.LIVEKIT_API_KEY,
  process.env.LIVEKIT_API_SECRET
);

// Remove the agent by specifying the room name and the agent's identity
await roomService.removeParticipant(roomName, agentIdentity);
Just swap out
roomName
with your room's name and
agentIdentity
with the agent's identity you want to remove. This will disconnect the agent right away, without disturbing the room itself. If you have any questions or need more help, feel free to ask—I'm here for you! Sources: Managing participants | LiveKit Docs | agent-console/app/api/room/remove-participant/route.ts | pkg/rtc/room.go | Job lifecycle | LiveKit Docs
c
Write it using python SDK
t
I'd be happy to help you remove a participant (agent) from a room using the Python SDK! You can do this easily with the
remove_participant
method from the
RoomService
client. Here's a simple example to guide you through it:
Copy code
from livekit import api
import asyncio

async def remove_participant_from_room():
    lkapi = api.LiveKitAPI("https://your-livekit-server.com")
    try:
        await lkapi.room.remove_participant(
            api.RoomParticipantIdentity(room="my-room", identity="participant-identity")
        )
        print("Participant removed successfully")
    except Exception as e:
        print(f"Failed to remove participant: {e}")
    finally:
        await lkapi.aclose()

asyncio.run(remove_participant_from_room())
Just swap out
"my-room"
with your actual room name, and
"participant-identity"
with the participant's identity you want to remove. Also, handling exceptions like in the example will help you catch any issues smoothly. If you have any questions or run into troubles, feel free to ask—I'm here to help! Sources: Managing participants | LiveKit Docs | Connecting to LiveKit | LiveKit Docs | README.md | pkg/service/roommanager.go
c
I want to remove the agent. Write the code specific to that.
t
I'm happy to share with you the Python code designed to help you remove an agent from a room:
Copy code
from livekit import api
import asyncio

async def remove_agent_from_room():
    lkapi = api.LiveKitAPI("https://your-livekit-server.com")
    try:
        # Just replace 'room-name' with your room's name and 'agent-identity' with the agent's identity
        await lkapi.room.remove_participant(
            api.RoomParticipantIdentity(room="room-name", identity="agent-identity")
        )
        print("Agent removed successfully")
    except Exception as e:
        print(f"Failed to remove agent: {e}")
    finally:
        await lkapi.aclose()

asyncio.run(remove_agent_from_room())
Simply swap out
"room-name"
for your actual room name and
"agent-identity"
for the agent's identity you'd like to remove. This will disconnect the agent right away without impacting the rest of the room. If you have any questions or need further help, feel free to ask!
c
How do you ge tthe agent identity programatically? Do I need to use local participant?
t
I'm happy to help you retrieve the agent's identity! You can easily access it programmatically by using the
identity
property on the participant object. This works for both local and remote participants, so you don’t always have to rely on the local participant to get this info. Here’s a simple way to do it: • For the local participant: Just use
room.localParticipant.identity
. • For a remote participant: You can use
remoteParticipant.identity
, or find them by identity with methods like
getParticipantByIdentity()
or by checking the room's participant map. The
identity
is a unique string assigned to each participant as soon as they join the room, so you’ll have this information right away. Sources: Tests/LiveKitCoreTests/ParticipantTests.swift | livekit/src/room/mod.rs | Rooms, participants, and tracks | LiveKit Docs | livekit-android-sdk/src/main/java/io/livekit/android/room/Room.kt | README.md