Sub topic 16
Testing and evaluating tool use
This chapter assumes you have read the earlier chapters. You should know how a tool definition looks, how the loop runs calls, and that the model only proposes calls. You should also be comfortable writing Python tests.
What you are testing
Your dispatcher and validator are ordinary code, so normal unit tests cover them. The model is the uncertain part. It can pick the wrong tool, invent an argument, or call a tool when it should just answer. Evaluating tool use means measuring how often the model makes those mistakes.
Test two layers separately:
- Your code: does an unknown tool, a missing argument, or an exception produce an error result instead of a crash?
- The model's choices: given a prompt, does it propose the right tool with the right arguments?
Build a small test set
Start with 15 to 30 cases. Each case holds a prompt and the call you expect. Include cases where no tool should be called, and cases where two similar tools could be confused. Add every real failure you see in production as a new case.
Score the first proposed call
Only run the first model reply. You are checking the decision, so you do not need to execute the tool or wait for a final answer. The scorer below compares the tool name exactly and checks only the arguments you list in expected. Extra arguments such as an optional unit are not penalized.
CASES = [
{"prompt": "What is the weather in Oslo?",
"expected": {"name": "get_weather", "arguments": {"city": "Oslo"}}},
{"prompt": "Tell me a joke.", "expected": None},
]
def score_case(call_model, case):
reply = call_model([{"role": "user", "content": case["prompt"]}])
calls = reply.get("tool_calls", [])
expected = case["expected"]
if expected is None:
return len(calls) == 0
if len(calls) != 1:
return False
call = calls[0]
if call["name"] != expected["name"]:
return False
return all(call["arguments"].get(k) == v
for k, v in expected["arguments"].items())
def evaluate(call_model, cases, runs=3):
passed = total = 0
for case in cases:
for _ in range(runs):
total += 1
if score_case(call_model, case):
passed += 1
return passed / total
def fake_model(messages):
if "weather" in messages[-1]["content"].lower():
return {"role": "assistant", "content": None, "tool_calls": [
{"id": "call_1", "name": "get_weather",
"arguments": {"city": "Oslo"}}]}
return {"role": "assistant", "content": "Why did the chicken..."}
print(evaluate(fake_model, CASES))Replace fake_model with a function that calls your provider. Model output varies between runs, so the runs parameter repeats each case. A case that passes 3 of 3 times is much more trustworthy than one that passes once.
Common errors
- Testing only easy prompts. Add vague requests, typos, and prompts that mention a tool's topic without needing it.
- Requiring an exact argument match. A model may write "oslo" instead of "Oslo". Decide which fields to normalize before comparing.
- Ignoring the no-tool cases. A model that always calls a tool scores perfectly on positive cases.
- Changing prompts and tool descriptions without rerunning the suite. Any edit to a description can change which tool the model picks.
Exercise
Your test set contains only prompts that need a tool, and your model scores 100%. Why can this still hide a problem, and what do you add?
Show answer
The model might call a tool for every prompt, including ones that need none. Add cases where the expected result is no tool call, plus cases that tempt the model toward a similar but wrong tool.