LOOP ENGINEERING · 4-Q REPORT · 2026-08-07 PART 3 OF 4 · APPLICATION
Q3 OF 4

项目中如何应用
Loop Engineering?

5 种典型项目 × 必做 3 件 · 落地优先级 · 不同项目类型映射
mapping · 5 项目类型 × 必做
1单轮问答 — 不需要 loop (0 件必做, 单轮就够了)
2RAG 文档问答 — 1 件 (max-iter)
3多步 agent / tool-calling — 3 件全做

Q2 给出了"8 best practice + 3 必做 + 5 nice-to-have"。Q3 进入下一步:不同项目类型 · 不同的应用组合。5 种典型项目 × 必做 3 件 × 落地优先级。

§15 种典型项目 × 必做映射

项目类型 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 项目。其他根据复杂度递进。

§2项目类型 1 · 单轮问答

场景:替代 ChatGPT 完成某领域问答(投资分析 / 代码 review / 文档总结)。

不需要 loop — 单轮 prompt 就能解决。Loop Engineering 不适用。

§3项目类型 2 · RAG 文档问答

场景:企业内部文档 / 法律法规 / 学术论文问答。可能需要 multi-step 检索 + 答案生成。

必做 · 1 件

代码示例 · RAG + Max-iter

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 自己判断满意

§4项目类型 3 · 多步 Agent

场景:需要 LLM 调用多个工具完成复杂任务。

必做 · 3 件全做

代码示例 · LangGraph ReAct Loop

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)

§5项目类型 4 · Long-loop Agent

场景:loop 跑 50-200 round(数据探索 / 复杂代码生成 / Long-horizon 任务)。

必做 · 4 件

代码示例 · Reflexion 反思

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 时看到 → 避免重蹈覆辙。

§6项目类型 5 · Persistent Agent

场景:长期跑 agent(Cursor / 跨 session 助手 / 你的 Hermes Agent)。

必做 · 5 件全做

Voyager 论文风格:loop 跑成功 → 沉淀为 skill → 下次 loop 复用 skill → 越跑越快 + 越跑越准。

§7所以 · Q3 答案

5 种项目 × 必做映射:
单轮问答 · 不需要 loop
RAG 文档问答 · Max-iter only
多步 Agent · Max-iter + Scratchpad + Cost (3 件)
Long-loop Agent · + Reflection (4 件)
Persistent Agent · + Skill Library (5 件)
核心原则 · Max-iter + Stuck Detection 是 Day 1 必做,其他根据复杂度递进。
— Q3 答 · Hermes Agent · 2026-08-07

§8下一个问题

Q3 给出了"5 项目类型 × 必做映射"。Q4 进入 效果到底多大 — Inset Magnifier · 4 角色 · 复合效应 vs 单点效应 · 量化数据。

03 / Q3