Q2 给出了"8 best practice + 3 必做 + 5 nice-to-have"。Q3 进入下一步:不同项目类型 · 不同的应用组合。5 种典型项目 × 必做 3 件 × 落地优先级。
| 项目类型 | Max-iter + Stuck | Scratchpad Reset | Cost Limits | Reflection | Skill Library | 何时应用 |
|---|---|---|---|---|---|---|
| ① 单轮问答 | — | — | — | — | — | 不需要 loop |
| ② RAG 文档问答 | 必做 | — | — | — | — | Day 1 (max 5-10 round) |
| ③ 多步 agent | 必做 | 必做 | 必做 | — | — | Day 1 |
| ④ Long-loop Agent | 必做 | 必做 | 必做 | 必做 | — | Week 2+ |
| ⑤ Persistent Agent | 必做 | 必做 | 必做 | 必做 | 必做 | Month 1+ |
核心洞察:Max-iter + Stuck Detection 是 Day 1 必做 — 任何 loop 项目。其他根据复杂度递进。
场景:替代 ChatGPT 完成某领域问答(投资分析 / 代码 review / 文档总结)。
不需要 loop — 单轮 prompt 就能解决。Loop Engineering 不适用。
场景:企业内部文档 / 法律法规 / 学术论文问答。可能需要 multi-step 检索 + 答案生成。
def rag_loop(question: str, max_iter: int = 10): # Step 1: retrieve docs = retrieve(question, top_k=5) # Step 2: answer with retrieved context context = "\n".join(docs) answer = llm.invoke(f"Context: {context}\nQuestion: {question}\nAnswer:").content # Step 3: 如果 answer 不满意 (可选 loop) for i in range(max_iter): # Self-evaluation: 这个 answer 满足 question 吗? satisfied = llm.invoke(f"Question: {question}\nAnswer: {answer}\nIs this answer complete and correct? Answer YES or NO.") if "YES" in satisfied.upper(): return answer # 如果 NO, 重 retrieve with refined question refined = llm.invoke(f"Question: {question}\nAnswer: {answer}\nGenerate a better search query.").content new_docs = retrieve(refined, top_k=5) context = "\n".join(new_docs) answer = llm.invoke(f"Context: {context}\nQuestion: {question}\nRefined answer:").content return answer
关键设计:max_iter=10 防止无限循环 · self-evaluation feedback 让 LLM 自己判断满意
场景:需要 LLM 调用多个工具完成复杂任务。
from langgraph.graph import StateGraph, END from typing import TypedDict, Annotated import operator # 1. LangGraph state (loop 的 state) class LoopState(TypedDict): messages: Annotated[list, operator.add] scratchpad: str total_tokens: int MAX_ITER = 10 TOKEN_BUDGET = 50000 RESET_EVERY = 5 # 每 5 轮 reset scratchpad # 2. Loop node def loop_node(state: LoopState) -> LoopState: # Cost check if state["total_tokens"] > TOKEN_BUDGET: return {"messages": ["❌ Budget exceeded"]} # Reset scratchpad every N rounds if len(state["messages"]) % RESET_EVERY == 0: scratchpad = summarize(state["scratchpad"]) else: scratchpad = state["scratchpad"] # Think + Act response = llm.invoke(scratchpad + "\n\nNext step:") return { "messages": [response], "scratchpad": scratchpad + f"\n{response.content}", "total_tokens": state["total_tokens"] + response.usage.total_tokens, } # 3. Conditional: continue or end def should_continue(state: LoopState): if len(state["messages"]) >= MAX_ITER or state["total_tokens"] > TOKEN_BUDGET: return END return "loop" # 4. Build graph workflow = StateGraph(LoopState) workflow.add_node("loop", loop_node) workflow.add_conditional_edges("loop", should_continue, {"loop": "loop", END: END}) workflow.set_entry_point("loop") app = workflow.compile()
关键设计:3 层防护都在 state 里 — total_tokens (cost limit) · RESET_EVERY (scratchpad reset) · should_continue (max_iter + cost)
场景:loop 跑 50-200 round(数据探索 / 复杂代码生成 / Long-horizon 任务)。
def react_with_reflexion(question: str, max_iter: int = 50): scratchpad = f"Question: {question}\n" reflections = [] for i in range(max_iter): # 1. Think (with previous reflections) thought = llm.invoke( f"{scratchpad}\n\n" f"Past reflections: {reflections}\n" f"Next step (avoid repeating past mistakes):" ).content # 2. Act + Observe result = act(thought) scratchpad += f"\nStep {i+1}: {thought}\nObs: {result}\n" # 3. 如果失败, 反思 if "error" in result.lower() or "failed" in result.lower(): reflection = llm.invoke( f"Action: {thought}\nResult: {result}\n" f"Why did this fail? What should I do differently next time?" ).content reflections.append(reflection) # 4. 成功 → 返回 if "success" in result.lower(): return result
关键设计:reflections list 累积所有反思 · 下一步 think 时看到 → 避免重蹈覆辙。
场景:长期跑 agent(Cursor / 跨 session 助手 / 你的 Hermes Agent)。
Voyager 论文风格:loop 跑成功 → 沉淀为 skill → 下次 loop 复用 skill → 越跑越快 + 越跑越准。
Q3 给出了"5 项目类型 × 必做映射"。Q4 进入 效果到底多大 — Inset Magnifier · 4 角色 · 复合效应 vs 单点效应 · 量化数据。