Refine an agent's output until it passes a quality check

One agent drafts, a second agent scores it against your criteria, and the loop repeats until the check passes or the turn limit is reached.

Stopping a loop that will not converge

An evaluator-optimiser loop passes an output back and forth between two agents until it clears a check. One agent produces a version, a second agent reads it against the set criteria, and if the check fails, the output goes back with that feedback attached. The exchange repeats, each round carrying forward the previous attempt and the latest notes, until the evaluator agrees the output is ready.

What happens if it never clears the check

Nothing in that exchange guarantees an end point on its own. The evaluator can hold a standard the optimiser keeps missing, the feedback can point back toward something already tried, or the criteria themselves can be set in a way no output will satisfy. Left alone, the two agents keep handing the same work back and forth, and each turn adds a model call, a delay, and a cost to the workflow.

Setting a turn limit

A step built to repeat in this way can carry a limit on how many turns it takes before it stops, regardless of whether the evaluator has passed the output yet. Reaching that limit ends the loop and moves the workflow on.

Write the limit into the plan next to the handoff order between the two roles. The turn count then sits in the same place as the agents doing the work.

Deciding what happens at the limit

Reaching the turn limit still needs somewhere to go. The last output produced can be passed on as it stands, flagged for a person to look at, or routed to a different agent, depending on how much the workflow can tolerate an answer the evaluator never signed off. That choice sits with whoever is designing the workflow. The loop can tell you a check kept failing; it cannot tell you why the criteria were wrong, or whether the last attempt is good enough to use.

Four Turns to an Approved Onboarding Email

This example is invented to show the pattern.

The writer agent drafts a 150-word onboarding email for new trial users of a project management tool. The evaluator agent scores each draft out of 10 against three criteria — clarity, tone, and the strength of the call to action — and calculates the average. The exit condition is an average of 8 or higher, and the loop is capped at four turns.

Scores across four turns

TurnClarityToneCall to actionAverageResult
15665.7Below threshold, returned with feedback
27766.7Below threshold, returned with feedback
37887.7Below threshold, returned with feedback
48988.3Threshold met, loop exits

How the loop runs

  1. The writer agent produces the first draft and hands it to the evaluator agent.
  2. The evaluator agent scores it on clarity, tone, and call to action, then averages the three: (5 + 6 + 6) ÷ 3 = 5.7.
  3. 5.7 falls short of the 8.0 exit condition, so the evaluator agent flags the call to action as underdeveloped and hands the draft back.
  4. The writer agent revises. The second draft scores (7 + 7 + 6) ÷ 3 = 6.7 — closer, but still below threshold, so the loop continues.
  5. The writer agent revises again. The third draft scores (7 + 8 + 8) ÷ 3 = 7.7, within half a point of the threshold but not over it.
  6. The writer agent produces a fourth draft, scoring (8 + 9 + 8) ÷ 3 = 8.3. This clears the exit condition, and the loop exits with that draft as the result.

The loop used every turn available to it. Turn four is also the cap set at the start, so if the average had landed at 7.9 instead of 8.3, the orchestrator would have stopped the loop there regardless and passed the fourth draft on as the best attempt made, rather than letting the writer and evaluator agents negotiate indefinitely.

Setting the criteria the loop exits on

A writer agent drafts, an evaluator agent reads the draft, and the writer revises. That loop only ever stops if someone has decided in advance what counts as good enough. Without a stated criterion, the evaluator is just giving an opinion that can shift on every pass, and a loop built on a shifting opinion has no reason to end.

Writing criteria the evaluator can check

A usable criterion is something the evaluator agent can test against the draft directly. That might mean checking the draft against a source document for factual claims, checking it against a word count or section structure, or checking that a required field is present in the output. The more concrete the check, the more consistent the score from one pass to the next.

  • Factual claims matched against a named source
  • Required sections or fields present in the output
  • Length, format, or structure constraints met
  • Tone or terminology matched against a stated style

Setting the exit condition

Once the criteria are written down, the exit condition is the rule that decides what happens with the score. That can be a pass or fail against each criterion, or a numeric score against a threshold you set. Below the threshold, the evaluator hands back to the writer with the specific criteria that failed. At or above it, the evaluator hands the draft forward to the next role in the chain.

Give that trigger condition to the evaluator role in your plan. The handoff back to the writer, or on to the next agent, then follows from the score.

What happens with loose criteria

An instruction like "make it better" gives the evaluator nothing to check, so the score drifts with each pass and the loop has no stable point to converge on. Writing the criteria as a short list, each one checkable against the draft, is what gives the writer agent something concrete to revise against and gives the evaluator a reason to pass the draft on.