Lesson 19.2 · 2 of 4 · 4 min read · Reviewed 24 Nov 2026
Tool-call accuracy and argument checks
By the end, you can
- Score tool selection and arguments.
- Write checks for tool calls in agent traces.
What to check
| Check | Example rule |
|---|---|
| Tool choice | First call is lookup_order |
| Arguments | order_id equals “1042”; amount is a number in dollars |
| Order | verify_identity before issue_refund |
| Must not | Never delete_account; never send_email to an address not on file |
| Count | search called no more than 3 times |
Writing the checks
Record each case’s traceTrace: A record of everything that happened during one request or agent run, as a tree of timed steps. as a list of calls, then test it in code:
calls = [c for c in trace if c["type"] == "tool_use"]
names = [c["name"] for c in calls]
assert names[0] == "lookup_order"
assert calls[0]["input"]["order_id"] == case["order_id"]
assert names.index("verify_identity") < names.index("issue_refund")
assert "delete_account" not in names
Flexible matching
AgentsAgent: A system where a model decides its own next steps, calling tools in a loop until the task is done. may add harmless extra steps, such as a second lookup. Prefer rules like “X happened before Y” and “Z never happened” over exact sequence matches, unless the task truly has one valid path.
Fixes this points to
Wrong tool or wrong arguments usually point to tool design: vague descriptions, confusing parameter names, or overlapping tools (chapter 35).
Key points
- Check that the agent picked a suitable tool at each step and filled its arguments correctly.
- Compare against expected calls where there's one right answer; use rules (must, must not) where several paths are fine.
- Argument errors (wrong id, wrong units, missing field) are a common and easy-to-test failure.
Check tool calls and their arguments
Write code checks for tool choice, argument values and call order in recorded agent traces.
You will have: A check_trace() function that returns a list of named problems per case.
Steps
- Read the three cases: each has the expected order id, amount in cents and a recorded trace.
- Check that the first call is lookup_order with the right order_id.
- Check that create_refund's amount_cents matches the expected value.
- Check that search is called no more than 3 times, then run all cases.
Done when
- case-1 has no problems.
- case-2 reports the wrong amount (45 instead of 4500 cents).
- case-3 reports too many searches.
Open in the Playground No API key needed.
Starter code (Python)
CASES = [
{"id": "case-1", "order_id": "1042", "amount_cents": 4500, "trace": [
{"name": "lookup_order", "input": {"order_id": "1042"}},
{"name": "create_refund", "input": {"order_id": "1042", "amount_cents": 4500}}]},
{"id": "case-2", "order_id": "1043", "amount_cents": 4500, "trace": [
{"name": "lookup_order", "input": {"order_id": "1043"}},
{"name": "create_refund", "input": {"order_id": "1043", "amount_cents": 45}}]},
{"id": "case-3", "order_id": "1044", "amount_cents": 1200, "trace": [
{"name": "lookup_order", "input": {"order_id": "1044"}}] + [{"name": "search", "input": {"q": "refund rules"}}] * 4 + [
{"name": "create_refund", "input": {"order_id": "1044", "amount_cents": 1200}}]},
]
def check_trace(case):
problems = []
names = [c["name"] for c in case["trace"]]
# TODO: first call must be lookup_order with input order_id == case["order_id"]
# TODO: every create_refund must have amount_cents == case["amount_cents"]
# TODO: search may be called at most 3 times
return problems
for case in CASES:
p = check_trace(case)
print(case["id"], "OK" if not p else p)
Show a solution
CASES = [
{"id": "case-1", "order_id": "1042", "amount_cents": 4500, "trace": [
{"name": "lookup_order", "input": {"order_id": "1042"}},
{"name": "create_refund", "input": {"order_id": "1042", "amount_cents": 4500}}]},
{"id": "case-2", "order_id": "1043", "amount_cents": 4500, "trace": [
{"name": "lookup_order", "input": {"order_id": "1043"}},
{"name": "create_refund", "input": {"order_id": "1043", "amount_cents": 45}}]},
{"id": "case-3", "order_id": "1044", "amount_cents": 1200, "trace": [
{"name": "lookup_order", "input": {"order_id": "1044"}}] + [{"name": "search", "input": {"q": "refund rules"}}] * 4 + [
{"name": "create_refund", "input": {"order_id": "1044", "amount_cents": 1200}}]},
]
def check_trace(case):
problems = []
calls = case["trace"]
names = [c["name"] for c in calls]
if not calls or names[0] != "lookup_order":
problems.append("first call is not lookup_order")
elif calls[0]["input"].get("order_id") != case["order_id"]:
problems.append("lookup_order has the wrong order_id")
for c in calls:
if c["name"] == "create_refund" and c["input"].get("amount_cents") != case["amount_cents"]:
problems.append(f"amount_cents {c['input'].get('amount_cents')} != {case['amount_cents']}")
if names.count("search") > 3:
problems.append(f"search called {names.count('search')} times (max 3)")
return problems
for case in CASES:
p = check_trace(case)
print(case["id"], "OK" if not p else "; ".join(p))