Loop engineering: give an AI agent a way to check its work

Published on
•1 read

I don’t expect an AI coding tool to get every change right on its first try. I do expect it to notice when something is wrong.

That is the useful idea behind loop engineering: give the tool a task, let it make a small change, check the result, and feed that result into its next attempt. Stop when the work meets the requirements, or when continuing needs a person’s decision.

The name is newer than the habit. Developers have been working this way with tests, error messages and reviews for years. With an AI agent, I want those checks to be part of the process rather than something a person remembers to do afterward.

A small example

Suppose I want a function that turns a heading into the last part of a web address. Hello world! should become hello-world. That readable part of the address is often called a slug.

This first version looks plausible:

def slugify(text):
    return text.lower().replace(" ", "-")

assert slugify("Hello world!") == "hello-world"

The check fails. The function returns hello-world!, including the exclamation mark. An assert checks whether a condition is true and raises an error when it is not.

Without that check, the agent could hand back the function and announce that the job is done. In a useful loop, the failed check becomes information for the next change.

A small revision handles that example and repeated spaces:

def slugify(text):
    words = text.lower().replace("!", "").split()
    return "-".join(words)

assert slugify("Hello world!") == "hello-world"
assert slugify("  Two   spaces  ") == "two-spaces"

This removes the exclamation mark, breaks the text into words, then joins those words with hyphens. Both example checks pass.

It is deliberately limited. It does not handle every punctuation mark, empty input or every rule a real website might need. I would not mistake two passing checks for a finished URL library. That limitation is part of the lesson: the loop can only check the behaviour we have defined.

Give the next attempt useful information

“Try again” tells the agent very little. A useful failure report says what was expected, what happened, and which input exposed the difference.

For a website, that might mean opening the actual page and clicking the button. An API returning a successful response does not prove the button shows the result. For a data change, it might mean reading the saved record back instead of trusting the message that said it was saved.

I would start with the smallest check that reproduces the problem, then run wider checks once the fix works. That keeps each change easier to understand.

Decide when to stop

A loop should not mean letting an agent retry forever. I would set a limit on attempts or spending, preserve the working version, and stop if the same failure keeps returning without new information.

A failed test is a reason to investigate, not permission to delete the test. Missing credentials, unclear requirements and risky actions need a person’s decision. Code from an agent should run in a restricted test environment, not with unrestricted access to a live system.

My starting point would be one task, a clear example of success, a way to inspect the result, and a stop rule. More tools can come later. First, I want the agent to respond to evidence rather than merely sound confident.

Sources

Published by Letterpad Newsroom. Daniel Weber is an editorial byline.

Author
Daniel Weber
Daniel Weber

Daniel Weber is an editorial byline of Letterpad Newsroom.

Discussion (0)

Subscribe