with a realtime model like openai realtime, how ca...
# ask-ai
m
with a realtime model like openai realtime, how can i wait for my agent to finish talking before execute the tool? in my case it's the transfer tool
t
Thanks for reaching out, Droxy. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
To make sure the agent finishes speaking before using a tool like the transfer tool, here are a couple of easy ways you can do it: • Using `SpeechHandle.wait_for_playout()`: This method waits until the agent’s speech is completely done before moving on. Here’s an example:
Copy code
# Start the speech and wait for it to finish
handle = session.say("Let me transfer you now.")
await handle.wait_for_playout()

# Now, execute the transfer tool
await perform_tool_action()
• Using `RunContext.wait_for_playout()`: If you’re inside a function tool, this method helps you pause until the agent finishes talking before running the tool. Like this:
Copy code
@function_tool
async def transfer_tool(ctx: RunContext):
    # Wait for the agent to finish speaking
    await ctx.wait_for_playout()

    # Then do the transfer action
    await perform_tool_action()
Also, to avoid any interruptions while the agent is speaking, you can set
allow_interruptions=False
when calling
say()
, or use
RunContext.disallow_interruptions()
inside a function tool. If you want me to walk you through this or have any questions, just let me know—I’m here to help! Sources: livekit-plugins/livekit-plugins-openai/livekit/plugins/openai/realtime/real... | Agent speech and audio | LiveKit Docs
m
does wait_for_playout work with realtime models?
t
Absolutely!
wait_for_playout
works smoothly with real-time models like OpenAI Realtime. It makes sure the agent finishes speaking before moving on to the next steps, even when dealing with real-time speech pipelines. With real-time models,
SpeechHandle.wait_for_playout()
keeps an eye on the speech’s progress and waits until the audio finishes playing or gets interrupted. This way, any follow-up actions, like running tools, happen only after the speech is completely done. If you’re working with a function tool, you can use
RunContext.wait_for_playout()
to pause until the agent’s speech wraps up before running the tool—this helps avoid any tricky circular dependencies. Feel free to ask if you want me to walk you through it with examples or if anything’s unclear! Sources: livekit-agents/livekit/agents/beta/workflows/email_address.py | Realtime model integrations | LiveKit Docs