How can I make a tool call only after the agent ha...
# ask-ai
b
How can I make a tool call only after the agent has finished speaking?
t
Thanks for reaching out, Charles. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
To make sure a tool call happens only after the agent has finished speaking, here are a few friendly ways you can do it: • Using `wait_for_playout()`: This handy method waits until the assistant is completely done talking. Here’s a quick example:
Copy code
@function_tool
async def my_tool_function(self, ctx: RunContext):
    current_speech = ctx.session.current_speech
    if current_speech:
        await current_speech.wait_for_playout()
    return "Tool executed after speech finished"
• Using PlaybackFinished Event: You can listen for the
playback_finished
event to know exactly when the agent has stopped speaking:
Copy code
from livekit.agents.voice.io import PlaybackFinishedEvent

@session.output.audio.on("playback_finished")
def _on_playback_finished(ev: PlaybackFinishedEvent):
    logger.info(f"playback finished: {ev.interrupted}")
    # This is where you can trigger your tool execution
• Coordinating with Speech Generation: Another approach is to generate the reply, wait for the speech to wrap up, and then run your tool logic like this:
Copy code
# Generate speech first
handle = await session.generate_reply(instructions="I'll process that for you now.")

# Wait for the speech to complete
await handle.wait_for_playout()

# Now execute your tool logic
result = await my_tool_function()
These options help make sure your tool only kicks in after the agent has finished talking. If you want me to walk you through any part of this or have more questions, just let me know—I’m here to help! Sources: livekit-agents/livekit/agents/voice/agent_activity.py | How to detect when an agent has finished speaking
b
In my assistant, I have a tool call for when the conversation finishes. How can I use the tool call to inform the session that after the next output playback is done, to disconnect
t
Here’s a friendly way to let the session disconnect right after the next output finishes playing: • Using `wait_for_playout()`: This makes sure the agent finishes speaking before it disconnects. For example:
Copy code
@function_tool
async def end_session(self, ctx: RunContext):
    # Wait for the current speech to finish
    current_speech = ctx.session.current_speech
    if current_speech:
        await current_speech.wait_for_playout()

    # Close the session
    await ctx.session.aclose()
• Another option: You can generate a final reply first, then close the session like this:
Copy code
@function_tool
async def goodbye(self):
    await self.session.generate_reply(instructions="Say goodbye to the user")
    current_speech = self.session.current_speech
    if current_speech:
        await current_speech.wait_for_playout()
    await self.session.aclose()
These approaches help the session wrap up smoothly after the playback finishes. If you want a hand with anything else, just let me know—I’m here to help! Sources: livekit-agents/livekit/agents/voice/agent.py | How to detect when an agent has finished speaking | Agent speech and audio | LiveKit Docs | basics/exit_message.py