20 Loop Design Patterns Every AI Engineer Should Know
Most AI engineers know how to build an agent. 大多数人工智能工程师知道如何构建一个智能体。
Very few know how to build a system that gets better after the first attempt. 极少数人知道如何构建一个在初次尝试后还能不断优化的系统。
That gap is worth six figures. 这一差距价值六位数。
Here’s the difference: 以下是关键区别:
An agent is a worker. 代理是一个工作者。
A loop is what makes the worker improve. 循环是让工作者提升的关键。
The most capable AI systems in production today are not single model calls. 当今生产环境中能力最强的AI系统并非单一模型调用。
They are loops. 它们都是循环系统。
Generate → Evaluate → Learn → Improve. 生成 → 评估 → 学习 → 改进。
Over and over. 周而复始。
Until the output is actually good. 直到输出真正达到理想效果。
Here are 20 loop design patterns that show up repeatedly in production AI systems. 以下是生产级AI系统中反复出现的20种循环设计模式。
Save this. You will build with these. 收藏这个。你将会用到这些。
Agents vs Loops 智能体(Agents)vs. 循环(Loops)
Old way: Prompt → Response → Done. 旧方式:提示 → 回复 → 完成。
New way: Generate → Critique → Rewrite → Score → Retry → Remember → Improve. 新方式:生成 → 评判 → 改写 → 评分 → 重试 → 记忆 → 改进。
One is a factory worker who does the job once. 一个是工厂工人,只做一次工作。
The other is a factory worker who studies every mistake, rewrites the playbook, and gets 3% better every single shift. 另一个工厂工人会研究每个错误,改写操作手册,每班次都进步3%。
The teams shipping production AI right now are not writing better prompts. 现在正在交付生产级AI的团队并不是在编写更好的提示词。
They are building better loops. 他们是在构建更好的循环。
CATEGORY 1 — QUALITY IMPROVEMENT LOOPS (Make the output better before it leaves the system)类别1 — 质量改进循环(在输出离开系统前提升其质量)
1. Generate → Critique → Rewrite 1. 生成 → 评估 → 重写
The most important loop in AI engineering. AI工程中最关键的循环。
Generate output. Critic reviews it. Generator rewrites based on feedback. Repeat until quality threshold is met. 生成输出。评估者进行审查。生成器根据反馈重写。重复操作直至达到质量标准。
Not one model. Two roles. One pipeline. 不是一个模型。两个角色。一个流程。
[Generator] → draft
[Critic] → "paragraph 3 is vague. missing evidence. tone is off."
[Generator] → rewrite based on critique
[Critic] → "better. but conclusion still weak."
[Generator] → final rewrite
Used for: writing, code review, reports, strategy docs, sales emails. 用途:写作、代码审查、报告、策略文档、销售邮件。
The insight: the model that generates is not the best judge of its own output. 核心洞察:负责生成的模型并不适合评判自己的输出。
A separate critic finds what the generator missed every time. 一个独立的评判者每次都能发现生成器遗漏的内容。
2. Score-and-Retry Loop 2. 评分重试循环
Generate. Score. Retry if below threshold. 生成。评分。低于阈值则重试。
Simple. Powerful. Underused. 简单。强大。却未被充分利用。
score = evaluate(output)
score = evaluate(output)
while score < threshold:
output = generate(prompt)
score = evaluate(output)
attempts += 1
if attempts > max_retries:
return best_so_far
Best when quality is measurable — extraction accuracy, format compliance, factual correctness, lead scoring. 在质量可衡量时效果最佳——例如提取准确率、格式合规性、事实正确性以及线索评分。
The generator doesn’t know it’s being graded. 生成器并不知道自己正在被评分。
The evaluator does. 评估者执行评估。
That separation is the pattern. 这种分离便是模式。
3. Multi-Critic Loop 3. 多评判循环
One critic has blind spots. 单一评判者存在盲点。
Use four. 使用四个。
→ Correctness critic: is it factually accurate? → Style critic: is it clear and well-written? → Safety critic: is it appropriate and safe? → Domain critic: does it meet specialist standards? → 正确性评判:事实是否准确? → 风格评判:是否清晰且文笔流畅? → 安全性评判:是否恰当且安全? → 领域评判:是否符合专业标准?
Each evaluates independently. 每一项都独立评估。
Final output must satisfy all four before it exits. 最终输出必须满足所有四项条件方可退出。
Used in: medical AI, legal document review, financial analysis, regulated content. 适用于:医疗AI、法律文档审查、金融分析、监管内容。
4. Adversarial Critique Loop 4. 对抗性批判循环
The critic’s only job is to break the answer. 批判者唯一的任务就是推翻答案。
Not improve it. Break it. 不是改进它。而是推翻它。
Questions the adversarial critic asks: 对抗性批评者提出的问题:
→ What assumptions fail here? → What evidence is missing? → What would a skeptic say? → Where is this confidently wrong? → 这里哪些假设不成立?→ 缺少什么证据?→ 怀疑论者会怎么说?→ 这个说法在哪些方面明显有误?
The generator then defends or rewrites. 生成者随后进行辩护或重写。
The best answer survives the attack. 最好的答案能够经受住攻击。
Used for: research synthesis, investment thesis review, strategic planning, risk analysis. 用于:研究综述、投资观点审查、战略规划、风险分析。
5. Judge Ensemble Loop 5. 评审集成循环
One judge gives noisy scores. 单一评审者给出带噪音的分数。
Five judges average out the noise. 五位评审者平均后消除噪音。
Run the same output through multiple evaluators. 将相同输出提交至多个评估器。
Aggregate scores. 汇总分数。
Only outputs with high consensus advance. 仅高一致性结果的输出得以进入下一阶段。
Used when: single-model evaluation is unreliable, stakes are high, edge cases matter. 适用场景:单模型评估结果不可靠、风险较高、边缘案例需谨慎处理时。
CATEGORY 2 — MEMORY LOOPS (Learn from what happened so next time is smarter)类别2 — 记忆循环(从过往经历中学习,让下次更聪明)
6. Reflexion Loop 6. 反思循环
The most important self-improvement pattern that exists. 这是最重要的自我改进模式。
Agent fails. Agent analyzes why it failed. Agent stores the lesson. Agent retries with that lesson in context. 智能体失败。智能体分析失败原因。智能体存储教训。智能体带着教训重试。
Each iteration: smarter than the last. 每一次迭代都比上一次更聪明。
attempt 1: fails
reflection: "I assumed X but X was wrong. Next time verify X first."
attempt 2: incorporates lesson → partial success
reflection: "Better. But I skipped Y. Add Y check."
attempt 3: succeeds
The difference between a system that fails once and a system that only fails once. 一个只失败一次的系统与一个从不重复失败的系统之间的区别。
7. Memory Update Loop 7. 记忆更新循环
After every task, store three things: 每完成一项任务后,记录三件事:
→ What decision was made → What the outcome was → What would be done differently → 做了什么决策 → 取得了什么结果 → 下次会采取什么不同做法
Future runs inherit this knowledge. 后续执行中将继承这些经验。
The system in month 6 is not the same as the system in month 1. 第6个月的系统已不再是第1个月的系统。
It has read 6 months of its own history. 它已经读取了自己过去6个月的历史。
8. Error Library Loop 8. 错误库循环
Store every failure. 记录每一次失败。
Wrong answer. Bad output. Failed execution. Edge case. 错误的答案、糟糕的输出、失败的执行、边缘情况。
Before acting on a new task: 在执行新任务之前:
Search the error library first. 先查阅错误库。
If a similar failure exists → apply the known fix before even starting. 如果存在类似的失败记录 → 在开始之前就直接应用已知的解决方案。
The system stops making the same mistake twice. 系统不再重复犯同样的错误。
The most underused pattern in production AI. 在生产环境AI中最被低估的模式。
9. Success Pattern Loop 9. 成功模式循环
Most engineers only store failures. 大多数工程师只存储失败案例。
Store successes too. 也要存储成功案例。
When a task goes well: 当一项任务进展顺利时:
→ Save the approach → Save the context → Save what made it work → 保存方法 → 保存上下文 → 保存成功的关键因素
Retrieve successful patterns when facing similar tasks. 在面对类似任务时,调用这些成功的模式。
Learn from wins. Not just mistakes. 从胜利中学习,而不仅仅是从错误中。
10. Memory Compression Loop 10. 记忆压缩循环
Memory grows forever. 记忆无休止地增长。
Unlimited memory is unusable memory. 无限的记忆是无法使用的记忆。
After N items accumulate: 在积累N个项目后:
Compress them. 压缩它们。
Many specific memories → fewer higher-level abstractions. 许多具体记忆 → 更少的高层抽象。
Before compression:
"Failed on task A because of X"
"Failed on task B because of X"
"Failed on task C because of X"
After compression:
"Pattern: X causes failures. Always check X first."
Context stays manageable. Patterns stay accessible. System stays fast. 上下文保持可控。模式保持可访问。系统保持快速。
CATEGORY 3 — PLANNING LOOPS (Adapt the plan when reality changes)类别3 — 规划循环(当现实变化时调整计划)
11. Plan → Execute → Replan 11. 规划 → 执行 → 重新规划
The most common mistake in AI agent design: AI智能体设计中最常见的错误:
Treating the plan as fixed. 将计划视为固定不变。
Plans break on contact with reality. 计划在现实面前往往不堪一击。
The pattern: 模式如下:
Create plan → execute step → observe outcome → update plan → continue 制定计划 → 执行步骤 → 观察结果 → 更新计划 → 继续推进
Not a waterfall. 而非瀑布式流程。
A spiral. 一个螺旋。
Each lap around tightens the approach. 每一圈的环绕都在收紧逼近。
Used when: environment changes, tasks have dependencies, long-horizon goals. 适用场景:环境变化时,任务具有依赖性,需要长期目标的场景。
12. Dynamic Workflow Loop 12. 动态工作流循环
Most pipelines are fixed. 大多数流水线都是固定的。
Step 1 → Step 2 → Step 3. Always. 步骤1 → 步骤2 → 步骤3。始终如此。
Dynamic workflows change based on results. 动态工作流会根据结果发生变化。
If output A → run branch X If output B → run branch Y If output C → skip to step 5 如果输出A → 执行分支X;如果输出B → 执行分支Y;如果输出C → 跳至步骤5。
The pipeline decides its own shape at runtime. 流水线在运行时自行决定其形态。
Used in: multi-document research, customer support routing, adaptive content pipelines. 用于:多文档研究、客户支持路由、自适应内容流水线。
13. Goal Decomposition Loop 13. 目标分解循环
Large goal enters. 大型目标输入。
System breaks it into subgoals. 系统将目标分解为子目标。
Each subgoal breaks into tasks. 每个子目标分解为任务。
Each task breaks into steps. 每个任务分解为步骤。
Decompose until each unit is small enough to execute in one call. 持续分解,直到每个单元小到能够通过单次调用执行。
Goal: "Write a comprehensive competitive analysis"
↓
Subgoal 1: "Identify top 5 competitors"
Subgoal 2: "Analyze each competitor's product"
Subgoal 3: "Compare pricing models"
Subgoal 4: "Identify gaps"
↓
Each subgoal → tasks → individual model calls
The loop keeps decomposing until the system can act. 循环持续分解,直至系统能够执行。
14. Progress Evaluation Loop 14. 进度评估循环
Every N steps: stop and ask. 每 N 步:暂停并提问。
“Are we actually getting closer to the goal?” “我们真的离目标更近了吗?”
If yes: continue current strategy. If no: change strategy, tools, or plan. 如果是:继续当前策略。如果否:改变策略、工具或计划。
The system monitors its own progress. 系统会自我监控进展。
Not just executes blindly. 而不是盲目执行。
Used in: long-running research agents, multi-day autonomous tasks, debugging agents. 用于:长时间运行的研究型智能体、持续数日的自主任务、调试型智能体。
15. Constraint Satisfaction Loop 15. 约束满足循环
Keep running until all constraints are met. 持续运行,直至所有约束条件均得到满足。
while not all_constraints_satisfied(output):
output = improve(output, unsatisfied_constraints)
constraints = [
budget_under_limit,
quality_above_threshold,
latency_under_200ms,
tone_matches_brand,
no_hallucinations
]
Very common in production systems. 在生产系统中非常常见。
The output is not done until every business rule passes. 只有所有业务规则都通过后才会输出结果。
CATEGORY 4 — EXPLORATION LOOPS (Find the best answer by trying multiple paths)第4类——探索型循环(通过尝试多条路径找到最佳答案)
16. Branch-and-Explore Loop 16. 分支探索循环
Don’t commit to one path. 不要只走一条路。
Explore several simultaneously. 同时探索多条路径。
paths = [
generate(approach="conservative"),
generate(approach="aggressive"),
generate(approach="creative")
]
scores = [evaluate(p) for p in paths]
best = paths[scores.index(max(scores))]
Compare outcomes. Choose the best branch. Discard the rest. 比较结果,选择最佳分支,舍弃其余。
Used for: content variations, architecture decisions, debugging multiple hypotheses, A/B generation. 用于:内容变体、架构决策、调试多个假设、A/B生成。
17. Tree Search Loop 17. 树搜索循环
Branch-and-Explore goes one level deep. 分支探索仅深入一层。
Tree Search goes as deep as needed. 树搜索则根据需要深入任意层次。
Expand the most promising nodes. Prune the weakest ones. Keep exploring until the solution is found. 扩展最有希望的节点。剪除最弱的分支。持续探索直至找到解决方案。
root → [A, B, C]
A → [A1, A2] # A looks promising, expand it
B → prune # B is weak, stop here
A1 → [A1a, A1b]
A1a → solution ✓
Used for: complex reasoning chains, multi-step planning, code debugging, research synthesis. 适用于:复杂推理链、多步骤规划、代码调试、研究综合。
Computationally expensive but finds solutions single-pass calls cannot. 计算成本高昂,但能解决单次调用无法找到的方案。
18. Debate Loop 18. 辩论循环
Two agents. One topic. Opposite positions. 两个智能体。一个主题。对立的立场。
Agent A argues for the answer. Agent B argues against it. 智能体A为答案辩护。智能体B则提出反对意见。
Each round challenges assumptions, demands evidence, exposes weak logic. 每一轮都质疑假设、要求证据、暴露逻辑漏洞。
Final answer emerges through disagreement. 最终答案在分歧中浮现。
Not through agreement. 而非共识。
The adversarial pressure finds what confident single-agent answers miss. 对抗性压力能够发现自信的单一智能体答案所遗漏之处。
Used for: investment decisions, strategic planning, risk assessment, research critique. 用于:投资决策、战略规划、风险评估、研究评论。
CATEGORY 5 — SYSTEM OPTIMIZATION LOOPS (The loop improves the loop)类别5——系统优化循环(用循环改进循环)
19. Prompt Optimization Loop 19. 提示词优化循环
Most engineers write a prompt once and never touch it again. 多数工程师写一次提示词后便不再修改。
Prompt optimization loops change that. 提示词优化循环改变了这一状况。
The system: 系统:
→ Runs the prompt on a test set → Scores every output → Identifies where the prompt fails → Rewrites the prompt to fix those failures → Reruns and rescores → 在测试集上运行提示 → 对每个输出进行评分 → 识别提示失败的位置 → 重写提示以修复这些失败 → 重新运行并重新评分
The prompt gets better automatically. 提示会自动改进。
Without a human touching it. 无需人工干预。
current_prompt = "Summarize this 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)
# Prompt rewrites itself based on where it fails
Used in: production pipelines, automated content systems, classification tasks. 应用于:生产流水线、自动化内容系统、分类任务。
The best prompts in production AI were not written by a human. 生产环境中最佳的人工智能提示并非由人类撰写。
They were evolved. 它们是经过演化生成的。
20. Workflow Optimization Loop 20. 工作流优化循环
This is where it gets interesting. 有意思的地方来了。
The loop improves the loop. 循环优化循环本身。
The system measures its own performance: 系统衡量自身性能:
→ latency: how long does each step take? → cost: how many tokens does each call use? → quality: what is the output score at each stage? → 延迟:每一步耗时多久? → 成本:每次调用消耗多少 token? → 质量:每个阶段的输出评分如何?
Then it modifies its own workflow. 随后它会调整自己的工作流程。
Too slow? Parallelize two steps. Too expensive? Replace a GPT-4 call with a smaller model where quality holds. Quality dropping? Add a critic before the final output. 速度太慢?将两个步骤并行化。成本过高?在质量达标处,将 GPT-4 调用替换为更小的模型。质量下降?在最终输出前添加一个审核环节。
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)
This is where truly self-improving systems begin. 这就是真正自我进化系统的起点。
Not just outputs that improve. 不仅仅是输出结果的改进。
Systems that redesign themselves. 而是系统能够自我重构。
The pattern behind all 20 patterns 这正是所有 20 种模式背后的核心模式。
Every single loop above shares one structure: 以上每个循环都共享同一结构:
Act → Observe → Evaluate → Adjust 行动 → 观察 → 评估 → 调整
That is the entire recipe. 这就是全部秘诀。
The output is never final on the first attempt. 初次尝试永远不是最终输出。
The output is a starting point. 输出只是一个起点。
The loop is what turns a starting point into something production-worthy. 循环机制是将起点转化为可投产产品的关键。
The full map 完整图谱
Category 1 — Quality Loops (Make output better before it leaves)第一类——质量循环(在输出前提升质量)
→ 1. Generate → Critique → Rewrite → 1. 生成 → 批判 → 重写
→ 2. Score-and-Retry → 2. 评分并重试
→ 3. Multi-Critic → 3. 多重批判
→ 4. Adversarial Critique → 4. 对抗式批判
→ 5. Judge Ensemble → 5. 裁判集成
Category 2 — Memory Loops (Learn from what happened)类别二 —— 记忆循环(从已发生之事中学习)
→ 6. Reflexion → 6. 反思
→ 7. Memory Update → 7. 记忆更新
→ 8. Error Library → 8. 错误库
→ 9. Success Pattern → 9. 成功模式
→ 10. Memory Compression → 10. 记忆压缩
Category 3 — Planning Loops (Adapt when reality changes)第三类——规划循环(在现实变化时自适应调整)
→ 11. Plan → Execute → Replan → 11. 规划 → 执行 → 重新规划
→ 12. Dynamic Workflow → 12. 动态工作流
→ 13. Goal Decomposition → 13. 目标分解
→ 14. Progress Evaluation → 14. 进度评估
→ 15. Constraint Satisfaction → 15. 约束满足
Category 4 — Exploration Loops (Find best answer by trying many paths)类别 4 — 探索循环(通过尝试多条路径寻找最佳答案)
→ 16. Branch-and-Explore → 16. 分支探索
→ 17. Tree Search → 17. 树搜索
→ 18. Debate → 18. 辩论
Category 5 — System Optimization Loops (The loop improves the loop)第5类 — 系统优化循环(循环优化循环)
→ 19. Prompt Optimization → 19. 提示词优化
→ 20. Workflow Optimization → 20. 工作流优化
Most engineers think agents are the future. 大多数工程师认为智能体是未来。
Agents are just workers. 智能体只是工作者。
Loops are what make workers improve. 循环才是让工作者进步的关键。
The biggest shift happening in AI right now is not better models. 目前人工智能领域最大的变革并非模型本身的提升。
It’s moving from: 正从:
Prompt → Response 提示词 → 回答
to 转变为
Generate → Evaluate → Learn → Improve 生成 → 评估 → 学习 → 改进
The teams that master loop design will not build better prompts. 擅长循环设计的团队不会仅仅是构建更好的提示词。
They will build systems that get better every single day after deployment. 他们将构建出在部署后每天都能自我优化的系统。
Without anyone touching them. 无需任何人干预。
If this was useful: 如果这对你有用的话:
→ Repost to share it with every AI engineer you know → Follow @sairahul1 for more patterns like this → Bookmark this — pick one loop and implement it this week→ 转发分享给你认识的每一位AI工程师 → 关注@sairahul1获取更多此类模式 → 收藏本文——挑选一个循环,本周付诸实践
I write about AI, building products, and systems that work without you. 我撰写关于AI、产品搭建以及无需你介入即可自主运行的系统的内容。