i want to remove all the participants in the room ...
# ask-ai
b
i want to remove all the participants in the room and clean up the resources
t
Thanks for reaching out, Utkarsh. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
You're asking to remove all participants and clean up the room—do you want to delete the room entirely (recommended) or just disconnect everyone but keep the room? • If you want to delete the room (disconnects all participants and cleans up tracks/egress/ingress): please tell me the room name/ID and I will run the DeleteRoom action. • If you want to kick everyone but keep the room: tell me the room name/ID and I will remove participants individually instead. Which option would you like, and what is the room name/ID? Sources: Managing rooms | LiveKit docs | Job lifecycle | LiveKit docs | Connecting to LiveKit | LiveKit docs | Managing participants | LiveKit docs
b
i have a call in progress but when it reaches certain timeout threshold i want to the call to get disconnected, so how should i proceed with the it? I have the ctx object with me which we give me the room and room name
t
You're asking how to automatically disconnect a call when it reaches a timeout using your
ctx
(which gives you the room and room name). I can help—quick questions first so I give the right code: • Do you want to disconnect only the agent/process using
ctx.room.disconnect()
or end the call for everyone by deleting the room (
DeleteRoom
)? • Which language/SDK are you using (Python, Node.js, Go, or other)? • Do you want a total call duration limit, an inactivity/silence timeout, or both? If you confirm those, I’ll provide a concise code example you can drop into your job/worker. Sources: Job lifecycle | LiveKit docs | Agents telephony integration | LiveKit docs | How to detect and handle silence during calls | Agents Framework | livekit/livekit_composite | DeepWiki | examples/voice_agents | livekit/livekit_composite | DeepWiki
b
i want to end the call for everyone
and using python sdk
t
Good — since you want to end the call for everyone, delete the room when your timeout triggers. Deleting the room disconnects all participants and cleans up tracks/egress/ingress. Below are two concise timeout-monitor patterns you can adapt. Replace
ROOM_NAME
with
ctx.room.name
and set your timeout value. Python (async):
Copy code
import asyncio
import time
from livekit.api import DeleteRoomRequest

CALL_TIMEOUT = 300  # seconds

async def start_timeout_monitor(ctx):
    start = time.time()
    async def monitor():
        while True:
            if time.time() - start > CALL_TIMEOUT:
                await ctx.api.room.delete_room(DeleteRoomRequest(room=ctx.room.name))
                return
            await asyncio.sleep(1)
    asyncio.create_task(monitor())
Node.js (async):
Copy code
const CALL_TIMEOUT = 300 * 1000; // ms

function startTimeoutMonitor(ctx) {
  const timer = setTimeout(async () => {
    await ctx.api.room.deleteRoom({ room: ctx.room.name });
  }, CALL_TIMEOUT);

  ctx.on('shutdown', () => clearTimeout(timer));
}
Notes: • Use an inactivity timer instead of a fixed start-time timer if you want silence-based disconnects; reset the timer when user activity is detected (e.g., on speech events). • Ensure your process has permission to call the Room Delete API. Would you like the inactivity/silence-based variant or a version in another language?
You're ending the call for everyone using the Python SDK — below is a concise pattern you can drop into your job/worker that monitors a timeout (total duration and/or inactivity) and deletes the room with
ctx.api.room.delete_room(...)
when the threshold is reached.
Copy code
import asyncio
import time
from livekit.api import DeleteRoomRequest

# thresholds (seconds)
CALL_TIMEOUT = 300       # total call duration
SILENCE_THRESHOLD = 30   # inactivity threshold (optional)

async def entrypoint(ctx):
    call_start = time.time()
    last_activity = time.time()
    monitor_task = None

    # Example: update last_activity when you detect user activity
    # Replace with your actual event handler that detects speech/activity
    def on_user_activity():
        nonlocal last_activity
        last_activity = time.time()

    async def monitor():
        try:
            while True:
                now = time.time()
                if now - call_start >= CALL_TIMEOUT:
                    # delete the room for everyone and clean up resources
                    await ctx.api.room.delete_room(DeleteRoomRequest(room=ctx.room.name))
                    return
                if SILENCE_THRESHOLD and (now - last_activity) >= SILENCE_THRESHOLD:
                    await ctx.api.room.delete_room(DeleteRoomRequest(room=ctx.room.name))
                    return
                await asyncio.sleep(1)
        except asyncio.CancelledError:
            return

    monitor_task = asyncio.create_task(monitor())

    @ctx.on_shutdown
    async def _shutdown():
        if monitor_task:
            monitor_task.cancel()
Notes: •
ctx.api.room.delete_room(DeleteRoomRequest(room=ctx.room.name))
disconnects all participants and cleans up room resources. • Update
last_activity
from your real activity/speech events (example placeholder
on_user_activity
). • Adjust
CALL_TIMEOUT
and
SILENCE_THRESHOLD
as needed. Would you like this adapted to use a specific voice-activity event or example of hooking into your existing speech-detection handler?
b
i want to the agent to speak some text and after it has finish speaking the clean up should happen, and i want to call some tools as well before deleting the room,
t
You're asking for a Python pattern that: 1) makes the agent speak, 2) waits until speech finishes, 3) runs some tool calls, and 4) deletes the room for everyone. Below is a concise, ready-to-drop example that follows that sequence using the AgentSession speech handle and
ctx.api.room.delete_room(...)
.
Copy code
import asyncio
import os
from livekit.api import DeleteRoomRequest, LiveKitAPI
from livekit.agents import AgentSession, Agent
from livekit.plugins import silero, openai, cartesia, deepgram

# Example tool: run any async cleanup/tool calls here
async def run_pre_delete_tools():
    # replace with your real tool calls
    await asyncio.sleep(1)
    return "tools finished"

async def entrypoint(ctx):
    # initialize models/plugins (adjust as needed)
    vad = silero.VAD.load()
    session = AgentSession(
        ctx=ctx,
        stt=deepgram.STT(),
        llm=openai.LLM(model="gpt-4o-mini"),
        tts=cartesia.TTS(voice="your-voice-id"),
        vad=vad,
    )

    agent = Agent()
    await session.start(agent=agent, room=ctx.room)
    await ctx.connect()

    # 1) Have the agent speak — returns a SpeechHandle you can await
    handle = session.generate_reply(instructions="Thank you for your time. The session is ending now.")
    await handle  # wait until speech finishes

    # 2) Run any tools you need after speech completes
    tool_result = await run_pre_delete_tools()

    # 3) Delete the room for everyone (disconnects participants and cleans up resources)
    lk_api = LiveKitAPI(os.getenv("LIVEKIT_URL"), os.getenv("LIVEKIT_API_KEY"), os.getenv("LIVEKIT_API_SECRET"))
    try:
        await lk_api.room.delete_room(DeleteRoomRequest(room=ctx.room.name))
    finally:
        await lk_api.aclose()

    # optional: log or return tool_result if you need it
    return tool_result
Notes: • Awaiting the
SpeechHandle
(the
handle
) ensures speech completes before proceeding. • Put your actual tool calls inside
run_pre_delete_tools()
or call agent tools directly after
await handle
. •
lk_api.room.delete_room(...)
ends the call for everyone and cleans room resources. • Ensure
LIVEKIT_URL
,
LIVEKIT_API_KEY
, and
LIVEKIT_API_SECRET
are set in your environment. Would you like this adapted to call agent tools via the Agent framework (e.g.,
agent.tool_call(...)
) or to include error handling/retry for the delete step? Sources: Agents telephony integration | LiveKit docs | Job lifecycle | LiveKit docs | Agent speech and audio | LiveKit docs
b
Copy code
import asyncio
import time
from livekit.api import DeleteRoomRequest

# thresholds (seconds)
CALL_TIMEOUT = 300       # total call duration
SILENCE_THRESHOLD = 30   # inactivity threshold (optional)

async def entrypoint(ctx):
    call_start = time.time()
    last_activity = time.time()
    monitor_task = None

    # Example: update last_activity when you detect user activity
    # Replace with your actual event handler that detects speech/activity
    def on_user_activity():
        nonlocal last_activity
        last_activity = time.time()

    async def monitor():
        try:
            while True:
                now = time.time()
                if now - call_start >= CALL_TIMEOUT:
                    # delete the room for everyone and clean up resources
                    await ctx.api.room.delete_room(DeleteRoomRequest(room=ctx.room.name))
                    return
                if SILENCE_THRESHOLD and (now - last_activity) >= SILENCE_THRESHOLD:
                    await ctx.api.room.delete_room(DeleteRoomRequest(room=ctx.room.name))
                    return
                await asyncio.sleep(1)
        except asyncio.CancelledError:
            return

    monitor_task = asyncio.create_task(monitor())

    @ctx.on_shutdown
    async def _shutdown():
        if monitor_task:
            monitor_task.cancel()
in the above when the condition: if now - call_start >= CALL_TIMEOUT is true, can we make the agent say something and call a tool as well before the room gets deleted
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!
b
what can we do with the participant sid field
how can we the remote participant details in a room>