On this page
Loop engineering is designing the automated cycle that decides what a coding agent does next, checks what it did, and decides when to stop, so a person no longer prompts the agent turn by turn. The engineer writes the loop (trigger, prompt, checks, state, limits) and the agent runs inside it, often overnight and with no one watching.
Part of the AI Context & MCP guide.
What is loop engineering?
Loop engineering moves the engineer's job from writing prompts to building the system that writes and judges them. Addy Osmani's essay Loop Engineering (June 2026) puts it as "replacing yourself as the person who prompts the agent". He quotes Peter Steinberger: "You shouldn't be prompting coding agents anymore. You should be designing loops that prompt your agents."
The term was popularized in June 2026 by Steinberger's post and Osmani's essay. Who said it first is not settled, and the loop idea itself has an obvious ancestor in Geoffrey Huntley's Ralph technique from 2025.
IBM's explainer (July 2026) describes the cycle as goal, action, observation, adjustment. The agent works toward a goal, acts, observes the result, and adjusts. The loop only works if it has a verifiable stopping criterion, because otherwise the agent decides for itself that it is done.
Control loops are old, and the name is borrowed from them. What changed is that the thing inside the loop now writes code, and code that looks right is cheap to produce and expensive to trust. So in loop engineering the verification step is the whole game.
Loop engineering vs prompt, context and harness engineering
Each term names a different unit of design: one turn, one window, the controls around the agent, or the cycle that runs it.
| Term | Unit of design | Question it answers |
|---|---|---|
| Prompt engineering | One instruction | How do I word this request? |
| Context engineering | The context window, per call | What should the model see right now? |
| Harness engineering | The controls around the agent | What guides and checks surround it? |
| Loop engineering | The cycle across many runs | What happens next, and when does it stop? |
Scroll the table sideways to see every column.
They nest: a loop calls an agent, which runs inside a harness. For the inner layers, read what context engineering is and what harness engineering is. This post is about the outermost layer.
The simplest loop: Ralph
The Ralph loop is loop engineering at its smallest: the same prompt fed to an agent again and again until the work is done. Huntley's original from July 2025 is a one-line shell loop. Each pass picks one task, does it, and exits, and tests act as the backpressure that rejects bad work.
Ralph has a trigger (immediately), a prompt, a state file, and checks only if you add them. Loop engineering is the general case: the same parts, designed on purpose, with a stop condition you chose. If you want the details of Ralph itself, they are in the Ralph loop post.
The four parts of an agent loop
Every agent loop has four parts: a trigger that starts a cycle, verification that judges it, state that carries over, and stop conditions that end it.
| Part | What it decides | Typical choices |
|---|---|---|
| Trigger and cadence | When a cycle starts | Manual, cron, GitHub Actions, an event hook |
| Verification | Whether the cycle's work is acceptable | Tests, linters, CI, a separate reviewer agent with fresh context |
| State and memory | What the next cycle knows | A progress file, a task list, git history |
| Stop conditions and guardrails | When the loop ends or pauses | Completion criteria, iteration cap, budget cap, human approval gate |
Scroll the table sideways to see every column.
IBM adds that a separate verifier usually beats an agent checking its own work.
A worked example: a nightly loop that fixes failing tests
Design one loop end to end and the choices become concrete. Here is a hypothetical nightly loop that works through the failing tests on a service repository and opens a pull request in the morning.
Trigger. A scheduled GitHub Actions workflow at 02:00, or a cron entry on a build machine. Manual runs use the same script, so you can test it by hand first.
Prompt. Short and fixed. Read PROGRESS.md, run the test suite, pick the single most important failing test, fix its cause, run the suite again, append what you learned to PROGRESS.md, stop. One task per cycle keeps each diff reviewable.
Verification. Three layers, cheapest first. The test suite must pass for the targeted test with no new failures. A linter and type check must stay clean. Then a second agent with a fresh context reviews the diff against the task and is told it may reject. An agent that wrote the change will tend to approve it.
State file. PROGRESS.md, append-only, plus git history. Each cycle starts with a fresh context and reads this file to catch up.
Stop condition. The loop ends when the suite is green, or after 12 cycles, whichever comes first. The green suite is an objective signal. The cap is the backstop for the night the agent keeps changing the same test.
Budget. A hard token or dollar cap per night, enforced outside the agent. If the cap trips, the loop writes its last state to PROGRESS.md and exits without opening a pull request.
Human gate. The loop never merges. It opens a draft pull request, and a person reads it.
Where loops fail on a real codebase
Loops fail in two places: the agent starts each cycle with the wrong context, and the check that should catch a bad change is too weak. A loop repeats both without anyone watching.
On context, a fresh-context cycle means the agent rediscovers the repository every time. If it does that with grep and whole-file reads, each of the twelve cycles pays the exploration cost again, and each can miss the caller in the module it never searched. On verification, "tests pass" only proves what the tests cover. A change can pass and still add a deeply nested function to a file with a long record of bug fixes.
This is the codebase layer, and repowise supplies it. It plugs into the context and verification parts of whatever loop you build.
Where repowise fits each part of the loop
The verify step decides whether a loop converges, and repowise checks the diff as well as the tests. With no arguments, get_change_risk() reviews the uncommitted diff and reports what the change made worse and which tests cover the changed lines, so a verifier can send the cycle back when those tests never ran.
get_change_risk()
Context at the start of each cycle, the hooks that push it and the opt-in health gate are covered in harness engineering. For noisy test output, repowise distill <cmd> returns a compact result, so one long log does not fill the window of every cycle after it.
How to start
Build the verification first, run it by hand a few times, then wrap a trigger and a cap around it. When the loop fails in a way you did not predict, fix the check so it cannot repeat. The agents page has the setup for the MCP tools and hooks above.
FAQ
- What is loop engineering in AI?
- Loop engineering is designing the automated cycle that prompts a coding agent, verifies its work and decides when to stop, with little human input. The engineer builds the trigger, checks, state and limits, and the agent runs inside them. IBM describes the cycle as goal, action, observation and adjustment.
- Loop engineering vs prompt engineering?
- Prompt engineering writes one good instruction for one turn. Loop engineering designs the system that generates and judges instructions across many turns, including when to run, how to verify and when to end. Context engineering and harness engineering sit between them, covering what the model sees and what controls surround the agent.
- Is loop engineering the same as a Ralph loop?
- A Ralph loop is the simplest form of it: the same prompt fed to an agent repeatedly until the work is complete. Loop engineering is the broader practice of designing that cycle on purpose, with a chosen trigger, an independent verification step, a state file and objective stop conditions.
- What are the components of an agent loop?
- An agent loop has four components. A trigger starts a cycle (manual, cron, GitHub Actions or an event hook). Verification judges the result with tests, linters, CI or a separate reviewer agent. State carries what was learned to the next cycle. Stop conditions and guardrails end the loop through completion criteria, iteration limits, budget caps and human approval gates.
- How do you stop an agent loop from burning tokens?
- Set an iteration cap, a hard budget cap enforced outside the agent, and an objective stop condition such as a passing test suite. Give each cycle one task so a stuck cycle is easy to spot, and exit with a written state file when a cap trips. Keeping test and build output compact, for example with
repowise distill, also cuts the tokens each cycle spends.



