I need to add/update new tools dynamically based o...
# ask-ai
f
I need to add/update new tools dynamically based on user's input what is the best place to do it?.. and after couple turns I wanted to remove those tools as well
t
Thanks for reaching out, Janak. 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 dynamically managing tools in LiveKit agents during runtime! You can easily add, update, or remove tools using the
update_tools()
method. Just keep in mind that this method replaces all currently available tools, so when adding new ones, be sure to include any existing tools you'd like to keep. Adding or Updating Tools: • If you want to add a tool on the fly, start by getting the current tools with
agent.tools
, then add your new tool like this:
Copy code
await agent.update_tools(agent.tools + [tool_a])
• If you'd rather replace all tools (including those originally defined in the agent class), simply pass a new list:
Copy code
await agent.update_tools([tool_a, tool_b])
Removing Tools: • To remove a specific tool, you can filter it out from the current tools like this:
Copy code
await agent.update_tools(agent.tools - [tool_a])
Creating Tools Programmatically: • You also have the flexibility to create tools on-the-fly using the
function_tool
function (note: not as a decorator). You'll need to provide a name, description, and a callable function:
Copy code
def _set_profile_field_func_for(self, field: str):
    async def set_value(context: RunContext, value: str):
        return f"field {field} was set to {value}"
    return set_value

# Create tool dynamically
tool = function_tool(
    name=f"set_{field}_profile",
    description=f"Set the {field} field in user profile",
    func=set_value_func
)
await agent.update_tools(agent.tools + [tool])
Please feel free to reach out if you'd like me to walk you through any part of this or if you have any other questions. I'm here to help! Sources: Tool definition and use | LiveKit docs | Agent Session Management & Turn Detection | livekit/livekit_composite | Dee... | livekit-plugins/livekit-plugins-ultravox/livekit/plugins/ultravox/realtime/... | plugins/openai/src/realtime/realtime_model.ts
f
where is the best place to update the tools dynamically? for example if user's message intention is about sending an email I wanted to load send email related tools
t
I’m glad you asked about dynamically updating tools based on the user’s message intention! The best spot to handle this is within the agent’s runtime logic, right when the user’s intent is recognized. This way, you load only the tools that really matter for that specific intent, which helps save resources and makes responses more accurate. Here’s a simple way to dynamically update your tools: 1. First, detect the user’s intent from their message. For example, if they want to send an email, you can identify this through intent recognition or message parsing. 2. Next, use the
update_tools()
method to load the tools that match the detected intent. For example:
Copy code
# Python example
if user_intent == "send_email":
    await agent.update_tools([send_email_tool])
1. If you want to keep the current tools while adding new ones, you can combine them like this:
Copy code
await agent.update_tools(agent.tools + [new_tool])
1. And if some tools are no longer relevant, you can remove them to keep things focused:
Copy code
await agent.update_tools([tool for tool in agent.tools if tool.name != "irrelevant_tool"])
Some best practices to keep in mind: • Always double-check your tools before updating to make sure they work well with your backend. • Try to minimize session restarts by comparing the current and new toolsets before you update. • If your agent’s behavior needs to change with the new intent, update the instructions along with the tools. If you’d like, I’m here to help with more details or examples on how to implement this smoothly—just let me know! Sources: Agent Session Management & Turn Detection | livekit/livekit_composite | Dee... | README.md | Tool definition and use | LiveKit docs | plugins/openai/src/realtime/realtime_model.ts | Tool definition and use | LiveKit docs