How to Give Tools to AI Agent: Simple Guide
To give tools to an
AI agent, you connect it with external APIs or plugins that perform specific tasks. This lets the agent use those tools to fetch data, perform actions, or extend its abilities beyond just text generation.Syntax
Giving tools to an AI agent usually involves defining a list of tools with their names, descriptions, and functions, then passing them to the agent's constructor or setup method.
Each tool typically has:
- Name: Identifier for the tool.
- Description: What the tool does.
- Function: The code or API call the tool executes.
The agent uses this list to decide which tool to call based on user input.
python
tools = [
{
"name": "calculator",
"description": "Performs basic math calculations",
"function": lambda x: eval(x)
},
{
"name": "weather_api",
"description": "Fetches current weather data",
"function": fetch_weather_data
}
]
agent = AI_Agent(tools=tools)
response = agent.handle_query("What is 5 plus 7?")Example
This example shows a simple AI agent that can use a calculator tool to answer math questions.
python
class AI_Agent: def __init__(self, tools): self.tools = {tool['name']: tool for tool in tools} def handle_query(self, query): if "plus" in query: expression = query.replace("What is", "").replace("plus", "+").replace("?", "").strip() return self.tools['calculator']['function'](expression) return "I don't know how to answer that." # Define the calculator tool calculator_tool = { "name": "calculator", "description": "Performs basic math calculations", "function": lambda expr: eval(expr) } # Create agent with the calculator tool agent = AI_Agent(tools=[calculator_tool]) # Ask a math question result = agent.handle_query("What is 5 plus 7?") print(result)
Output
12
Common Pitfalls
Common mistakes when giving tools to AI agents include:
- Not defining clear tool names and descriptions, causing confusion.
- Passing tools without proper function implementations, leading to errors.
- Allowing unsafe code execution (like
eval) without validation. - Not handling unknown queries gracefully.
Always validate inputs and restrict tool capabilities to avoid security risks.
python
wrong_tools = [
{
"name": "calc",
"description": "Does math",
"function": None # Missing function
}
]
# This will cause errors when the agent tries to use the tool
# Correct way:
correct_tools = [
{
"name": "calculator",
"description": "Performs math calculations",
"function": lambda expr: eval(expr)
}
]Quick Reference
- Define tools with name, description, and function.
- Pass tools to the AI agent during setup.
- Agent uses tools based on user input.
- Validate inputs to keep tool usage safe.
Key Takeaways
Give AI agents tools by defining named functions or APIs they can call.
Pass these tools to the agent so it can choose the right one for each task.
Always validate inputs to tools to avoid security risks.
Clear tool descriptions help the agent understand when to use each tool.
Test tools separately before integrating with the AI agent.