How to prevent interruption of agent reply after a...
# ask-ai
a
How to prevent interruption of agent reply after a tool call?
t
Thanks for reaching out, Rod. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
Hi there! To help you avoid any interruptions in an agent's reply after a tool call, you can use the
cancel_tool_reply()
method inside the
function_tools_executed
event handler. This handy method stops the agent from automatically crafting a reply based on the tool's output. Here’s a simple example to guide you:
Copy code
@session.on("function_tools_executed")
def on_function_tools_executed(ev: FunctionToolsExecutedEvent):
    # Cancel the automatic tool reply
    ev.cancel_tool_reply()
Also, just so you know, if a tool doesn’t return any value, it won’t trigger an automatic reply. For instance:
Copy code
@function_tool()
async def turn_on_light(self):
    """Called when user asks to turn on the light."""
    self.light_on = True
    logger.info("Light is now on")
    # No return value means no automatic reply
And if you happen to call multiple tools at the same time, as long as at least one returns a value, a reply will be generated—unless you explicitly cancel it with
cancel_tool_reply()
. If anything feels unclear or you’d like a hand with this, please don’t hesitate to ask. I’m here to help! Sources: Turn detection and interruptions | LiveKit Docs | livekit-agents/livekit/agents/voice/agent_session.py
a
When the user speaks during the tool call, the reply gets interrupted
t
I totally understand how important it is to have smooth, uninterrupted replies from the tool. To help with that, you can simply disable interruptions by setting
allow_interruptions=False
when you call
generate_reply()
or
say()
. This way, the agent can finish its response fully before taking any more input from the user. Here’s a quick example:
Copy code
await session.generate_reply(allow_interruptions=False)
And if you want to use direct speech output, you can do it like this:
Copy code
session.say("Please wait while I process that.", allow_interruptions=False)
Another option is to set this globally by passing
allow_interruptions=False
when you create your
AgentSession
. Just a heads-up though: if you're working with a realtime model that has built-in turn detection, this setting won’t take effect unless turn detection is turned off on the server side. If anything’s unclear or if you’d like me to walk you through it more, please don’t hesitate to ask—I’m here to help! Sources: Turn detection and interruptions | LiveKit Docs | Agent speech and audio | LiveKit Docs | livekit-agents/livekit/agents/voice/agent_session.py | docs/index.yaml | Agent Session & Activity Management | livekit/livekit_composite | DeepWiki