how to property end call with websdk python agent
# ask-ai
n
how to property end call with websdk python agent
t
Thanks for reaching out, rohit. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
To properly end a call using the LiveKit Web SDK with a Python agent, here’s a friendly step-by-step guide to help you through the process: 1. First, make sure the agent has finished speaking by calling
ctx.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:
Copy code
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::Agents
n
can livekit calls support text and voice? I want to allow the websdk user to type or use voice. I'm currently using the websdk
t
Absolutely! LiveKit calls are designed to support both text and voice communication through the Web SDK. This means users can enjoy exchanging audio and text streams at the same time, making conversations more dynamic and flexible. For instance, you can easily set up the SDK to allow both text and audio within a session. This way, users have the option to chat by speaking or typing. Plus, the Next.js Voice Agent starter app is a great example that shows how to bring both voice and text features into a web-based application. If you'd like, I’m here to help you with the setup or discuss any specific needs you have. Just let me know, and I’ll gladly provide more details! Sources: Docs::Agents | Web Embed Example | livekit/livekit_composite | DeepWiki | Text and transcriptions | LiveKit Docs | agents/src/voice/room_io/room_io.ts | Agents Overview | LiveKit Docs