In the previous chapter, Agent Skill (Progressive Disclosure), we taught our agent how to find the right instruction manual (Skill) from a massive library.
Now that the agent has the manual, it needs to actually do something. It needs to check the weather, query a database, or send an email. In the world of AI, we call these capabilities Tools.
There is a fundamental clash in AI development:
Tool Design is the art of building the dashboard that connects the Pilot to the Plane.
If you give the pilot a button labeled "Do Stuff," they will be confused. They might press it at the wrong time. If the button breaks, and a red light just flashes "ERROR," the pilot won't know how to fix it.
A good tool is a Contract. It guarantees: "If you give me specifically this input, I promise to do that action."
Let's build a tool for a weather agent. We want the agent to tell us if we need an umbrella.
Many developers write tool descriptions for themselves.
This is terrible for an AI. It doesn't tell the AI when to use it or what creates a valid input. The description is actually a prompt that tells the AI how to behave.
Imagine you need to check the weather.
get_latitude(), get_longitude(), and fetch_weather_by_coords().get_weather(city_name).The Consolidation Principle: If a human engineer would find it annoying to call three functions to do one simple task, the agent will too.
Agents have a limited "attention budget." Every time they have to use a tool, they risk getting distracted or hallucinating. Group related steps into a single, robust tool.
Let's look at how we define a tool in Python. We treat the tool definition as a dictionary that we feed to the LLM.
We must be strict about what we accept. We use a concept called a Schema (often defined using JSON or libraries like Pydantic/Zod).
weather_tool_definition = {
"name": "get_weather",
"description": "Get current weather for a specific city.",
"parameters": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "City and State, e.g., 'Austin, TX'"
},
"unit": {
"type": "string",
"enum": ["celsius", "fahrenheit"]
}
},
"required": ["location"]
}
}
Explanation: This acts as the "Dashboard Label." It tells the agent exactly what switches (parameters) exist. Notice the enumโwe restrict the AI to only two specific choices for units.
This is the rigid code that runs when the AI pulls the lever.
def execute_weather_tool(location, unit="fahrenheit"):
"""The actual Python function the agent triggers."""
# In a real app, this calls a Weather API
if location.lower() == "london":
return "Rainy, 55 degrees"
else:
return "Sunny, 75 degrees"
What happens if the Agent tries to use the tool incorrectly?
get_weather(location="The Big Apple")404 Not Found. (The agent doesn't know what to do).Error: Location 'The Big Apple' not recognized. Please provide a standard City format like 'New York, NY'.If we design error messages to be instructive, the Agent can self-correct. It reads the error, thinks "Oops," and tries again with "New York, NY".
How does the fuzzy brain actually "click" the rigid button? It happens in a loop.
Here is a simplified view of how we handle the Agent's request to use a tool.
import json
def handle_tool_call(tool_call_json):
"""Parses the AI's string response into a function call."""
# 1. Parse the fuzzy string into a rigid Dictionary
data = json.loads(tool_call_json)
tool_name = data.get("name")
params = data.get("arguments")
# 2. Route to the correct function
if tool_name == "get_weather":
return execute_weather_tool(**params)
return "Error: Tool not found."
Explanation: The AI gives us a JSON string. We convert that string into Python variables. If the string is malformed (e.g., missing a quote), json.loads will fail. A robust system catches that failure and sends a "Fix your JSON" error message back to the AI.
The final part of the contract is the Return Value. Remember Chapter 1: Context Engineering? We have a limited context window.
If get_weather returns a 5,000-line JSON blob with historical barometric pressure, we are filling the workbench with junk.
Design your tools to return concise summaries.
{"condition": "Rain", "temp": 55, "unit": "F"}In this chapter, you learned:
Now we have a context (Workbench), a manual (Skills), and a dashboard (Tools). But simply calling a tool isn't enough. The agent needs to think about the result. It needs to look at the weather, realize it's cold, and then decide to check for coat prices.
We call this process Interleaved Thinking.
Next Chapter: Interleaved Thinking
Generated by Code IQ