← all notes
aiarchitecture3 min read

Guardrails in agent tool loops

March 15, 2026

The loop is the risk

An AI agent is a loop: prompt the model, parse the response, execute the requested tool call, feed the result back, repeat until the model produces a final answer. Every interesting failure mode lives in that loop, and almost none of them are fixed by better prompts.

Three failure shapes dominate:

1. Infinite loops. The model keeps calling tools, each result prompting another call. Without a ceiling, this runs until something else breaks. 2. Invalid tool calls. Arguments that look right but violate the schema: wrong types, missing fields, values out of range. Passed through, they crash handlers or corrupt state. 3. Permission drift. A task that nominally needs read access talks the model into calling a write tool, because the write tool was registered and available.

Step ceiling, outside the model

The step limit has to be enforced by the runner, not requested in the system prompt. "Stop after 10 steps" is a suggestion to a model and a law to a counter. The runner increments a plain integer, compares it to a ceiling, and halts the loop when it is exceeded. The model cannot negotiate with an integer.

Same for wall-clock time. A model stuck in a slow tool call is indistinguishable from a hung process without an external timer.

Validate at dispatch

Every tool declares its argument schema. The dispatcher validates before execution. One enforcement point that every tool call must pass through. Validation inside individual tools is a convention; validation at dispatch is an architecture. Adding a new tool cannot silently skip it.

On failure, return a structured error to the model: which field, what was expected. Models self-correct well when the error is precise.

Permissions are set by the caller

The permission set is chosen at run creation, by the code that knows the task, not by the model, and not per call. A read-only run cannot call write tools because they are not in its dispatch table at all. The strongest permission system is the one that never has to say no, because the option was never presented.

Trace everything

Every step (prompt, model output, tool called, arguments, and result) goes to an append-only log. Not for debugging in production, though that helps. Because a run that cannot be replayed cannot be tested, and an agent loop that cannot be tested is a liability with a nice demo.