wide-dress-57902
08/20/2025, 11:09 PMtall-airline-16756
08/20/2025, 11:09 PMtall-airline-16756
08/20/2025, 11:10 PMRunContext isn’t automatically passed into all function tool calls. It only gets injected when your function explicitly includes a parameter typed as RunContext. The system checks the function’s parameters to decide if it should add the context.
So, if you want your function tool to receive RunContext, make sure to add it to your function signature like this:
@function_tool()
async def my_tool(
self,
context: RunContext, # You need to declare this explicitly
other_param: str,
) -> str:
# Here you can access session, function_call, speech_handle, userdata via context
return "Tool executed"
Just a heads-up: RunContext gives you access to the current session, function_call, speech_handle, and userdata, but you have to opt-in by adding that type annotation. Hope that helps!
Sources: livekit-agents/livekit/agents/llm/utils.py | Tool definition and use | LiveKit Docswide-dress-57902
08/20/2025, 11:11 PMtall-airline-16756
08/20/2025, 11:11 PM@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()
# Delete the room to end the call for all participants
job_ctx = get_job_context()
await job_ctx.api.room.delete_room(
api.DeleteRoomRequest(
room=job_ctx.room.name,
)
)
Also, you can keep an eye on the SIP participant’s call status (like sip.callStatus) to catch when they want to hang up. When the status changes to hangup, that’s your cue to delete the room and wrap things up.wide-dress-57902
08/20/2025, 11:13 PMtall-airline-16756
08/20/2025, 11:17 PMcurrent_speech attribute in LiveKit shows you the speech that's currently being generated or played by the agent. It gives you a SpeechHandle object, or None if there's no speech happening at the moment.
Here’s how it can help you:
• Waiting for Speech to Finish: You can use current_speech to pause and wait until the agent finishes speaking before moving on to the next step:
# Wait for the agent to finish speaking
current_speech = ctx.session.current_speech
if current_speech:
await current_speech.wait_for_playout()
• Handling Interruptions: The system uses current_speech to manage interruptions smoothly. For example, it checks if the current speech can be interrupted when a user starts talking.
• Manually Interrupting Speech: If you want to stop the current speech yourself, you can call the interrupt() method like this:
current_speech = self._current_speech
if current_speech is not None:
current_speech = current_speech.interrupt()
Some quick details:
• Type: Returns SpeechHandle | None
• Access: You can get it through the session context (ctx.session.current_speech)
• State Management: LiveKit takes care of managing this automatically
• Thread Safety: It’s safely handled within the agent’s internal locking to avoid any concurrency issues
In short, current_speech is your go-to for keeping speech operations in sync, making sure things happen at the right time, and managing interruptions smoothly in voice-based apps.
Sources: livekit-agents/livekit/agents/voice/agent_activity.py | Agent speech and audio | LiveKit Docswide-dress-57902
08/23/2025, 8:28 PMtall-airline-16756
08/23/2025, 8:28 PMwait_for_playout function before shutting down the room the agent is in. This way, any speech the agent is currently delivering gets to finish smoothly, so the user doesn’t experience any sudden cutoffs.
Here’s a handy pattern to follow:
@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()
# Proceed to shut down the room
await hangup_call()
By calling wait_for_playout, you make sure the agent’s last message is fully heard, keeping the call ending feeling smooth and professional.
Sources: livekit-agents/livekit/agents/job.py | Agents telephony integration | LiveKit Docs | Agent speech and audio | LiveKit Docs | agent.py