20 Loop Design Patterns Every AI Engineer Must Master

This article catalogs twenty high‑frequency loop architectures that transform single‑call AI models into autonomous, self‑optimising agents, explaining each pattern’s purpose, workflow, concrete code example, and typical commercial scenarios such as content creation, compliance review, and strategic decision making.

AI Architecture Hub
AI Architecture Hub
AI Architecture Hub
20 Loop Design Patterns Every AI Engineer Must Master

Loop Design vs. Single‑Call Pipelines

Traditional single‑call pipelines execute prompt → model output → stop with no feedback. Closed‑loop architectures repeatedly run generate → evaluate → learn → optimise cycles, allowing the agent to improve autonomously until predefined quality thresholds are satisfied.

Core Loop Patterns

1. Generate‑Critic‑Rewrite Loop

Two independent models cooperate: a generator produces an initial draft; a critic identifies issues (e.g., vagueness, missing evidence). The generator rewrites based on the critic’s feedback. The cycle repeats until quality standards are met.

[Generator] output initial draft
[Critic] feedback: paragraph 3 is vague, lacks evidence
[Generator] revise draft based on feedback
[Critic] second review: improved, but conclusion weak
[Generator] final revision

Applicable to copywriting, code compliance checks, market‑research reports, business strategy documents, and formal emails.

2. Score‑Retry Loop

The model generates content, an automated evaluator assigns a quantitative score, and if the score is below a threshold the system regenerates the content. The loop continues until the score is acceptable.

score = evaluate(output)
while score < threshold:
    output = generate(prompt)
    score = evaluate(output)
    attempts += 1
    if attempts > max_retries:
        return best_so_far

Used where quality metrics are easily quantifiable (information‑extraction accuracy, format compliance, factual correctness, completeness).

3. Multi‑Critic Parallel Loop

Four specialised critics evaluate different dimensions; all critiques must be passed before the content is released, reducing blind spots of a single critic.

Fact‑check critic – verifies truthfulness.

Style critic – assesses readability and tone.

Compliance critic – screens for legal or policy violations.

Domain critic – checks against industry standards.

Common in regulated fields such as medical AI, legal document generation, and financial data analysis.

4. Adversarial Critic Loop

An adversarial critic actively seeks contradictions and hidden assumptions, forcing the generator to defend and strengthen its arguments. Valuable for literature reviews, investment‑logic checks, and strategic planning.

5. Multi‑Review Aggregation Loop

Multiple independent reviewers score the same output; the system aggregates scores and proceeds only when a consensus above a threshold is reached, mitigating random noise from any single reviewer.

6. Reflexion (Self‑Reflection) Loop

When a task fails, the agent analyses the failure, extracts a lesson, stores it, and reuses the insight in future similar tasks, reducing repeat mistakes.

Attempt 1: task failed – assumption X invalid
Reflexion: store lesson "validate X"
Attempt 2: apply lesson → partial success
Reflexion: identify missing step Y
Attempt 3: apply both lessons → task completed

7. Task Memory Update Loop

Each completed task logs three core items: decision logic, execution result, and optimisation suggestions. Future tasks retrieve and apply this accumulated knowledge, gradually improving the agent’s policy.

8. Error‑Case Library Loop

All failure cases (error output, exception traces, edge‑case data) are stored. Before a new task, the system searches for similar failures and reuses proven fixes, dramatically reducing repeat errors.

9. Success‑Case Reuse Loop

Successful executions are archived with context and strengths. When a new task matches a past success, the system reuses the proven solution, balancing learning from both failures and wins.

10. Memory Compression & Abstraction Loop

When raw memory grows large, the system abstracts repetitive patterns into high‑level rules, preserving essential knowledge while improving retrieval efficiency.

Raw records:
  Task A fails – missing X
  Task B fails – missing X
Compressed rule:
  If X not validated → high failure risk

11. Dynamic Planning Loop (Plan‑Execute‑Replan)

The agent creates an initial plan, executes a step, observes results, and replans based on feedback, allowing adaptation to changing environments.

12. Dynamic Branch Workflow Loop

Workflow branches are chosen at runtime based on intermediate outputs (e.g., if result matches scenario A, follow path X; else follow path Y). Suitable for multi‑document research, intelligent routing, and adaptive content generation.

13. Goal‑Decomposition Loop

Large objectives are recursively broken into sub‑goals until each sub‑goal fits within a single model call, ensuring complex tasks are tractable.

Top goal: Produce competitor analysis
→ Select top‑5 competitors
→ Analyse core features
→ Compare pricing models

14. Execution‑Progress Evaluation Loop

After a fixed number of steps, the system pauses to assess whether progress aligns with the goal; if not, it switches strategies, invokes tools, or restructures the plan.

If progress positive → continue.

If stalled → change strategy.

15. Constraint‑Satisfaction Loop

The agent iteratively improves output until all business constraints (budget, latency, quality, style, factuality) are satisfied.

while not all_constraints_satisfied(output):
    output = improve(output, unsatisfied_constraints)
# Example constraints
constraints = [budget ≤ limit,
               quality ≥ threshold,
               latency ≤ 200ms,
               style matches brand,
               no hallucinations]

16. Prompt‑Auto‑Optimization Loop

The system runs the current prompt on a test set, scores results, identifies weak spots, rewrites the prompt, and repeats until the average score meets the target.

current_prompt = "Summarise the document."
for iteration in range(max_iterations):
    outputs = [run(current_prompt, doc) for doc in test_set]
    scores = [evaluate(o) for o in outputs]
    avg_score = mean(scores)
    if avg_score >= target:
        break
    failures = [o for o, s in zip(outputs, scores) if s < threshold]
    current_prompt = improve_prompt(current_prompt, failures)

17. System‑Self‑Optimization Loop (Global Workflow Optimisation)

The agent monitors latency, token cost, and quality across the whole pipeline. If latency exceeds a target, slow steps are parallelised; if cost is high, expensive models are swapped for lighter ones; if quality drops, additional critic steps are inserted.

metrics = measure_workflow(outputs, latency, cost)
if metrics.latency > target_latency:
    workflow = parallelize(slow_steps)
if metrics.cost > budget:
    workflow = replace_with_cheaper_model(high_cost_steps)
if metrics.quality < threshold:
    workflow = add_critic_before(final_output_step)

Unified Execution Chain

All twenty patterns share a common backbone: action → observation → evaluation → strategy adjustment . By repeatedly applying this chain, agents evolve from simple prompt‑response tools into robust, commercially viable AI systems capable of autonomous, long‑term improvement.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

design patternsPrompt Engineeringagent architecturefeedback mechanismsAutonomous AgentsAI loopsiterative optimisation
AI Architecture Hub
Written by

AI Architecture Hub

Focused on sharing high-quality AI content and practical implementation, helping people learn with fewer missteps and become stronger through AI.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.