nice-room-70150
09/29/2025, 10:02 PMtall-airline-16756
09/29/2025, 10:02 PMtall-airline-16756
09/29/2025, 10:03 PMctx.wait_for_playout() or checking ctx.session.current_speech.wait_for_playout(). This way, we avoid any abrupt cutoffs and keep the experience smooth for everyone.
2. Next, use the delete_room API to close the entire room. This ensures all participants are disconnected and prevents them from hearing silence if only the agent leaves.
3. It’s a good idea to wrap the delete_room call in a try-except block. This helps gracefully handle any possible issues like network errors or invalid room names.
4. For an even better experience, consider sending a closing message to participants before ending the call. It’s a nice touch that lets everyone know what’s happening.
Here’s an example implementation to guide you:
from livekit import api
from livekit.agents import get_job_context
import logging
logger = logging.getLogger("call_termination")
async def hangup_call():
ctx = get_job_context()
if ctx is None:
logger.warning("No job context available; cannot hang up call")
return
try:
await ctx.api.room.delete_room(
api.DeleteRoomRequest(room=ctx.room.name)
)
logger.info(f"Room {ctx.room.name} deleted successfully")
except Exception as e:
logger.error(f"Error deleting room {ctx.room.name}: {e}")
async def end_call(ctx):
logger.info("Initiating call end procedure")
await ctx.session.generate_reply(
instructions="Thank you. Ending the call now. Goodbye!"
)
await ctx.wait_for_playout()
await hangup_call()
And just a quick note: if you want to disconnect only the agent but keep the participants in the room, you can use ctx.shutdown() instead of delete_room. This way, everyone stays connected while the agent gracefully leaves.
Sources: Agents telephony integration | LiveKit Docs | Agents telephony integration | LiveKit Docs | agent.py | complex-agents/role-playing/role_playing_frontend/components/livekit/agent-... | Docs::Agentsnice-room-70150
09/29/2025, 10:35 PMtall-airline-16756
09/29/2025, 10:35 PM