Sub topic 17
Testing tool selection and refining definitions
This chapter assumes you can call an LLM API with tools, read JSON Schema, and read short Python examples. You have already written names, descriptions, and schemas. Now you measure whether the model uses them correctly.
Why test tool selection
A tool definition is a prompt. Small wording changes can change which tool the model picks or what arguments it sends. Without a test set, you cannot tell whether an edit helped or only fixed the one case you were looking at.
The working model is a loop: write a set of realistic prompts, record what the model does, change one thing in the definitions, and run the same prompts again.
Build a small test set
Each case pairs a user prompt with the tool you expect. Include three kinds of case:
- Prompts that clearly match one tool.
- Prompts that sit near the boundary between two similar tools.
- Prompts that need no tool at all, where the correct answer is
None.
Use phrasing real users write, not phrasing copied from your descriptions. A test that repeats the description word for word proves little.
A minimal evaluation harness
You supply choose_tool, which wraps your own model call and returns the name of the tool the model called, or None.
CASES = [
("Where is order ord_1001?", "get_order"),
("Show me my cancelled orders from last month", "search_orders"),
("What is the capital of France?", None),
]
def run_eval(choose_tool):
"""choose_tool(prompt) -> name of the tool the model called, or None."""
failures = []
for prompt, expected in CASES:
actual = choose_tool(prompt)
if actual != expected:
failures.append((prompt, expected, actual))
passed = len(CASES) - len(failures)
print(f"{passed}/{len(CASES)} correct")
for prompt, expected, actual in failures:
print(f" {prompt!r}: expected {expected}, got {actual}")
return failuresRun it with the same model, the same system prompt, and the lowest temperature your provider allows. Model output varies, so run each case several times if a result looks unstable.
Check arguments as well as tool choice
Picking the right tool is only half the job. A call to search_orders with status: "canceled" fails against your enum. Extend each case to store the expected arguments, or at least the ones that matter, and compare them too. Also record the validation errors your server returns, since they show which parameter descriptions are unclear.
Read failures and edit one thing
Group failures by cause before you edit anything.
| Symptom | Likely cause | Fix |
|---|---|---|
| Model picks `search_orders` when an ID is given | Description does not state the boundary | Add "If the user gives an order ID, use get_order instead" |
| Model calls a tool for a general question | Description is too broad | State when not to use the tool |
| Date sent as "last month" | Format not stated | Add the format and an example to the parameter description |
| Model never recovers after an error | Error message gives no next step | Name the tool or action to try next |
Change one definition per run. If you change three things and the score rises, you do not know which change helped. Keep a record of each edit and its score.
Guard against regressions
A fix for one case can break another. Always rerun the full set, not only the failing case. When a real user prompt fails in production, add it to CASES. The set grows from real failures and stays useful over time.
Exercise
Your eval shows the model calls search_orders for "Where is ord1001?" when it should call `getorder`. You want to fix it. What do you change, and what do you do afterward?
Show answer
Add the boundary to the search_orders description, such as "If the user gives an order ID, use get_order instead", and change only that. Then rerun the whole test set, not just this case, to confirm the fix did not break other cases.