i want to end the session through a tool call, how...
# ask-ai
r
i want to end the session through a tool call, how to do this?
t
Thanks for reaching out, Taresh. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
I'm happy to help you with ending a session through a tool call in LiveKit! You have two friendly options to choose from: • Disconnect the agent only: You can use the
ctx.shutdown()
method to gently close the agent's connection while keeping other participants happily connected in the room. This method also takes care of triggering any shutdown hooks you've set up. • End the session for all participants: If you want to wrap things up for everyone, you can use the
delete_room
API via the LiveKit server SDK. This deletes the entire room and disconnects all participants. You can do this using the
LiveKitAPI
client in Python or the
RoomServiceClient
in Node.js. If you'd like, I’m here to guide you through detailed implementation steps or help with any specific programming language you’re working with. Just let me know! Sources: Docs::Agents | telephony/sip_lifecycle.py | Inside a session | LiveKit Docs | Docs::Agents | agent.py
r
give an example
t
I'd love to help you with ending a session for all participants using the
delete_room
API in Python. Here's a simple example you can follow:
Copy code
from livekit import api
from livekit.agents import get_job_context

async def hangup_call():
    ctx = get_job_context()
    if ctx is None:
        return

    await ctx.api.room.delete_room(
        api.DeleteRoomRequest(
            room=ctx.room.name,
        )
    )
In this example, the
delete_room
API is called to delete the room and gracefully disconnect everyone. Just make sure your environment variables
LIVEKIT_URL
,
LIVEKIT_API_KEY
, and
LIVEKIT_API_SECRET
are set up correctly. If you'd like, I’m here to provide more details or share examples in another programming language—just let me know!
r
and how do i define this function as a tool for the llm?
t
To help you define the function as a tool for the LLM, you can use a decorator or a specific tool registration method depending on your framework. Here’s a friendly example in Python that might make things clearer:
Copy code
from livekit import api
from livekit.agents import get_job_context
from livekit.llm import function_tool

@function_tool
async def end_call(ctx):
    """Ends the session for all participants."""
    ctx = get_job_context()
    if ctx is None:
        return "No active job context found."

    try:
        await ctx.api.room.delete_room(
            api.DeleteRoomRequest(
                room=ctx.room.name,
            )
        )
        return "Session ended successfully."
    except Exception as e:
        return f"Error ending session: {e}"
Here’s a quick rundown: • The
@function_tool
decorator helps register your function as a callable tool for the LLM. • The function uses the
delete_room
API to end the session and thoughtfully returns a success or error message. If you’d like, I’m here to help you with this setup or provide more examples—just let me know!