Best Practices for Building LLM Apps

Chapter 4 · Practice 2 of 5

Validate tool arguments in code before executing them

From Tools, agents and least privilege

Why it matters

The model fills in tool arguments, so treat them like form input from an unknown user. Before any tool runs, your code checks:

  • Types and ranges against the tool schema.
  • Ownership: referenced ids belong to the current user.
  • Caps on amounts, counts and recipients.

When validation fails, return a clear, short error message to the model so it can correct itself, and log the rejected call.

A sudden rise in rejected tool calls is often the first sign of an injection attempt or a prompt regression. Put it on a dashboard.