Designing Simple Tool Interfaces › The Anatomy of a Tool
AI Agent HandbookAll chapters Designing Simple Tool Interfaces › The Anatomy of a ToolOne section · Sections in this chapter3 / 12 ↓
Let's break down what a tool actually consists of. We'll use a simple weather tool as our example:
The Tool Function
At its most basic, a tool is just a Python function:
def get_weather(city: str) -> dict:
"""Get current weather for a city."""
# In reality, this would call a weather API
# For now, we'll return mock data
return {
"city": city,
"temperature": 72,
"conditions": "partly cloudy",
"humidity": 65
}This function takes a city name and returns weather information. Simple enough. But how does the agent know this function exists? How does it know what parameter to pass? That's where tool descriptions come in.
The Tool Description
The agent needs metadata about the tool. Think of this as the function's documentation, but in a format the agent can understand:
weather_tool = {
"name": "get_weather",
"description": "Get current weather conditions for a specified city",
"parameters": {
"city": {
"type": "string",
"description": "The name of the city (e.g., 'Tokyo', 'New York', 'London')"
}
}
}This description tells the agent:
- What the tool does: "Get current weather conditions"
- What it's called:
get_weather - What inputs it needs: A city name (string)
- What the parameter means: Examples help the agent understand the format
With this information, the agent can decide whether this tool is appropriate for a given question and extract the necessary parameters.
The Tool Registry
If you have multiple tools, you need a way to organize them. A simple registry is just a dictionary mapping tool names to their functions:
tool_registry = {
"get_weather": get_weather,
"calculate": calculate,
"search_web": search_web
}When the agent decides to use a tool, it looks up the function in this registry and calls it with the appropriate parameters.