rough-butcher-91877
09/23/2025, 5:59 AMtall-airline-16756
09/23/2025, 5:59 AMtall-airline-16756
09/23/2025, 5:59 AMrough-butcher-91877
09/23/2025, 6:01 AMtall-airline-16756
09/23/2025, 6:02 AMRunContext.wait_for_playout() method inside your tool function. This makes sure your tool logic pauses until the speech finishes playing. Here's a simple example to guide you:
@function_tool
async def my_tool(self, run_ctx: RunContext) -> str:
# Wait for the speech tied to this function call to finish
await run_ctx.wait_for_playout()
# Tool logic executes only after speech playout completes
return "Tool completed after speech"
This approach helps avoid any circular waiting and keeps everything running smoothly. If you have any questions or want to dive deeper into the implementation, just let me know—I’m here to help!
Sources: Bringing AI avatars to voice agents | Bringing AI avatars to voice agents | Bringing AI avatars to voice agents | examples/voice_agents/long_running_function.py | agents/src/voice/agent_activity.tsrough-butcher-91877
09/23/2025, 6:05 AMtall-airline-16756
09/23/2025, 6:06 AMRunContext.wait_for_playout() method is a helpful part of the LiveKit Agents framework that lets us patiently wait for the assistant's spoken response (just for the current function call step) to finish before moving forward. Here’s the source code implementation for you to take a look:
async def wait_for_playout(self) -> None:
"""Waits for the speech playout corresponding to this function call step.
Unlike `SpeechHandle.wait_for_playout`, which waits for the full
assistant turn to complete (including all function tools),
this method only waits for the assistant's spoken response prior running
this tool to finish playing."""
await self.speech_handle._wait_for_generation(step_idx=self._initial_step_idx)
For example, if you have a tool that ends a call, you could use it like this:
@function_tool
async def end_call(self, ctx: RunContext):
"""Use this tool when the user has signaled they wish to end the current call."""
await ctx.wait_for_playout() # let the agent finish speaking
await hangup_call() # then end the call
This way, you make sure the agent completes its speech before any other actions take place. If you'd like, I'm here to help with more details or any other questions you might have!
Sources: livekit-agents/livekit/agents/voice/agent_activity.py | Agent speech and audio | LiveKit Docs | Agents telephony integration | LiveKit Docs