boundless-author-7811
12/18/2025, 5:48 AMtall-airline-16756
12/18/2025, 5:48 AMtall-airline-16756
12/18/2025, 5:48 AMboundless-author-7811
12/18/2025, 5:49 AMtall-airline-16756
12/18/2025, 5:50 AMctx (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 | DeepWikiboundless-author-7811
12/18/2025, 5:50 AMboundless-author-7811
12/18/2025, 5:50 AMtall-airline-16756
12/18/2025, 5:50 AMROOM_NAME with ctx.room.name and set your timeout value.
Python (async):
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):
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?tall-airline-16756
12/18/2025, 5:50 AMctx.api.room.delete_room(...) when the threshold is reached.
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?boundless-author-7811
12/18/2025, 6:10 AMtall-airline-16756
12/18/2025, 6:11 AMctx.api.room.delete_room(...).
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 docsboundless-author-7811
12/18/2025, 9:02 AMimport 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 deletedtall-airline-16756
12/18/2025, 9:03 AMboundless-author-7811
12/18/2025, 9:46 AMboundless-author-7811
12/18/2025, 11:13 AM