My agent is calling a Tool to handoff to another a...
# ask-ai
w
My agent is calling a Tool to handoff to another agent, but after the tool call the old agent starts talking again and after this the new agent enters. How can i prevent speeking after a handoff?
t
Thanks for reaching out, Lukas. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
I’m happy to help you with preventing an agent from speaking after a handoff! You can achieve this by using the
cancel_tool_reply()
function inside the
function_tools_executed
event handler. This way, the old agent won’t generate any speech once the handoff is triggered. Here’s a simple example to guide you:
Copy code
@session.on("function_tools_executed")
def on_function_tools_executed(ev: FunctionToolsExecutedEvent):
    if ev.has_agent_handoff:
        ev.cancel_tool_reply()  # This stops the agent from replying after the handoff
Using this approach will make sure the handoff happens smoothly and silently. If you have any questions or want me to walk you through it further, just let me know—I’m here to help! Sources: livekit-agents/livekit/agents/voice/run_result.py | Tool definition and use | LiveKit Docs
w
and can i somehow check the tool, so that this is only done for the handoff tool?
t
I want to help you make sure that
cancel_tool_reply()
is applied just for a specific handoff tool. You can do this by checking the tool's name inside the
function_tools_executed
event. Here’s a simple example to guide you:
Copy code
@session.on("function_tools_executed")
def on_function_tools_executed(ev: FunctionToolsExecutedEvent):
    # Extract all tool names from the function calls
    tool_names = [fnc.name for fnc in ev.function_calls]
    
    # Cancel reply only if the specific handoff tool was called
    if "your_handoff_tool_name" in tool_names:
        ev.cancel_tool_reply()
This way, the reply gets canceled only when that particular tool runs. If you have any questions or need a hand with anything else, please don’t hesitate to ask—I’m here to help! Sources: livekit-agents/livekit/agents/voice/agent_activity.py | Events and error handling | LiveKit Docs | Tool definition and use | LiveKit Docs