The Pattern
Some tasks need multiple passes to get right. A loop pattern runs the same prompt repeatedly, checking after each iteration whether the result meets your criteria. When it does, the loop exits.
The flow: Generate → Check → (not done?) → Refine → Check → (not done?) → Refine → Check → Done!
Example: Self-Improving Translation
Here's a loop that keeps refining a translation until it meets quality criteria:
Translation Prompt
Translate this English text to French. The translation should be natural, idiomatic French — not word-for-word.
English: "It's raining cats and dogs outside, so I'll just Netflix and chill tonight."
Translation
"Il pleut des chats et des chiens dehors, donc je vais juste Netflix et me détendre ce soir."
Quality Check
Issue: "Il pleut des chats et des chiens" is a literal translation. French idiom would be "Il pleut des cordes" (it's raining ropes).
↻ Retry with feedback
Refinement Prompt
Previous translation had issues:
- "Il pleut des chats et des chiens" is literal, not idiomatic
Please provide an improved, more natural French translation.
Translation
"Il pleut des cordes dehors, donc je vais rester tranquille devant Netflix ce soir."
Quality Check
Better idiom usage. The casual tone could still be improved to match the original.
↻ Retry with feedback
Refinement Prompt
Previous translation is close but could better capture the casual, modern tone. Please refine.
Final Translation
"Il pleut des cordes dehors, alors ce soir c'est soirée Netflix tranquille à la maison."
Quality Check
✓ Uses proper French idiom for heavy rain
✓ Natural, casual tone matching the original
✓ Captures the staying-in-and-relaxing vibe
Quality criteria met — exit loop
Why Looping Works
First attempts are often imperfect. But AI can evaluate its own work and improve it — if you give it feedback about what's wrong. Each iteration incorporates the critique from the previous round.
The key is having clear exit criteria. What does "done" look like? A quality score above a threshold? Passing all validation checks? Without clear criteria, loops run forever.
This is automated iteration — the same evaluate-and-refine process humans do, but running until quality standards are met.
The Technique
Run a prompt, check if the result meets your criteria. If not, feed the result back with critique and run again. Repeat until the criteria are satisfied or you hit a maximum count.
Types of Exit Conditions
- • Quality score: AI rates its own output 1-10, exit when it reaches 8+
- • Validation pass: Output passes all specified checks
- • No more issues: Evaluator finds nothing to improve
- • Human approval: Loop pauses for human review
- • Max iterations: Safety limit to prevent infinite loops
Setting Limits
Always set a maximum iteration count. Even with good exit criteria, edge cases can cause infinite loops. Three to five iterations is usually enough — if it's not converging by then, the prompt or criteria likely need adjustment.
You can run loops manually (copy output, evaluate, paste back with feedback) or automate them with code. Manual loops give you more control; automated loops scale better.