how to write tool function in python that hangs up...
# ask-ai
n
how to write tool function in python that hangs up the call ?
t
Thanks for reaching out, Ahmad. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
To write a Python function that hangs up a call in LiveKit, you can use the
delete_room
API. Here's an example implementation:
Copy code
from livekit import api
from livekit.agents import get_job_context
from livekit.agents.llm import function_tool
import asyncio

async def hangup_call():
    ctx = get_job_context()
    if ctx is None:
        # Not running in a job context
        return

    # Let the agent finish speaking if there's ongoing speech
    current_speech = ctx.session.current_speech
    if current_speech:
        await current_speech.wait_for_playout()

    # Delete the room to end the call
    await ctx.api.room.delete_room(
        api.DeleteRoomRequest(
            room=ctx.room.name,
        )
    )

class MyAgent(Agent):
    @function_tool
    async def end_call(self, ctx: RunContext):
        """Called when the user wants to end the call"""
        # Let the agent finish speaking
        current_speech = ctx.session.current_speech
        if current_speech:
            await current_speech.wait_for_playout()

        await hangup_call()
Key Points: • The
hangup_call
function handles the deletion of the room to terminate the call. • The
end_call
method is decorated with
@function_tool
, making it accessible as a tool for the LLM. • It ensures any ongoing speech finishes before hanging up the call. This approach ensures a clean termination of the call for all participants.
n
but shouldn't the job context be passed as an argument to the tool?
t
You're absolutely right—passing the job context as an argument to the tool is a clearer and more flexible way to handle things. This way, the function knows exactly what context it’s working in and doesn’t have to depend on any global state. Here’s an updated version for you:
Copy code
from livekit import api
from livekit.agents.llm import function_tool

async def hangup_call(ctx):
    if ctx is None:
        # Not running in a job context
        return

    # Let the agent finish speaking if there's ongoing speech
    current_speech = ctx.session.current_speech
    if current_speech:
        await current_speech.wait_for_playout()

    # Delete the room to end the call
    await ctx.api.room.delete_room(
        api.DeleteRoomRequest(
            room=ctx.room.name,
        )
    )

class MyAgent(Agent):
    @function_tool
    async def end_call(self, ctx):
        """Called when the user wants to end the call"""
        # Let the agent finish speaking
        current_speech = ctx.session.current_speech
        if current_speech:
            await current_speech.wait_for_playout()

        await hangup_call(ctx)
By passing
ctx
explicitly like this, your function becomes more modular and way easier to test or reuse in different situations. Hope this helps!
n
what if we pass the function tool as an argument to the class, then how we add that as a tool ?
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!