Multi-tool agents
Real AI agents have multiple tools. Give the AI a calculator, a weather API, and a product database and it picks the right tool for each part of a complex question. One question can trigger multiple different tools.
Multi-tool selection
How an agent chooses which tool to use for a query
The AI reads the description field in each tool schema and matches it against the user question. A clear, specific description is the most important factor in correct tool selection. Vague descriptions lead to wrong routing.
def get_weather(city: str) -> dict:
"""Get current weather for a city (simulated)."""
weather_data = {
"tokyo": {"temp": 18, "condition": "Partly cloudy", "humidity": 65},
"london": {"temp": 12, "condition": "Rainy", "humidity": 80},
"new york": {"temp": 22, "condition": "Sunny", "humidity": 55}
}
city_lower = city.lower()
if city_lower in weather_data:
data = weather_data[city_lower]
return {"city": city, "temperature": data["temp"],
"condition": data["condition"], "humidity": data["humidity"]}
return {"error": f"Weather data not available for {city}"}A simulated weather function. In production, this would call a real weather API. The schema tells AI when to use it.
Next, a product search function. This tool takes optional filters for category and price, so the AI can decide which filters to apply based on the user question.
PRODUCTS = [
{"id": 1, "name": "Laptop Pro", "category": "electronics", "price": 1200},
{"id": 2, "name": "Budget Laptop", "category": "electronics", "price": 600},
{"id": 3, "name": "Wireless Mouse", "category": "electronics", "price": 25},
{"id": 4, "name": "Office Chair", "category": "furniture", "price": 300},
]
def search_products(category: str = None, max_price: float = None) -> dict:
"""Search products by category and/or price."""
results = PRODUCTS
if category:
results = [p for p in results if p["category"].lower() == category.lower()]
if max_price:
results = [p for p in results if p["price"] <= max_price]
return {"products": results, "count": len(results)}A product search function with optional filters. The AI decides which filters to apply based on the user's question.
Now the exciting part: combining all three tool types into a single agent. The AI will automatically route each part of a complex question to the right tool.
# Combine ALL tools into one agent
all_tools = calculator_tools + weather_tools + search_tools
# The AI picks the right tools for each part!
answer = call_with_tools(
"What's the weather in London? Also, find me a laptop "
"under $700 and calculate the total if I buy 2.",
all_tools
)
# AI calls: get_weather("London") → Rainy, 12°C
# AI calls: search_products("electronics", 700) → Budget Laptop $600
# AI calls: multiply(600, 2) → 1200
# Answer: "London is rainy at 12°C. The Budget Laptop
# is $600. Two would cost $1,200."One question, three different tools. The AI automatically routes each part of the question to the right function.
Matching exercise: Match questions to tool calls
Loading practice…
Great instinct to ask! The AI can only call functions you put in the registry. It cannot reach outside that sandbox. But you still need to validate the arguments it sends because a malicious prompt could try to trick the AI into passing unexpected values. Here are the key security rules:
Input validation is your first line of defense. Never execute arbitrary code from AI, only call pre-defined functions from your registry. Always validate the arguments the AI sends before executing. A malicious prompt could trick the AI into passing unexpected values.
Safety practices for production: Set max_iterations to prevent infinite loops. Log all tool calls for debugging and auditing. Handle errors gracefully by returning error messages instead of crashing, so the AI can adjust its approach.
Checkpoint: Function calling concepts
Loading practice…
Hints: Hints
Loading practice…
You can now build multi-tool agents that pick the right function for each task. Next, we will review best practices for security, cost, and reliability in production tool-calling systems.