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:
English: "It's raining cats and dogs outside, so I'll just Netflix and chill tonight."
- "Il pleut des chats et des chiens" is literal, not idiomatic
Please provide an improved, more natural French translation.
✓ 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.