Found something good?

Save it before you doomscroll past it.

Loop Engineering: how a system improves itself

Loop Engineering: how a system improves itself

They sell you a prompt. One perfect prompt for money. The secret is that the perfect prompt does not exist. What exists is a loop that finds a good prompt on its own while you sleep.

Loop engineering is not about writing the right instruction on the first try. It is about building a loop that makes an attempt, looks at the result, understands what went wrong, and rewrites itself. Round and round until the metric stops climbing.

The whole idea in one sentence: you do not engineer the answer, you engineer the loop that arrives at the answer.

The core of every loop: four steps

The loop is always the same under any sauce. Four steps that repeat until the result is good enough.

Generate: the system makes an attempt. Writes a prompt, code, an answer, a strategy.

Evaluate: the result runs through a metric. A test, a score, a compiler, a judge. The point is the evaluation is automatic and numeric.

Reflect: the model looks at the failures and states in words why it failed. Not just "score 0.4", but text like "it missed the empty-input check here".

Mutate: based on the reflection the system rewrites its attempt, and the loop starts again.

the four steps that repeat until the metric stops climbing
the four steps that repeat until the metric stops climbing

Why this works and prompt engineering does not

Manual prompting is you guessing. You try a phrasing, look with your eyes, change a word, look again. You are a slow evaluator, and you get tired.

The loop does the same thing a thousand times a night and never tires. And the key difference is that it has memory. Every attempt is saved to an archive with its score, and the next mutation starts not from scratch but from the best so far.

This is the part people skip. Without the archive it is not a self-improving loop, it is just a retry. The archive of past attempts and their results is what turns random search into directed evolution.

without the archive it is a retry; with it, the next mutation starts from the best result, not from zero
without the archive it is a retry; with it, the next mutation starts from the best result, not from zero

How the metric climbs in practice

Look at a real run and the individual attempts jump up and down, that is noise. But the "best in archive" line climbs monotonically, because the archive remembers the good finds and does not let them slip back. The loop stops when the target is reached or the iteration budget runs out.

individual attempts are noisy, but the best-so-far from the archive pulls quality up to the target threshold
individual attempts are noisy, but the best-so-far from the archive pulls quality up to the target threshold

This mechanism is what modern systems run on. ShinkaEvolve from Sakana AI reaches a new state-of-the-art on the circle-packing task in roughly 150 evaluations where prior systems burned thousands, thanks to the archive and smart parent sampling. GEPA lifts accuracy on math benchmarks just by reflecting on failures, and on some tasks turns out several times more sample-efficient than reinforcement learning.

The Thinker · Worker · Verifier pattern

When one model in a loop hits a wall, the roles get split across three agents.

Thinker breaks the task into steps and plans. Worker executes a concrete step, generates output. Verifier checks quality and decides: accept or send back for a redo.

It is the same generate → evaluate → reflect, but the roles are explicit, and the verifier is a separate agent rather than just a test. Useful where evaluation itself needs reasoning. The downside is three agents cost three times more, so the verifier usually runs on a cheap model and the thinker on an expensive one.

thinker plans, worker executes, verifier accepts or sends back for a redo
thinker plans, worker executes, verifier accepts or sends back for a redo

The main trap: verifiable outcome

The loop improves exactly what you measure. If the metric is off, the system gets better at the off metric and worse in reality. This is called goodharting: the proxy metric climbs while the product degrades.

after the divergence point the proxy metric keeps climbing while real quality falls
after the divergence point the proxy metric keeps climbing while real quality falls

Hence the iron rule: loop engineering only works where the outcome is verifiable automatically and honestly. Code passes tests, yes or no. A math problem has a right answer, yes or no. A SQL query ran in so many milliseconds. Those are good loops.

Where there is no honest automatic evaluation (like "do people enjoy the text"), the loop will optimize the judge and not reality. So either build an honest evaluator, or keep a human in the loop on the final step. Recent work handles this by anchoring the loop to a specification and a regression oracle rather than a scalar metric, then the loop converges toward the spec instead of over-optimizing a proxy.

A minimal loop in Python

No framework, pure idea. Claude generates, the test evaluates, Claude reflects, repeat.

Three things turn a retry into a real loop: memory is fed back into generation, the best is stored separately, and there is a stop condition on both target and iteration count. Without the last one the loop spins forever and burns money.

Where this actually gets used

Code. An agent writes a function, runs tests, reads the traceback, fixes it. SICA (Self-Improving Coding Agent) edits its own codebase and lifts its own SWE-bench score.

Prompts. Instead of hand-tuning you run prompt evolution against a dataset. GEPA reflects on failures and improves the prompt iteratively.

Algorithms. AlphaEvolve from DeepMind and its open counterparts evolve source code using execution as the fitness function, and find solutions better than human ones.

Data engineering. The loop optimizes SQL and pipelines using the query plan and runtime as the metric.

Repositories (all open)

Take it and run it, there is no one to pay. Links verified at time of writing, still check the stars, license, and last-commit date before you depend on anything.

SakanaAI/ShinkaEvolve: program evolution with an archive, parallel evaluation, and smart parent sampling; sample-efficient, Apache-2.0. Has skills to run inside Claude Code and Codex. https://github.com/SakanaAI/ShinkaEvolve

gepa-ai/gepa: reflective evolution of prompts and code: optimizes any textual parameter against any metric via natural-language feedback. Adapters for DSPy, RAG, MCP. https://github.com/gepa-ai/gepa

MaximeRobeyns/self_improving_coding_agent: SICA, an agent that edits its own codebase. A genre classic. https://github.com/MaximeRobeyns/self_improving_coding_agent

algorithmicsuperintelligence/openevolve: an open implementation of AlphaEvolve: code evolution through a MAP-Elites archive and a cascade evaluator. https://github.com/algorithmicsuperintelligence/openevolve

ShengranHu/ADAS: automated design of agentic systems: a meta-agent that invents agent architectures by programming them in code. https://github.com/ShengranHu/ADAS

XMUDeepLIT/Awesome-Self-Evolving-Agents: a large curated survey with papers, benchmarks, and code on the topic. A good place to dig further. https://github.com/XMUDeepLIT/Awesome-Self-Evolving-Agents

For the basics, read Reflexion (language agents with verbal reinforcement learning) and DSPy as a framework where loop optimization is built in (the GEPA optimizer ships as a teleprompter).

The main idea

Loop engineering is a shift from "you write the answer" to "you build the machine that finds the answer". Four steps, generate, evaluate, reflect, mutate, plus an archive and an honest metric.

All the difficulty is not magic but two things nobody sells in a course: build an honest evaluator, and do not let the loop optimize the proxy instead of reality.

The prompt is sold for money. The loop that finds the best prompt on its own sits on GitHub for free.

Article by @vorty279, content maker in the AI space. Verify figures and model versions against primary sources before publishing, this field moves fast.

X Article
434059.1K
Reading tools
Keep it forever

Create a free account to save everything you preview — private to you.

Preview another link

Works with X, Instagram, TikTok & YouTube.

One place for everything
Tweets, TikToks, Reels, Shorts & articles in one searchable home.
Media at your fingertips
Full-screen viewer for photos and video — save any post to your collection.
Actually find it later
Full-text search across everything you save.

More to discover

More from @vorty279