The model chooses the tool and fills the arguments, but your code owns the call: validating the arguments before the tool runs is what stops a hallucinated order number from becoming a refund. Validation is cheap and it belongs in code, not in the prompt, because the model cannot be trusted to check its own homework.
Before you start
A list of your agent’s tools with their parameters, and a way to run a check before each call.
Write the rules per tool
For each tool, write the rules its arguments must pass: the order number matches the pattern ORD-0000, the amount is a positive number, the date is a real date in YYYY-MM-DD form, the enum value is one of the allowed set.
Include the cross-field rules: the refund amount must not exceed the order total, the due date must be after the invoice date.
Validate before the call, not after
Run the rules on the arguments before the tool executes. If they pass, call the tool. If they fail, do not call it: return an error message to the agent as the tool result, naming the field and the rule it broke.
Log every validation failure with the tool name and the bad value, so a pattern of bad arguments can be fixed in the tool description or the prompt.
Let the agent correct and retry
When the agent receives the validation error, it can correct the argument and try again. Count how often the correction succeeds: a tool whose arguments are usually wrong on the first try needs a better description.
If the agent fails the same validation three times, stop and escalate: the problem is the task or the tool, not the retry count.
Practice check: fictional Maple Corner agent, refund tool, 10 calls
The table below is fictional practice material, worked out by hand from the stated validation rules. It is the expected result for the practice calls, not a run of any agent.
| Call | Arguments | Validation | Outcome | Verdict |
|---|---|---|---|---|
| 1 | ORD-4821, $248.00 | Pass | Refund issued | Pass |
| 2 | ORD-9999, $10.00 | Fail – unknown order | Error returned | Pass – blocked |
| 3 | ORD-4821, $-5.00 | Fail – negative amount | Error returned | Pass – blocked |
| 4 | ORD-4821, $248.00 | Pass | Refund issued | Pass |
Two of four calls are blocked by validation before they reach the tool: an unknown order and a negative amount. The two valid calls go through and the refunds are issued.
Expected result. UNTESTED: the calls in the practice table are worked out by hand, not measured by running an agent. Write the validation rules for your own tools, check the arguments before each call, and return the error to the agent when they fail.
Limits
- Validation belongs in code: the model cannot check its own arguments reliably.
- A validation error must name the field and the rule, or the agent cannot correct it.
