Sub topic 5
Giving the agent tools
This chapter assumes you can read basic Python, handle JSON, and know the marketing metrics impressions, clicks, conversions and spend. It also assumes you have prompted a language model before.
Why an agent needs tools
A language model only produces text. It cannot open your ad platform, read a spreadsheet, or divide 150 by 10 reliably. A tool is an ordinary function that the agent is allowed to request. The model does not run the function. It emits a request naming the tool and its arguments, and your code runs the function and returns the result. This is also called function calling.
Tools matter for reporting because they split the work by strength:
- The model decides what to look up and how to explain it.
- Your code fetches data and does the arithmetic.
The tool request cycle
Your code sits between the model and the function. That position is where you validate input, log calls, and enforce permissions.
What makes a tool definition
Each tool needs three things:
- A name the model can request.
- A description and an argument schema that tell the model when and how to use it. The model sees only this text, so write it like documentation.
- A function that returns plain data, such as a dictionary that serializes to JSON.
Here is a deterministic metrics tool with its definition and a registry. The function never raises on a zero denominator. It returns None, so the model cannot be handed a crash or a made-up number.
def campaign_metrics(rows):
"""Compute standard performance metrics from per-campaign rows."""
impressions = sum(r["impressions"] for r in rows)
clicks = sum(r["clicks"] for r in rows)
spend = sum(r["spend"] for r in rows)
conversions = sum(r["conversions"] for r in rows)
return {
"impressions": impressions,
"clicks": clicks,
"spend": round(spend, 2),
"conversions": conversions,
"ctr": round(clicks / impressions, 4) if impressions else None,
"cpc": round(spend / clicks, 2) if clicks else None,
"cpa": round(spend / conversions, 2) if conversions else None,
}
TOOLS = {"campaign_metrics": campaign_metrics}
TOOL_SPECS = [
{
"name": "campaign_metrics",
"description": "Sum campaign rows and compute CTR, CPC and CPA. "
"Returns null for a metric whose denominator is zero.",
"parameters": {
"type": "object",
"properties": {
"rows": {
"type": "array",
"items": {
"type": "object",
"required": ["impressions", "clicks", "spend", "conversions"],
},
}
},
"required": ["rows"],
},
}
]
rows = [
{"impressions": 10000, "clicks": 200, "spend": 150.0, "conversions": 10},
{"impressions": 5000, "clicks": 50, "spend": 60.0, "conversions": 2},
]
print(TOOLS["campaign_metrics"](rows=rows))The next chapter wires TOOLS into a loop that calls the model repeatedly.
Designing good tools
- Keep each tool narrow. Use
get_campaign_rowsandcampaign_metricsinstead of onedo_analysistool. Narrow tools are easier to test and to describe. - Return structured data. Numbers and labeled fields are easier to check later than prose.
- Make errors readable. Return
{"error": "date range exceeds 90 days"}so the model can adjust, instead of a stack trace. - Prefer read-only tools. A tool that pauses a campaign or sends an email has real side effects. Give those their own approval step.
Common errors
- Letting the model do the math. If the model computes CPA in its head, you cannot trust the figure. Put every calculation in a tool.
- Vague descriptions. If two tool descriptions overlap, the model picks the wrong one.
- Trusting arguments. The model can send a misspelled field or a string where a number belongs. Validate before running.
- Returning too much. Dumping 50,000 raw rows into the conversation wastes context. Aggregate inside the tool.
Exercise
A tool computes CPA as spend / conversions. A campaign has zero conversions. What should the tool return, and why not let the model handle it?
Show answer
It should return None (null), as campaign_metrics does. A division by zero would crash the tool or push the model into guessing a value. A null is an explicit "not computable" that the report can state plainly.