2026-06-12
The moment an agent can take actions in the world — sending emails, writing files, calling APIs — the question stops being "can it do the task?" and starts being "does it know when to stop?"
Agent loop — the cycle where a model takes an action, observes the result, then decides the next action. Repeats until something stops it.
Exit condition — the rule that ends the loop. Could be a goal being met, an error threshold, or a step count.
Tool call — a structured request the model makes to run code, hit an API, or read a file. Each loop iteration usually involves one or more.
The Sorcerer's Apprentice problem: the apprentice enchants the broom to fetch water. The broom does it perfectly. Then keeps doing it. The bucket overflows. The room floods. The broom never had a stop condition.
Agents fail this way constantly. You ask Claude to fix a test suite. It runs the tests, finds another failing test, fixes that, finds another, fixes that — 47 tool calls later it has refactored half your codebase. The task was never unclear. The exit condition was.
The dangerous thing is that this looks like diligence. The agent is doing exactly what you'd do — keeping going until everything is clean. But it has no sense of scope, cost, or "this is getting weird."
Every agent loop needs four things designed before you build:
In Claude Code, you can enforce a step cap explicitly in your system prompt:
You are working autonomously on this task.
Rules:
- Complete the task in 10 tool calls or fewer.
- If you cannot finish within that limit, stop and summarise what you have done and what remains.
- If you see the same error twice in a row, stop and ask me.
In an n8n agentic workflow, add a counter node that increments on each loop iteration and routes to an "alert and stop" branch once it exceeds your threshold. Don't rely on the agent to count itself.
Design explicit exit conditions for any agent that: loops, retries, or can trigger its own next step. That covers almost every useful agentic workflow.
You don't need to think this hard about a single-shot prompt that returns text. The risk only appears when the agent has the ability to keep acting.
Pick one agentic workflow you've already built — in Claude Code or n8n. Ask yourself: what is the exit condition, and is it enforced by the system or just assumed from the prompt? If the answer is "assumed", add a hard step cap today and log how many steps the workflow actually takes over the next week.
Don't miss what's next. Subscribe to My Claude Daily Learning: