Sub topic 14
Evaluating agents with test tasks
This chapter assumes you can read the agent loop from earlier chapters, including run_agent, run_tool, and the step limit. You also need basic Python and JSON.
Why agents need task-based tests
An agent makes many decisions per run. A small prompt change can fix one task and quietly break three others. You cannot see this by reading a few transcripts. You need a fixed set of test tasks that you rerun after every change.
A test task has three parts:
- A goal, written as the user would write it.
- A check that decides pass or fail.
- Optionally, limits such as a maximum number of steps.
What to check
Check outcomes first, then process.
| Check | Question it answers | Example |
|---|---|---|
| Final answer | Did the agent reach the goal? | The answer contains "18" |
| Tool use | Did it call the right tool with the right arguments? | `get_weather` called with `city="Oslo"` |
| Efficiency | Did it finish within budget? | At most 3 steps |
| Safety | Did it avoid forbidden actions? | No call to a delete tool |
Prefer checks on the final answer and the tool calls. Avoid checks on exact wording, because a correct answer can be phrased many ways.
A small evaluation harness
The harness needs the agent to report what it did. The loop from the earlier chapter keeps its history in messages but does not return it. Here is a version that returns the final answer and the list of tool calls, so tests can inspect both.
import json
def get_weather(city: str) -> str:
return json.dumps({"city": city, "temp_c": 18})
TOOLS = {"get_weather": get_weather}
def run_tool(name: str, arguments: dict) -> str:
if name not in TOOLS:
return json.dumps({"error": f"unknown tool: {name}"})
try:
return TOOLS[name](**arguments)
except Exception as exc:
return json.dumps({"error": str(exc)})
def run_agent_traced(call_model, user_goal: str, max_steps: int = 8):
messages = [{"role": "user", "content": user_goal}]
calls = []
for _ in range(max_steps):
reply = call_model(messages)
messages.append({"role": "assistant", "content": reply.get("content"), "tool_call": reply.get("tool_call")})
call = reply.get("tool_call")
if call is None:
return reply["content"], calls
calls.append(call)
result = run_tool(call["name"], call["arguments"])
messages.append({"role": "tool", "name": call["name"], "content": result})
return "Stopped: step limit reached.", calls
def fake_model(messages):
if messages[-1]["role"] == "user":
return {"content": None, "tool_call": {"name": "get_weather", "arguments": {"city": "Oslo"}}}
return {"content": "It is 18 C in Oslo.", "tool_call": None}
TASKS = [
{
"goal": "What is the weather in Oslo?",
"expect_tool": "get_weather",
"expect_in_answer": "18",
"max_calls": 2,
},
]
def evaluate(call_model, tasks):
passed = 0
for task in tasks:
answer, calls = run_agent_traced(call_model, task["goal"])
ok = (
any(c["name"] == task["expect_tool"] for c in calls)
and task["expect_in_answer"] in answer
and len(calls) <= task["max_calls"]
)
passed += ok
print(("PASS" if ok else "FAIL"), task["goal"])
print(f"{passed}/{len(tasks)} passed")
evaluate(fake_model, TASKS)The scripted model makes this run deterministic, which tests the harness itself. With a real model, run the same tasks the same way and compare scores between versions.
Common errors
- Too few tasks. Five tasks cannot show a small regression. Start with 20 or more and add one each time you find a real bug.
- Only happy paths. Include tasks where a tool fails, an argument is missing, or the goal is impossible. The correct result may be a clear refusal.
- One run per task. Real models vary between runs. Run each task several times and track the pass rate.
- Exact-match checks. They fail correct answers. Check for key facts instead.
- Ignoring cost. Record steps per task. A change that passes more tasks but doubles the steps may not be an improvement.
Exercise
Your agent passes 9 of 10 test tasks on one run and 6 of 10 on the next, with no code changes. What does this tell you, and what should you do?
Show answer
The model's output varies between runs, so a single run is not reliable. Run each task several times (for example 5) and report the pass rate per task. Then investigate tasks that pass only sometimes, since they point to fragile prompts or tool descriptions.