Agent Loops Explained: Goal, Act, Verify, Stop
In June 2026, Business Insider wrote that 'loop engineering' was replacing prompt engineering. The idea is simple. You stop typing each prompt. You design a loop that prompts the agent, checks its work and decides when it is done. The hard part is the last two steps. An agent that cannot verify its work says 'done' too early, and a loop with no stop condition runs up a bill. This guide explains the four parts and the stop settings in each tool.
Bottom Line
- Every agent is a loop: goal, act, verify, stop. The model is the same in a good agent and a bad one. The loop is not.
- Verify is the weak step. Teams with heavy AI use merged 98% more pull requests, but review time rose 91%.
- “Done” is a claim, not a proof. Make the agent show a check that passed, such as a test command and its output.
- Every loop needs a stop condition: a turn limit, a time limit, a measurable end state, or a cost cap. Better, all four.
- OpenClaw limits runs by time, not by turns. Lower the 48-hour default and turn on loop detection for small models.
The Four Parts
| Part | Question | Example |
|---|---|---|
| Goal | What does “done” look like? | ”All tests in auth/ pass and git status is clean” |
| Act | What can the agent do? | Edit files, run commands, call tools |
| Verify | How does it check its work? | Run the tests, read the output, compare with the goal |
| Stop | When does the loop end? | Goal met, 20 turns used, 30 minutes passed, or $5 spent |
Anthropic’s “Building effective agents” (December 2024) gives the base pattern. Agents are “typically just LLMs using tools based on environmental feedback in a loop.” The same post says “it’s also common to include stopping conditions (such as a maximum number of iterations)”.
The two parts people skip are Verify and Stop. Without Verify, the agent stops when it thinks it is done. Without Stop, it never does.
What “Loop Engineering” Means
Business Insider’s article (20 June 2026) quoted several builders. Boris Cherny, who created Claude Code, said “I don’t write the prompt anymore.” Peter Steinberger, who created OpenClaw, said “You should be designing loops that prompt your agents.”
Addy Osmani gives the clearest definition, in his own post from 7 June 2026: “Loop engineering is replacing yourself as the person who prompts the agent.” He lists the parts: scheduled runs, worktrees, skills, connectors, subagents that check the work, and state kept outside the conversation. He also warns: “Verification is still on you.”
In the same Business Insider piece, Osmani put the checking problem in one line: “The model that wrote the code is way too nice grading its own homework.”
Why Verify Is the Bottleneck
| Source | Finding |
|---|---|
| Faros AI (July 2025), over 10,000 developers | High-AI teams merged 98% more pull requests. Review time rose 91% |
| arXiv study (July 2026), 1.02 million pull requests | Agent review made decisions faster but not better |
| Stack Overflow survey 2025 | 66% said AI answers are “almost right, but not quite”. 32.7% trust AI accuracy |
| Stack Overflow survey 2026 | 48% trust AI when they can easily check the answer |
Agents made code cheap. Checking it is now the expensive part. The fix is to build the check into the loop, so the agent runs it before it says “done”.
Good checks are deterministic: a test suite, a type check, a build, a linter, a curl that returns 200. A second model that reads the diff is weaker, but better than nothing.
If your local model says it ran a tool and did not, read your local model says it did it, but it didn’t.
Stop Conditions in Each Tool
OpenAI Agents SDK
The loop ends when the model returns a final answer with no tool calls. A turn limit protects you: max_turns defaults to 10, and the SDK raises MaxTurnsExceeded when it is reached. max_turns=None removes the limit.
Claude Code /goal
You give a condition. After each turn, a separate model checks it. The goal clears when the condition is met, when the task is judged impossible, or on an error you must fix. The docs say completion is “decided by a fresh model rather than the one doing the work.”
The checker does not run commands or read files itself. So write a condition the transcript can prove:
/goal All tests pass: `npm test` exits 0 and the output shows 0 failures. Do not edit test files. Or stop after 20 turns.
The docs recommend one measurable end state, a stated check, and constraints. The last clause is a turn limit you add yourself.
Claude Code Stop hooks
A Stop hook runs when Claude finishes a response. It can block the stop with exit code 2 and a reason, for example “the tests have not run”. Two built-in protections:
stop_hook_activeis true when Claude is already continuing because of a stop hook. Check it, so your hook does not block forever on a condition that cannot be met.- A hard cap of 8 forced continuations in a row. Then Claude Code ends the turn anyway.
Claude Code /loop
/loop 5m <prompt> runs on a fixed interval. With no interval, Claude picks its own pace between 1 minute and 1 hour. Recurring tasks expire after 7 days, which limits how long a forgotten loop can run.
Codex /goal
Codex CLI also has /goal to set, pause, resume or clear a task goal, up to 4,000 characters.
The Ralph loop: what no stop condition looks like
Geoffrey Huntley’s “Ralph” (July 2025) is the simplest agent loop there is: “In its purest form, Ralph is a Bash loop.”
while :; do cat PROMPT.md | claude-code ; done
It runs until you stop it. It is useful for long build jobs that you watch, but it has no Verify step and no Stop step. Add both before you leave it alone.
OpenClaw: How Its Loop Stops
OpenClaw limits runs by time and by repeated errors. We found no general turn-count limit for the main agent loop in the docs.
| Setting | Default | What it does |
|---|---|---|
agents.defaults.timeoutSeconds | 172800 (48 hours) | Time budget per model attempt. Each fallback model gets a new budget. 0 means no limit |
| Repeated-error guard | Always on | After 3 identical tool errors in a row, OpenClaw blocks that call and allows one recovery |
tools.loopDetection.enabled | false | Turns on detectors for repeated patterns and retries of unknown tools |
| Automation (cron) jobs | 60-minute watchdog | Set --timeout-seconds per job |
| Script payloads | 300 s, 50 tool calls | --script-timeout-seconds, --script-tool-budget |
channels.defaults.botLoopProtection | On, 20 events per 60 s | Stops two bots from answering each other forever |
The OpenClaw docs give two direct pieces of advice:
- “For smaller models, set
enabled: true” for loop detection. - “A completed model run is not proof that the requested task succeeded.” That is the Verify step again.
A tighter setup for a local model:
{
agents: { defaults: { timeoutSeconds: 1800 } }, // 30 minutes instead of 48 hours
tools: { loopDetection: { enabled: true } },
}
To stop a run now, send a message such as stop, abort or halt in the chat. OpenClaw also has /loop for recurring agent turns, with /loop status and /loop stop.
OpenClaw’s old heartbeat feature is retired. Recurring checks now run as normal automation jobs, and openclaw doctor --fix converts old heartbeat setups.
What a Missing Stop Condition Costs
InfoQ (July 2026) reported an agent with full AWS access that was asked to scan a hobby network. It started five large EC2 instances plus load balancers and Lambda functions, and kept reapplying its template. The bill was $6,531.30, which AWS reduced to $1,894. The owner noticed about a day later.
Research also shows that stop controls fail more often than people think. A July 2026 paper, “Stop Means Stop”, tested approval gates, cancellation and timeouts in six popular open-source agent frameworks. The contract held in none of them.
Put the hard limits outside the agent: a cloud budget alert, an API key with a spending cap, and an IAM role that cannot start large instances. An agent cannot talk its way past a limit it cannot reach.
A Loop Checklist
- Write the goal as a check. “
npm testexits 0”, not “fix the tests”. - Give the agent a way to run that check. No test command means no Verify.
- Separate the checker from the worker. Use a test, a
/goalchecker or a reviewer subagent. - Set a turn or time limit. In OpenClaw, lower
timeoutSeconds. - Set a cost limit outside the agent. A key or account cap.
- Turn on loop detection for small local models.
- Keep state on disk. A progress file lets a new session continue, as our context rot guide explains.
Honest Caveats
- The productivity data is mixed. METR’s 2025 study found developers 19% slower with AI. Its February 2026 follow-up showed a weak speedup and called its own data an unreliable signal.
- We found no turn limit for OpenClaw’s main loop. That is our reading of the docs. Check your version.
- The Business Insider article was read through its Yahoo Tech syndication.
- The AWS incident comes from InfoQ’s report, not the owner’s original post.
Sources
- Anthropic: Building effective agents (December 2024)
- Business Insider via Yahoo Tech: Forget prompt engineering, ‘loop engineering’ is all the rage now (20 June 2026)
- Addy Osmani: Loop engineering (7 June 2026)
- OpenAI Agents SDK: running agents
- Claude Code docs: /goal, hooks, scheduled tasks
- Codex CLI slash commands
- Geoffrey Huntley: Ralph (July 2025)
- LoopsBench: From Harness Engineering to Loop Engineering (July 2026)
- Faros AI: The AI productivity paradox (July 2025)
- Agentic code review study (July 2026)
- Stack Overflow Developer Survey 2025: AI and 2026: AI
- Stop Means Stop (July 2026)
- InfoQ: AI agents and billing guardrails (July 2026)
- METR: 2025 developer study and February 2026 update
- OpenClaw docs: agent loop, loop detection, cron jobs, bot loop protection, heartbeat
See Also
- Harness engineering explained: the loop is one part of the harness
- Context rot explained: why long loops get worse
- Pass rate vs completion rate explained: how “done” is measured
- Harness tests explained: test the hooks that run your stop checks