rhythmic-jordan-68259
08/05/2025, 8:54 PMtall-airline-16756
08/05/2025, 8:54 PMtall-airline-16756
08/05/2025, 8:54 PMdelete_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!rhythmic-jordan-68259
08/05/2025, 10:35 PMtall-airline-16756
08/05/2025, 10:35 PMasync 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 Docsrhythmic-jordan-68259
08/05/2025, 10:36 PMtall-airline-16756
08/05/2025, 10:36 PMend_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!rhythmic-jordan-68259
08/05/2025, 10:38 PMtall-airline-16756
08/05/2025, 10:38 PMend_call function in LiveKit using LangGraph, and how the agent decides to use this handy tool:
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!rhythmic-jordan-68259
08/05/2025, 10:45 PMtall-airline-16756
08/05/2025, 10:46 PMhangup_call function to end the call programmatically by calling the delete_room API.
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.
@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!