Article ยท Agents and tool calling
How I make LLM tool calls safe for a real backend
Five rules: schemas as contracts, validation before anything runs, narrow tools, idempotent writes and logs for every call.
By Ali Raza, Senior AI Engineer
An agent with tools is a new kind of caller for your backend: one that sometimes improvises. These are the five rules I apply before it touches real data.
1. The schema is the contract, not the prompt
Define every tool's input as a Pydantic model. The prompt can describe the tool, but the schema decides what is accepted.
2. Validate before anything runs
Check the model's arguments before any side effect. An invalid call returns an error to the model, which can correct itself. It never results in a partial write.
3. Keep tools narrow
"create_lead" beats "run_crm_action". A narrow tool is easier for the model to use correctly, easier to test, and limits the damage of a wrong call.
4. Make writes idempotent
Every write carries a request key, and a write with a key that already exists is not repeated. A retried call, a repeated user, or a timeout followed by a retry cannot create two bookings.
5. Log every call
Inputs, outputs, latency and cost for every tool call. You will need it the first time someone asks "why did it do that?"
None of this is AI-specific. It is backend engineering applied to a caller that sometimes improvises, and it is the difference between a demo and a system you can leave running.