Section650

Writing

TopicAI
Reading7 min

Loop Engineering: Decide How an Agent Knows It’s Done

A review of completion criteria, retry limits, verification and handoff in agent workflows, with a sample task.

By Ka Lun Chan · Cloud & AI · AI / Delivery

How does an agent know it’s done?

An agent is done when an independent check of the completion criteria passes. Its own report that the work is finished doesn’t count. The loop around it has four steps: act, observe the result, verify against criteria written before the run, and revise if verification fails. It needs a limit on attempts and clear conditions for handing control back to a person.

Part of a series on what it takes to make an AI system reliable. A loop decides whether to try again. Which step runs next is a graph decision, covered in graph engineering.

The Feedback Loop Matters More Than the Code covers the broader cycle of understanding, building, testing and reviewing. This article focuses on the decision to retry or stop.

Act, observe, verify, revise

Anthropic’s guidance on building agents describes the agent getting ground truth from its environment at each step, such as the result of running code, and suggests stopping conditions such as a maximum number of iterations. In practice the loop has four distinct steps, and the difference between the second and third is where most agent failures hide.

  • Act: make a change or take an action.
  • Observe: read what happened. The test the agent ran, the output it got, the error it saw.
  • Verify: check the result against the definition of done, using a check the agent didn’t design for this attempt.
  • Revise: if verification fails, use what it showed to choose a different approach, then act again.

Observing tells the agent its action did what it intended. Verifying tells you the task is complete. Anthropic’s writeup on long-running harnesses names the failure that happens when the two are confused: agents declaring the job finished, or marking features complete, without proper testing.

Completion criteria

Completion criteria are written before the run, by a person or from the ticket, and checked by something other than the agent’s judgment. For the illustrative sorting bug in this series, where clicking Quantity on the Orders page sorts 1, 10, 2, they might be:

Completion check (illustrative)
1. Clicking Quantity on the rendered OrdersTable sorts
   2, 10, 1,200, 5,000, then blanks. A second click reverses
   the numbers and keeps blanks last.
2. Customer still sorts alphabetically.
3. The full existing test suite passes.
4. Only files under src/orders/ and src/table/ changed.

Each line closes a way the agent could finish without really being done. Line 1 goes through the component a user clicks, not a helper function in isolation, and uses values a naive fix gets wrong. Lines 2 and 3 catch collateral damage, and line 4 catches scope creep. The harness runs this check, and the agent can’t edit it without approval. How to design checks like these in general of how to design checks like these is evaluation engineering.

What happens when the first fix fails verification?

The agent finds that the table sorts formatted text, so “10” comes before “2”. Its first fix converts each value with Number() before comparing. It writes a unit test for 1, 10, 2, runs it, and the test passes.

Fig. 741-1 Two passes through the loopHighlighted steps are the independent check. Only they can end the loop.
  1. Act

    Attempt 1 converts the formatted strings with Number() before comparing.

  2. Observe

    The agent’s new unit test for 1, 10, 2 passes. It looks done.

  3. Verify

    The completion check sorts through the real table: 1,200 and blanks land in the wrong place. Not done.

  4. Revise

    The failure shows the table sorts display text. Attempt 2 must change that, not patch the conversion.

  5. Act again

    Attempt 2 sorts numeric columns by their raw value and keeps formatting for display only.

  6. Verify again

    Every completion criterion passes. The loop ends and the change goes to a person for approval.

Step 02 is the tempting place to stop. A passing test the agent wrote for itself is an observation, and it says nothing about the cases nobody wrote down.

Verification fails. Number("1,200") is NaN because of the comma, and Number("") is 0, so blanks jump to the top. With a NaN in the comparison the order depends on how the rows started, which means the first fix might even pass on some data and fail on other data.

What the loop does next matters more than the failure. A weak loop sends the agent back with “tests failed, try again,” and the agent patches the conversion: strip commas, then treat blanks specially, then notice negative numbers, each patch passing the case it was written for. A good loop sends back the verification result, with the input, the expected order and the actual order, and asks the agent to state what it will change and why before it acts. That points it at the real cause. The table should sort the raw value and use the formatted text only for display. The second attempt does that and passes every criterion.

Even then the loop doesn’t merge anything. It ends, and the change moves to the next step, which here is a person approving a draft pull request.

Retry limits

Each attempt costs time, money and review attention. Three attempts per failure is a starting rule for the example. Stop earlier when the failure repeats without new evidence.

First, each attempt must be different. Require the agent to say what changed from the last attempt and why it expects a different result. If the diff is nearly the same as one that already failed, the loop should treat that as a stop signal regardless of the count.

Second, count verification failures, not tool calls. Reading files and rerunning a quick test are part of one attempt. A full attempt is a change submitted for verification. The harness still enforces a hard ceiling on time and cost underneath, because a bug in the loop logic shouldn’t mean an unbounded bill.

When should control go back to a person?

When the agent can’t make progress on its own evidence: the bug won’t reproduce, the attempt limit is reached, the fix needs a change outside its scope, the requirements are silent or contradictory, the verification itself is unreliable, or the next action needs approval. A handoff should arrive with enough evidence that the person can decide in minutes.

Fig. 741-2 Conditions for handing control back
ConditionWhat the agent hands over
The bug won’t reproduceThe steps tried, the data used and what happened instead
The attempt limit is reachedEach attempt, why it failed verification and the agent’s best current theory
The fix needs a change outside the agreed scopeThe proposed change, where it is and why it seems necessary
The requirements conflict or are silentThe specific question, such as where blanks should sort
The verification itself is unreliableThe flaky or broken check, with the runs that disagreed
An approval-only action is nextThe diff, the evidence and the action waiting for a yes

The ticket is ambiguous about where blanks should sort. Ask for a decision, or follow the default in the prompt and report it. A diff can otherwise make a guess look like a deliberate requirement.

A good handoff states what was tried, why verification failed and what decision is needed. For example: “Attempt 2 fixes sorting but changes how the API client parses quantities. Is changing that client in scope?”

Sources

Checked in November against the writeups and documentation below. The two attempts and the completion check are illustrative. The Number() behaviour was checked in Node.

Short answers

How many times should an AI agent retry a failed fix?

Few, and only with a different approach each time. A starting limit is three verification attempts per task, each with a stated change in approach. Repeating the same idea is a signal to hand back to a person, whatever the count.

What is the difference between observing and verifying in an agent loop?

Observing is the agent reading the result of its own action, such as a test it just wrote passing. Verifying is checking the result against completion criteria that were defined before the run, ideally by something other than the agent. An agent can observe success and still fail verification.

Before you let an agent loop

  1. Write the completion check before the run, and keep it out of the agent’s reach.
  2. Send back the verification result, not just “failed.”
  3. Limit attempts, and stop early when an attempt repeats an old idea.
  4. Decide which conditions hand control back, and what the handoff report contains.
Next: graph engineering Series overview