How can I make sure the agent only calls a functio...
# ask-ai
l
How can I make sure the agent only calls a function_tool (store_booking_details) once the booking details were confirmed by the agent?
t
Thanks for reaching out, Omar. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
Let's make sure the
store_booking_details
function only runs after the agent has confirmed the booking details. Here's a friendly guide to help you with that: • Define the Tool Clearly: It’s helpful to clearly state in the tool’s description that it should only be used once the agent has confirmed the booking details. For example, you could say, "Use this tool only when the booking details are confirmed by the agent." • Validate Preconditions: Try adding checks to make sure all important details (like date, time, customer name) are provided before the tool runs. If something’s missing, you can return a gentle error message asking the agent to gather that info. • Use Conversation State: You can keep track of whether the necessary details have been confirmed by using the conversation state. The tool can check this before moving forward. • Control Agent Responses: If you want, you can suppress the agent’s response after the tool runs by returning
None
or handling events to cancel the reply. By following these steps, you’ll have a smoother and more predictable process. If you’d like me to walk you through any part of this or provide examples, just let me know—I’m here to help! Sources: Tool definition and use | LiveKit Docs | Tool definition and use | LiveKit Docs | agent.py | examples/voice_agents/silent_function_call.py
l
Another issue that I noticed happens with realtime, when the agent is storing the booking details, it returns two responses, one during the call and one is the response from the function.
t
I'd be happy to help clarify how your booking details are being processed and stored. Could you please share a bit more about the following? • Are the booking details sent through LiveKit data messages? • Are they saved in an external database, or are they managed using function calls? • Do you need to prioritize or merge the responses in any way? Understanding these details will really help us find the best way to handle those duplicate responses together. Sources: pkg/rtc/room.go | Voice activity detection (VAD) parameters | LiveKit Docs
l
Assuming the openai-realtime voice agent has to call a function_tool, right now, it responds to the user and then directly responds again with the tool's response. is there a way to make sure the response only happens once
t
To help make sure the agent responds just once when calling a
function_tool
, here are a few friendly approaches you can try: • Cancel Automatic Replies: You can add an event handler for the
function_tools_executed
event and call
ev.cancel_tool_reply()
to stop the automatic response that usually happens after the tool runs:
Copy code
@session.on("function_tools_executed")
def on_function_tools_executed(ev):
    ev.cancel_tool_reply()
Return
None
from the Tool:
Make sure your tool doesn’t return any value, or explicitly returns
None
, which helps prevent an automatic reply from being triggered. • Disable Turn Detection: If you’re working with the OpenAI Realtime API, you can turn off
turn_detection
and take charge of when the agent speaks by using
generate_reply()
yourself:
Copy code
model = openai.realtime.RealtimeModel(voice="alloy", turn_detection=None)
agent.generate_reply(on_duplicate="cancel_existing")
I hope these tips make things clearer and easier for you! Please feel free to reach out if you’d like me to walk you through any part of the implementation or if you have any other questions. I’m here to help! Sources: livekit-agents/livekit/agents/voice/generation.py | Voice activity detection (VAD) parameters | LiveKit Docs