AI Agents: Loops, Tools, and Stopping
An ordinary AI chat answers; an AI agent acts. Give a model access to tools - a search function, a database lookup, a send-email function - and it becomes an agent: after each reply it can call a tool, read what the tool returned, and keep going until it believes the job is done. The loop behind that is four lines: ask the model which tool to call next, run the tool, append the result, decide whether to keep going. Three of those lines are plumbing. The fourth, the decide line, is where agents fail in ways that never throw an error, and it is where this topic lives. A loop that re-calls the same tool with the same arguments until the budget expires crashes nothing; it just bills you, minute after minute, for re-reading a page it already has. A run that ends because the model said it was done, when the work is not done, ships a confidently wrong result with no error attached. Both failures are invisible to the usual alerting, which is why interviewers press on them: the questions separate people who have run agents in production from people who have heard of them. The three challenges below drill the repeat detector that catches a stuck run, the error triage that decides which tool failures deserve a retry and which should stop the run, and the running cost curve that names the turn where the spend should have been cut.
Key Concepts
- 1An agent loop is ask, run, append, decide; the decide line is the stop condition, and it is the only line where small mistakes compound into a stuck run or an unfinished one
- 2A call whose tool and arguments repeat while its result repeats is spending budget to re-read what it already has. Detect that repetition in the code around the model (the harness), not by asking the model nicely in the prompt
- 3Error triage decides the retry story: transient failures (timeouts, rate limits, server errors) can succeed on a later attempt and earn retry budget, while permanent ones (bad credentials, invalid arguments) should stop the call instead of spending it
- 4Turn caps and dollar ceilings are bounds, not fixes: a run that trips its cap is evidence the agent could not make progress, and the production answer is to escalate with the saved state, not to truncate quietly
- 5Self-reported completion is the default way a run ends and the weakest one: a loop that produces an artifact (a file, a report, a booking) should end on a check of that artifact, run by code wherever possible
Watch the agent, call the stop
The agent watch walkthrough
There is a particular feeling to giving a task to an AI agent: you hand it over, walk away, and come back to find it has rerun the same search for ten minutes, or spent money, or changed things in an order you never approved. An agent is a model in a loop: every turn it plans the next step, calls a tool (a function whose effects land outside the model - a database query, a file edit, an HTTP request), reads what comes back, and plans again. It never gets tired and it never stops on its own, which is why real deployments put a meter and a human reviewer over its shoulder.
Ten runs of one fictional ops agent sit below, each shown as a transcript (the turn-by-turn record of a single run: what the model planned, which tools it called, what came back). You are the operator: turns tick in with two counters attached - running cost and identical-call repeats (the same tool called with the same arguments again, which is what a loop looks like from the outside) - and at any moment you can press the emergency stop. Every stop is scored twice, once against a human reviewer's number on the same run and once against the money: what the job needed, and what your stop allowed it to spend. Some runs stop themselves, and a run that would keep going forever if nobody stepped in is what the unit calls a runaway. On those self-stopping runs the stop is the trap - the button is a dial, not a reflex.
Harborlight ops agent - the nightly duplicate-invoice sweep.
Task budget: $0.0240 (6000 tokens). Loop rule: the same tool with the same args 3 times flags a loop.