little-king-41579
02/16/2026, 12:27 PMtall-airline-16756
02/16/2026, 12:27 PMtall-airline-16756
02/16/2026, 12:27 PMagent_identity = ctx.room.local_participant.identity
print(agent_identity)
This returns the agent’s participant identity string.
Removing (disconnecting) the agent from the room
• Gracefully shut down the agent session (drains pending work and removes media/tracks):
ctx.shutdown(drain=True)
• Or close immediately:
await ctx.aclose()
Either call disconnects the agent, removes its media tracks, and triggers the usual participant-left events.
If you want to remove all participants / delete the entire room
• Use the Server API to delete the room:
from livekit.api import LiveKitAPI, DeleteRoomRequest
api_client = LiveKitAPI(url=..., api_key=..., api_secret=...)
await api_client.room.delete_room(DeleteRoomRequest(room=ctx.room.name))
This removes the room and all participants.
Quick notes to avoid confusion
• There isn’t a separate operation to “remove an identity” without disconnecting the participant — identity is tied to the participant session.
• If you meant removing a different (non-agent) participant, or you're using SIP participants, or you need token/session details to force-disconnect a remote participant, tell me which case you mean and I’ll provide the exact steps or API calls.
Do you want the exact code sample integrated into your agent entrypoint, or are you removing a remote participant (not the agent itself)?
Sources: SIP Lifecycle Management Agent | LiveKit Documentation | Job lifecycle | LiveKit Documentationlittle-king-41579
02/16/2026, 12:30 PMtall-airline-16756
02/16/2026, 12:30 PMparticipant_joined webhooks to your server.
2. When you receive a participant_joined event, call the Server SDK method to remove only the agent participant from that room.
Example flow (webhook + Python)
1. Webhook receives event -> extract room and new participant identity.
2. Identify the agent participant (by fixed identity, prefix, or metadata).
3. Call remove_participant for that agent identity.
Pseudo-Python outline
# 1. Webhook handler receives participant_joined event
def webhook_handler(event):
room_name = event["room"]["name"]
new_identity = event["participant"]["identity"]
# 2. Identify agent - adjust logic to match how you name/mark agents
agent_identity = find_agent_identity(room_name)
if agent_identity:
# 3. Remove only the agent
remove_agent_from_room(room_name, agent_identity)
# Helper: find agent identity (example: look for identity prefix "agent_")
def find_agent_identity(room_name):
participants = list_participants(room_name)
for p in participants:
if p["identity"].startswith("agent_"):
return p["identity"]
return None
Server SDK call to remove a participant (Python)
• Use the LiveKit server API client to remove a single participant by identity.
• Call the remove participant endpoint for the target room and identity (the exact client method name depends on the SDK bindings you use; conceptually it is remove_participant(room_name, identity)).
If you cannot use webhooks and must poll
• Periodically list participants for the room.
• When you detect a new non-agent participant (compare to previous snapshot), run the same remove flow to evict the agent only.
Notes & best practices
• Identify the agent reliably: use a fixed identity (e.g., agent_bot) or set participant metadata (e.g., role:agent) so your server can find the agent quickly.
• Removing a participant does not delete the room or affect other participants.
• Removing is a server-side operation—don’t disconnect the entire room.
If you want, I can:
• Provide a concrete, runnable Python snippet using the exact LiveKit Python Server SDK methods (I’ll need to know whether you use the official Python SDK or a REST call).
• Show webhook verification and example payload handling.
• Show the polling implementation if webhooks aren’t possible.
Which option do you prefer: webhook or polling? And do you want a full Python code sample using the LiveKit Python Server SDK or a direct REST call example?
Sources: server-sdk-js | livekit/livekit_composite | DeepWiki | CreateRoom() is Taking Too Longlittle-king-41579
02/16/2026, 12:51 PMtall-airline-16756
02/16/2026, 12:52 PMsession.update_agent(new_agent) to swap agent logic without disconnecting the participant or closing the room.
• This preserves connection state and is fast.
Example outline (Agent framework)
# assume session is an active AgentSession already started in the room
# when you detect a new participant, create the new agent logic and update:
new_agent = Agent(instructions="You are Agent 2...")
await session.update_agent(new_agent)
When to use:
• You only need to change the agent’s behavior/instructions or LLM prompt.
• STT/TTS and other session resources remain the same.
Option B — Full session restart (use when you need different models/config)
• Close the current session, then create and start a new AgentSession that connects to the same room.
Example outline (Agent framework)
# graceful shutdown of current agent session
await session.aclose() # or ctx.shutdown(drain=True)
# create new session (can use different STT/LLM/TTS)
new_session = AgentSession(stt=..., llm=..., tts=..., vad=...)
await new_session.start(agent=new_agent, room=ctx.room)
# store new_session for later swaps
session = new_session
When to use:
• You need a different audio pipeline, different models, or fully isolated agent state.
Triggering replacement when a new participant joins
• Detect new participants via webhooks (recommended) or via room event handlers.
• Webhook approach: listen for participant_joined and then call remove-agent + start-new-agent logic.
• In-process approach (inside your Agent job): register a participant event handler and perform the swap there.
Example event handler pattern (inside your entrypoint)
@ctx.room.on("participant_connected")
async def on_new_participant(participant):
# Optionally remove existing agent participant if you previously used remove API
# Then choose swap method:
await session.aclose() # or session.update_agent(new_agent)
new_session = AgentSession(...)
await new_session.start(agent=new_agent, room=ctx.room)
Notes & best practices
• You do not need to delete the room; replacing the agent keeps the room and other participants intact.
• Identify agents reliably (fixed identity like agent_bot or metadata such as role=agent) so your server can find and remove/swap the correct participant.
• Prefer update_agent() for fast, in-place swaps when only agent logic changes.
• Use await session.aclose() or ctx.shutdown(drain=True) for clean shutdowns when restarting the session.
• If you need exact code using the LiveKit Python Server SDK methods (or a direct REST call), tell me: do you want a full runnable snippet, webhook verification code, or polling-based detection? Sources: Job lifecycle | LiveKit Documentation | Agents and handoffs | LiveKit Documentation | How to do call transfers with Five9 | LiveKit