Need to termiante the agent AI when it turns to a ...
# ask-ai
d
Need to termiante the agent AI when it turns to a function, for example It is enable to do a transfer but I need to hangup the livekit SIP connection, could you please share the code for that?
t
Thanks for reaching out, ahbe. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
Thanks — I understand you need code to hang up a LiveKit SIP call from an agent when a transfer fails (or when the agent decides to stop). Below I’m sharing a concise, practical example and explanation you can use inside the LiveKit Agents framework to end the SIP connection cleanly. What this does • Plays a short farewell message (so the caller hears the agent). • Waits for the playout to finish. • Deletes the room via the JobContext API, which disconnects all participants including the SIP leg — effectively hanging up the call. Python example (LiveKit Agents)
Copy code
from livekit.agents import get_job_context, function_tool, RunContext

@function_tool
async def end_call(self, context: RunContext):
    """End the current call (hang up SIP) after announcing failure."""
    # Let the caller know the transfer failed and we're ending the call
    await context.session.generate_reply(
        instructions="I'm sorry, I couldn't complete the transfer. Ending the call now."
    )
    # Ensure the spoken audio finishes before disconnecting
    await context.wait_for_playout()

    # Obtain the job context (gives access to the server-side API)
    job_ctx = get_job_context()
    if not job_ctx:
        return "Failed to end call: No job context available"

    # Delete the room to disconnect all participants (hang up SIP)
    try:
        await job_ctx.api.room.delete_room(
            job_ctx.room.name  # pass the room name/ID
        )
        return "Call ended successfully."
    except Exception as e:
        return f"Failed to end call: {e}"
How to call this when a transfer fails 1. In your transfer function, detect the transfer failure (exception, timeout, or other condition). 2. Call
await self.end_call(context)
to announce and hang up. Example sketch:
Copy code
@function_tool
async def transfer_call(self, context: RunContext, phone_number: str):
    try:
        # your transfer logic here
        ...
    except Exception as e:
        # transfer failed — end the call
        return await self.end_call(context)
Notes and tips •
context.session.generate_reply()
sends the agent’s voice reply;
context.wait_for_playout()
ensures that audio finishes before deletion. • Deleting the room disconnects everyone, so if you need to only remove the SIP participant (not others), let me know and I can show an alternate approach if your setup supports removing individual participants instead of deleting the whole room. • If you use different SDKs or need a JavaScript/TypeScript example, tell me which environment and I’ll provide that version. Would you like a JS/TS version or an example that removes only the SIP participant instead of deleting the room? Sources: SIP Lifecycle Management Agent | LiveKit Documentation | Agents telephony integration | LiveKit Documentation | Agent speech and audio | LiveKit Documentation