future-air-59443
05/09/2025, 3:02 AMtall-airline-16756
05/09/2025, 3:02 AMtall-airline-16756
05/09/2025, 3:02 AM@function_tool
async def end_call(self, context: RunContext):
"""When the user says goodbye, call this function to end the conversation."""
# interrupt any existing generation
self.session.interrupt()
# generate a goodbye message and hang up
# awaiting it will ensure the message is played out before returning
await self.session.generate_reply(
instructions="say a brief goodbye to the user",
allow_interruptions=False
)
# Delete the room to end the call
job_ctx = get_job_context()
await job_ctx.api.room.delete_room(api.DeleteRoomRequest(room=job_ctx.room.name))
This function interrupts any ongoing speech, generates a final goodbye message, and deletes the room to end the call. You can configure the agent to call this function when it detects the user saying goodbye.
Sources: Tree View: | dse-workbenchfuture-air-59443
05/09/2025, 3:03 AMtall-airline-16756
05/09/2025, 3:03 AMend_call
function in LiveKit, you will need the following imports:
from livekit import api, rtc
from livekit.agents import get_job_context
These imports allow you to access the LiveKit API for ending a call by deleting the room and obtaining the current job context.
Sources: Agents telephony integration | LiveKit Docsfuture-air-59443
05/09/2025, 3:05 AMtall-airline-16756
05/09/2025, 3:05 AM@function_tool
and RunContext
in LiveKit, you need to import them as follows:
from livekit.agents import function_tool, RunContext
The @function_tool
decorator is used to define tools callable by the LLM, while RunContext
provides access to the current session, function call, speech handle, and user data within your tool implementation.
Sources: Tool definition and use | LiveKit Docs