The Software GardenAgentic Loop Engineering ← Contents
The Software Garden

Agentic Loop Engineering

Don't be scared by the “loop” part. That's the easy bit.
Once you grasp it, it's engineering all the way down.

Scroll ↓
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. Put an agent inside it, 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.

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
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.

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.
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. Making loops faster and cheaper. Trading time and resources for confidence. Agents go round the loops; engineers design them.