The Software GardenAgentic Loop Engineering ← Contents
The Software Garden · Quality · 05

Agentic Loop Engineering

The “loop” is the part everyone gets wrong.
Get it right, and the rest is engineering all the way down.

Scroll ↓
Yes, this heading is click-bait

What everyone gets wrong about loop engineering.

Developers hear “loop” and think of the loops they write: for, while. So they hear “loop engineering” as loop programming: an agent in a while that keeps trying.

It isn't. Loop engineering is building a feedback loop out of three parts: an implementer, an implementation, and an evaluator that measures the implementation and signals the implementer. Nobody writes the loop. It exists because of how the parts are connected, and engineering that system is loop engineering.

We know the heading is click-bait. It's also true: almost everyone starts with the wrong loop in their head, and the wrong loop builds the wrong system.

changes measures signals Implementerthe agent Implementationthe code4 defects Evaluatorthe test run
 

Illustrative. Connect the parts in any order and run it to see what each wiring does.

The right picture

It's a feedback control loop. You already know one: TDD.

In TDD, the developer is the implementer, the code is the implementation, and the test run is the evaluator, feeding back to the implementer. Red means “keep going”. Green means “stop”. A thermostat is the same shape: the heater doesn't decide when the room is warm enough; a thermometer does.

Agentic loop engineering keeps the shape and puts an agent in the implementer's seat. Then the engineering is in the parts and the wiring:

The evaluator has to be independent.An implementer that grades its own work, or can weaken a failing test, stops without doing the work. Tick “The implementer grades its own work” in the demo above to watch it happen. That's why the evaluator sits where the agent can't quietly change it (rules that enforce themselves, zero-eyes review).
The signal says which way.“Red” alone is a retry. “Red: this assertion, in this file” is directions. That error signal is what lets the implementer climb instead of wander.
Feedback loops fail in known ways.Noise: a flaky evaluator makes the implementer chase ghosts (there are no flaky tests). Lag: slow evaluation means fewer corrections (faster, cheaper loops). No convergence: cap the rounds and escalate. Measuring the wrong thing: green, and still wrong (all software fails somewhere).
PartThermostatTDDAgentic
ImplementerThe heaterThe developerThe agent
ImplementationThe roomThe codeThe code
EvaluatorThe thermometerThe test runThe test run, and every other check
TargetThe settingThe testsThe spec, as tests
SignalToo cold / warm enoughRed / greenRed with the failures / green
The loop

TDD is the template.

You have a test. You write code and run the test. If the test fails, you change the code and run it again. When it passes, you're done.

That's the whole loop: the developer implements, the test run evaluates, and the result feeds back. Put an agent in the implementer's seat, and the agent does the going round.

Have a test
Write code
Run the test
Fails → change it
1iteration
Climbing to correctness

A hard validation condition lets the agent climb.

Each time round, the agent gets a clear signal: still failing, and why. It fixes, re-runs and gets closer. It climbs to correctness.

This doesn't mean you have to write the tests. It means you must have correctness conditions the agent can run to make sure its code doesn't ruin the codebase:

  • Local tests that prevent regressions.
  • A type system that enforces contract correctness through the compiler.
failing checks per iterationiteration 1
Loops inside loops

The tight loop sits inside bigger, slower ones.

Putting the correctness conditions in the local loop means the agent can iterate on its own machine, fast and cheap. Once they pass, the work moves out to CI, a longer loop with further guards. After it merges, integration and acceptance testing run. Users are the last loop of all, and the telemetry they generate can feed agents directly (Point them at a number).

Local loopTypes & compiler, unit tests, linters
seconds
CI loopFull suite, module boundary enforcement, builds
minutes
Integration & acceptanceAfter merge: with other systems, against real scenarios
hours
ProductionUsers, monitoring, bug reports, telemetry
days+
 
 
When a defect escapes

Fix the bug. Then fix the loops.

Say a defect slips through the local loop, CI, and integration and acceptance testing, and turns up in a user's bug report.

Of course you fix the defect. But an engineer also examines the engineering of the loops: how could we detect this class of bug as early as possible, in the tightest loop?

Could this class of defect be made impossible by changing the type system? This is the fun part. It's engineering.

🐞
 
Two kinds of feedback

Loops feed back into themselves, and into a loop that tunes the loops.

Loop engineering is designing these loops so they give you confidence in the implementation.

The loop

Fix the code

A failing check feeds back into the same loop: change the code, run it again. Agents do this, many times a minute.

The loop that tunes the loops

Fix the loops

Escaped defects, slow runs and wasted resources feed back into the design of the loops. Engineers do this: better types, better checks, a better place for each check.

–escape rate, last 30 defects
Escaped to users
🤖 Assistant

Pick a pile of escaped defects and I'll analyse where they got through.

The scoreboard

Having the loops is table stakes. Tuning them is the job.

Putting feedback loops in place is where loop engineering starts, not where it ends. Loops that nobody measures drift. The agents ship more, the system grows, and more defects reach users.

The agents write the code. We engineer the loops that determine the quality of the code. Obsessively, and continuously.

So measure them. For every defect, record which loop caught it: local, CI, integration, or a user. Watch how that changes over time. The share that reaches users is the defect escape rate, and it's the score. The game is to keep engineering the loops so it goes down.

To get the data, label each defect with where it was found and, when you can, the change that introduced it. Then every escape tells you which loop to work on next.
LocalCIIntegrationUsersEscape rate
–escape rate
–defects users found
 
Trade-offs

You can't put everything in the inner loop.

You could try, but you'll hit diminishing returns. If the inner loop catches everything but is slow and expensive to run, and 20% of it catches 80% of the issues, maybe the other 80% belongs later, in exchange for velocity.

  • How likely is a defect of this class? What's the impact?
  • How late in the loops can we afford to catch it?
  • What does rework cost at later stages, and how often does it happen?
inner loop
caught locally
loops / hour

Illustrative numbers. The checks in purple still run, just later (in CI or after merge), where an escape costs more to fix.

Faster, cheaper loops

Speed and cost are engineering problems too.

Faster loops that use fewer resources mean more of them can run at once, and each one finishes sooner. With many agents working in parallel, that multiplies.

Teams have switched to faster-compiling languages to speed up the inner loop, and to languages with more expressive type systems to get stronger guarantees.

A different testing strategyProperty tests, contract tests, test impact analysis: run only what this change can break.
A different tool setFaster test runners, incremental builds, caching, parallelism.
A stronger type systemMove whole classes of checks from tests into the compiler, the tightest loop there is.
A different languageFaster compiles for a quicker inner loop, or more expressive types for stronger guarantees.
The shape of the code

The import graph decides how much of the suite a change runs.

Test impact analysis runs the tests whose imports reach the changed file. It can only be as narrow as the import graph. A barrel file, an index that re-exports a whole folder, connects every module in that folder to every test that imports any of them.

In one project, editing a single store selected 966 of about 1,380 test files, because every test reached every store through the barrel. With each store imported from its own module, the same edit selects 64. Removing the services barrel takes an edit to one service from 944 test files to 199.

Cutting imports can change the order modules load in. The project’s first attempt failed on start; that crash became a lint rule (chapter 11, Rules that enforce themselves).

Edit
Imports
test files run
of the suite
skipped

Measured with Jest --findRelatedTests in that project. One square is about ten test files.

So

Once you understand the loop, it's engineering all the way down.

Designing the conditions. Placing each check in the right loop. Pushing detection inward. Measuring where every defect is caught, and tuning the loops until fewer escape. Making loops faster and cheaper, down to the shape of the import graph. Trading time and resources for confidence. Agents go round the loops; engineers design them.