I want to end the call, how I can do that
# ask-ai
r
I want to end the call, how I can do that
t
Thanks for reaching out, Umair. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
To end a call in LiveKit, you can use one of the following methods: • End the call for all participants: Use the
delete_room
API to terminate the call for everyone in the room. • End the call for a specific participant: Use the
remove_participant
API to remove an individual participant from the call. If you need further assistance with implementing these methods, let me know!
r
I am using langgraph and streaming Qnd if stream have work like 'end' Then i would like to end the call but also when after agent speah completes
t
When you want to end a call in LiveKit after the agent finishes speaking, here’s a simple way to do it:
Copy code
async def end_call(self, ctx: RunContext):
    """Called when the user wants to end the call"""
    # Wait for the agent to finish speaking
    current_speech = ctx.session.current_speech
    if current_speech:
        await current_speech.wait_for_playout()

    await hangup_call()

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,
        )
    )
This way, the agent’s speech wraps up nicely before the call ends for everyone, using the
delete_room
API. If you have any questions or want to chat more about this, just let me know—I’m here to help! Sources: Agents telephony integration | LiveKit Docs
r
From where i call end call?
t
Hey there! If you want to call the
end_call
function in LiveKit using LangGraph with streaming, here’s a simple way to do it: 1. Define the
end_call
Function:
Start by using the
@function_tool()
decorator. This makes the function available as a handy tool for the LLM. Basically, this function will take care of ending the call by deleting the room. 2. Integrate with LangGraph: Next, use the
LLMAdapter
to connect LangGraph as your LLM. This way, the model knows how to spot and run the
end_call
function during your chat. 3. Enable Streaming and Function Tools: Make sure your agent is set up to handle streaming conversations and function tools in the
AgentSession
configuration. This keeps everything running smoothly. 4. Invoke
end_call
During Conversation:
When you’re chatting and want to end the call, the LLM can trigger the
end_call
tool. Just make sure your conversation flow is ready to catch that cue. If you want, I’m here to help you with any part of this — just let me know!
r
Could you provide some example code Also when agent will call this tool how it will be decided?
t
Here’s a friendly example of how to call the
end_call
function in LiveKit using LangGraph, and how the agent decides to use this handy tool:
Copy code
from livekit import api, rtc
from livekit.agents import Agent, function_tool, get_job_context
from livekit.plugins import deepgram, openai, silero

class ExampleAgent(Agent):
    def __init__(self):
        super().__init__(
            instructions="""
            You are a voice assistant that can end calls when requested.
            Use the end_call tool when the user wants to terminate the conversation.
            """,
            stt=deepgram.STT(),
            llm=openai.LLM(model="gpt-4"),
            tts=openai.TTS(),
            vad=silero.VAD.load()
        )

    @function_tool()
    async def end_call(self, context: RunContext):
        """End the current call by deleting the room."""
        # Let's wait for any speech that's still playing to finish nicely
        current_speech = context.session.current_speech
        if current_speech:
            await current_speech.wait_for_playout()
            
        # Then, we delete the room to end the call smoothly
        job_ctx = get_job_context()
        if job_ctx:
            await job_ctx.api.room.delete_room(
                api.DeleteRoomRequest(room=job_ctx.room.name)
            )

# Here's how you might use this in an agent workflow:
async def entrypoint(ctx: JobContext):
    # First, we start the agent session
    session = AgentSession()
    agent = ExampleAgent()
    
    await session.start(
        agent=agent,
        room=ctx.room
    )
How the agent decides to call the tool: 1. User Input Detection: When you say something like "I want to end this call" or "Hang up," the LLM picks up on that as a signal to wrap things up. 2. Tool Invocation: The LLM then creates a response that includes a call to
end_call
, which is marked with
@function_tool()
so the agent knows it can run it. 3. Execution Flow: The
end_call
function runs, waits for any ongoing speech to finish, and then deletes the room to end the call cleanly. If you'd like me to walk you through this more or share some extra examples, just let me know—I’m here to help!
r
If we want to give that control to agent so agent decide to end the call then how we can do that
t
If you want to give the agent the power to decide when to end a call in LiveKit, here’s a friendly way to do it: 1. Define a Hangup Function: You can use the
hangup_call
function to end the call programmatically by calling the
delete_room
API.
Copy code
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,
        )
    )
2. Create an End Call Function Tool: Wrap the hangup logic inside a function tool that the agent can call once it finishes speaking.
Copy code
@function_tool
async def end_call(self, ctx: RunContext):
    """Called when the agent decides to end the call"""
    current_speech = ctx.session.current_speech
    if current_speech:
        await current_speech.wait_for_playout()
    await hangup_call()
3. Implement Decision Logic: Add some logic in the agent’s workflow to decide when to call
end_call
. For example, the agent might choose based on what the user says, the context of the conversation, or specific triggers like recognizing a voicemail system. This setup lets the agent take control and decide the perfect moment to end the call. If you’d like, I’m here to help you with the implementation or answer any questions!