Glean 拾遗
日刊 /2026-07-27 / Agent 架构三层次:Harness、Loop 与 Graph 工程区分

Agent 架构三层次:Harness、Loop 与 Graph 工程区分

原文 x.com 收录 2026-07-27 06:00 阅读 17 min
AI 解读

本文清晰区分了构建 AI Agent 时的三种核心工程层:Agent Harness(模型外围的代码、配置与运行时)、Loop(重复的工作-反馈循环)和 Graph(显式的工作流拓扑)。作者指出,Harness 负责提供工具、状态、安全和可观测性;Loop 负责设计迭代验证与停止条件;Graph 负责控制节点顺序、分支与并行。三者不是替代关系,而是协同工作。文章还给出了常见失败模式(如先画图后理解工作、无限制重试)和诊断表格,帮助工程师根据症状选择正确的优化层次。适合需要将 Agent 从演示推向生产的团队阅读。

原文 17 分钟
原文 x.com ↗
§ 1

Agent Harness Engineering vs. Loop Engineering vs. Graph Engineering

A practical guide to the three architecture layers people keep mixing together

The confusion is understandable. All three ideas sit around the same model, all three influence reliability, and all three can contain "loops." But they are not synonyms. They describe different engineering decisions, and the distinction matters the moment an agent leaves a demo notebook and starts touching files, APIs, customers, or production code

THE 30-SECOND ANSWER

  • Harness engineering builds the machinery around the model
  • Loop engineering designs the repeated work-and-feedback cycle
  • Graph engineering makes the workflow topology explicit: nodes, branches, joins, state transitions and controlled cycles The clean mental model is environment → feedback → flow

Agent Harness Engineering vs. Loop Engineering vs. Graph Engineering

这是一份实用指南,帮助理解人们常常混淆的三个架构层。

这种混淆情有可原——三者都围绕同一个模型、都影响可靠性、也都可能包含“循环”。但它们并非同义词。它们描述的是不同的工程决策,当代理走出演示笔记本,开始操作文件、API、客户或生产代码时,区分它们就变得至关重要。

30 秒快速回答

  • 工作台工程:构建模型外围的运转机制
  • 循环工程:设计重复执行与反馈的周期
  • 图工程:将工作流拓扑显式化——节点、分支、汇合、状态转移和受控循环

清晰的思维模型是:环境 → 反馈 → 流程

§ 2

A raw language model cannot create text, maintain a state for a project, run a test suite, look at a browser, enforce an approval rule, or restart a failed job. Those capabilities come from the environment it's in. As agentic software matures, a standard engineering stack is finally coming together. At the foundation is the agent harness, the code that actually runs the models. Next are the loops, which handle the repeating execution and quality checks. Finally, graphs map out the structured paths that guide the entire process

Labels are still not consistently standardized. In the current framework, the term "agent harness" is now starting to take on a rather specific definition. The term "loop engineering" arose as a newer term among practitioners in 2026. Graph engineering should be understood practically rather than as an academic field; it is just the process of creating agent workflows as explicit directed graphs or state machines. This practical distinction is helpful because it prevents a buzzword from hiding the real design question

原始语言模型无法创建文本、维护项目状态、运行测试套件、查看浏览器、执行审批规则或重启失败的任务。这些能力来自它所处的环境。随着智能体软件逐步成熟,一套标准的工程栈终于开始成形。底层是代理工作台(agent harness),即实际运行模型的代码;上层是循环(loops),负责重复执行和质量检查;最后是图(graphs),描绘出引导整个流程的结构化路径。

标签至今仍未完全标准化。在当前的框架中,“代理工作台”这一术语已经开始具备相当具体的定义;“循环工程”是 2026 年从业人员中兴起的新术语;而“图工程”应被理解为实践方法而非学术领域——它就是创建代理工作流为显式有向图或状态机的过程。这种实用的区分很有帮助,因为它能防止热门词汇掩盖真正的设计问题。

§ 3
  • According to Langchain, the agent is the model plus the harness, and the harness is the code, configuration and execution logic outside the model. In practice, this includes the system prompt, tool definitions, memory, filesystems, sandboxes, model routing, handoffs, middleware hooks, compaction, permissions, logging and verification interfaces
  • OpenAI's Agents SDK describes the same operational core from a runtime perspective: the runner calls the model, executes tool calls, handles handoffs, carries state and stops only when the run reaches a real terminal condition

The word harness is useful because it shifts attention away from model worship. Two teams can use the same foundation model and get very different outcomes because one gives the model clean tools, a stable workspace, constrained permissions and observable state, while the other gives it a vague prompt and an unreliable API wrapper. The intelligence may be similar; the working conditions are not. What a serious harness usually contains:

  • Context injection: instructions, retrieved facts, conversation state, skills and task-specific policies
  • Action surfaces: APIs, browsers, shells, code interpreters, databases and MCP-compatible tools
  • Persistence: files, checkpoints, sessions, progress logs, git history and long-term memory
  • Execution control: timeouts, retries, budgets, model routing, sub-agent spawning and approval gates
  • Safety and governance: permissions, isolation, allow lists, secret handling and human authorization
  • Observability: traces, tool inputs and outputs, state transitions, cost, latency and evaluation results

The model sits inside a wider harness of context, control, action, persistence and verification. Remove the model from your architecture diagram. Everything left is probably part of the harness: the tools, data access, state store, sandbox, middleware, evaluators, retry policy and UI

  • 根据 LangChain 的定义,代理是模型加上工作台(harness),而工作台是模型之外的代码、配置和执行逻辑。实践中,它包括系统提示、工具定义、记忆、文件系统、沙箱、模型路由、交接、中间件钩子、压缩、权限、日志记录和验证接口。
  • OpenAI 的 Agents SDK 从运行时角度描述了相同的操作核心:运行器(runner)调用模型、执行工具调用、处理交接、携带状态,仅当运行达到真正的终止条件时才停止。

“工作台”这个词很有用,因为它将注意力从模型崇拜上移开。两个团队可以使用相同的基础模型,得到截然不同的结果,因为一个团队为模型提供了清晰的工具、稳定的工作空间、受限的权限和可观测的状态,而另一个团队只给了模糊的提示词和不可靠的 API 封装。智能程度可能相似,但工作条件不同。一个严肃的工作台通常包含:

  • 上下文注入:指令、检索到的事实、对话状态、技能和任务特定策略
  • 操作界面:API、浏览器、Shell、代码解释器、数据库和 MCP 兼容工具
  • 持久化:文件、检查点、会话、进度日志、Git 历史和长期记忆
  • 执行控制:超时、重试、预算、模型路由、子代理生成和审批门禁
  • 安全与治理:权限、隔离、允许列表、密钥处理和人工授权
  • 可观测性:追踪、工具输入输出、状态转换、成本、延迟和评估结果

模型位于一个更广阔的工作台之中,包含上下文、控制、动作、持久化和验证。从架构图中移除模型,剩下的几乎都是工作台的一部分:工具、数据访问、状态存储、沙箱、中间件、评估器、重试策略和 UI。

§ 4

Where harness engineering earns its keep

Harness work is important for long-running tasks. In multi-session coding, Anthropic discovered that simply using context compaction was not sufficient. This was not a better prompt by itself, but they made a good setup that created an initializer, a progress file, git history and a discipline of incremental work that each new context can understand what happened and what is still to do. It's an improved working system with regard to the agent. Apply harness engineering when the agent doesn't have a capability, can't come back clean, loses state, accesses too much, can't be audited, or acts differently on environments

工作台工程的价值所在

工作台对于长时间运行的任务至关重要。在多会话编码中,Anthropic 发现仅仅使用上下文压缩是不够的。这本身并不是更好的提示词,而是他们建立了一个良好的设置:创建初始化器、进度文件、Git 历史以及增量工作的纪律,使每个新上下文都能理解已完成的工作和尚待完成的工作。这是一个针对代理的改进工作系统。当代理不具备某项能力、无法干净地恢复、丢失状态、访问过多、无法审计或在不同环境下行为不一致时,就应该应用工作台工程。

§ 5

Each agent that uses a tool has an embedded small loop:

  • call the model
  • look at the results
  • run the tools
  • input observations into the model
  • repeat until a final answer is returned When the builders intentionally build or stack new cycles around that behavior, it is the beginning of loop engineering, as OpenAI calls it. A verification loop, for instance, allows the agent to create an artifact, execute the deterministic check or a grader, receive explicit feedback and repeat only if there are evidence errors. An event-driven loop awakens the agent when a schedule, webhook, or new document is received. An improvement loop analyzes traces & failures, modifies instructions/tools, and tests if the new version works better. LangChain's 2026 framing refers to these as a stack of loops and not one magic while-statement

The anatomy of a well-engineered loop:

  • Trigger: what starts another cycle; user request, schedule, failed test, new data or evaluator feedback
  • Goal: a specific state to reach, not a vague instruction to "keep improving"
  • State and memory: what the next cycle needs to know without replaying everything
  • Action policy: what the agent may change, call, delegate or spend
  • Evidence: tests, schema validation, citations, diffs, metrics or human review
  • Feedback: a compact, actionable description of why the evidence failed
  • Stopping rule: success, budget limit, timeout, irrecoverable error or human escalation

A verification loop wraps the agent loop with an external grader and an explicit pass condition. Do not loop on confidence. Loop on evidence. "The agent says it is done" is not a stopping condition; "the tests pass, the links resolve, the schema validates and the reviewer approves" is.

每个使用工具的代理都有一个内嵌的小循环:

  • 调用模型
  • 查看结果
  • 运行工具
  • 将观察结果输入模型
  • 重复直到返回最终答案 当构建者有意在该行为周围构建或堆叠新的循环时,就是循环工程的开始,OpenAI 称之为循环工程。例如,验证循环允许代理创建工件、执行确定性检查或评分器、接收显式反馈,并仅在存在证据错误时重复。事件驱动循环在接收到计划任务、Webhook 或新文档时唤醒代理。改进循环分析追踪和失败、修改指令/工具,并测试新版本是否更好。LangChain 的 2026 年框架将这些称为循环堆栈,而非一个神奇的 while 语句。

一个精心设计的循环的结构:

  • 触发器:什么启动另一个周期?用户请求、计划任务、测试失败、新数据或评估器反馈
  • 目标:要达成的具体状态,而非模糊的“持续改进”指令
  • 状态与记忆:下一周期需要知道什么,而不必回放所有内容
  • 行动策略:代理可以改变、调用、委托或花费什么
  • 证据:测试、模式验证、引用、差异、指标或人工审查
  • 反馈:关于证据为何失败的紧凑、可操作的描述
  • 停止规则:成功、预算限制、超时、不可恢复错误或人工升级

验证循环用外部评分器和显式通过条件来包装代理循环。不要基于信心进行循环,而要基于证据进行循环。“代理说完成了”不是停止条件;“测试通过、链接可解析、模式验证通过、审查者批准”才是。

§ 6

Why loop engineering is not just prompt engineering

A prompt tells the model what to do during a call. A loop specifies what the system does after the call:

how it observes results, chooses feedback, decides whether to continue, persists progress and terminates

Prompt quality still matters, but the loop converts a one-shot instruction into a managed process. The main tradeoff is cost and latency. Each grader, reviewer or retry adds another model call or tool run. Anthropic's broader guidance is to prefer the simplest architecture that works and add agentic complexity only when the performance gain justifies it. The same advice applies to loops: add them where the failure cost is higher than the verification cost

为什么循环工程不仅仅是提示工程

提示词告诉模型在一次调用中做什么。循环则指定系统在调用之后做什么:

它如何观察结果、选择反馈、决定是否继续、持久化进度以及终止

提示词质量仍然重要,但循环将一次性指令转化为受管理的过程。主要的权衡是成本和延迟。每个评分器、审查者或重试都会增加一次模型调用或工具运行。Anthropic 的总体指导是:优先选择最简单的可行架构,只有在性能提升合理时才增加智能体复杂性。同样的建议也适用于循环:在失败成本高于验证成本的地方添加它们。

§ 7

Graph engineering asks a different question though: Not only what the agent does, but what component is permitted to run next. Steps are represented by nodes and allowed steps are represented by edges. These edges can be used to indicate sequence, conditional branching, parallel fan-out, joins, loops and human interrupts. The state traverses the graph, and the topology allows for the desired control flow to be checked. LangGraph is low-level orchestration infrastructure for long-running, stateful agents, with durable execution, state and human-in-the-loop control, and an explicit focus on control over agents rather than an abstraction of the workflow. Microsoft AutoGen's documentation is exceptionally straightforward: use a graph when you need exact control over agent order, different next steps for different outcomes, deterministic branching or complex multi-step processes with cycles. What graph engineers actually decide:

  • Node boundaries: which work belongs in a deterministic function, an LLM call, a specialist agent or a human review step
  • State schema: what each node may read or update, and how parallel updates are merged
  • Routing conditions: which evidence sends work forward, backward, sideways or to escalate
  • Concurrency: what can run in parallel, what must join, and what shared resources need coordination
  • Cycles and exits: where retries are legal, how many are allowed, and what makes the cycle safe
  • Durability: where checkpoints occur and how execution resumes after interruption

The above canvas makes agents, skills and relationships inspectable as a composed system. Graph engineering here means engineering graph-based execution. It is not the same as knowledge graph engineering, where the graph represents entities and relationships in data. A workflow graph represents control and state transitions

图工程提出了一个不同的问题:不仅代理做什么,而且哪个组件可以接着运行。步骤由节点表示,允许的步骤由边表示。这些边可以用来指示顺序、条件分支、并行扇出、汇聚、循环和人工中断。状态在图上游走,拓扑结构允许检查所需的控制流。LangGraph 是用于长时间运行的有状态代理的低层编排基础设施,具有持久执行、状态和人工在环控制,并明确关注对代理的控制而非工作流的抽象。Microsoft AutoGen 的文档非常直接:当你需要精确控制代理顺序、不同结果对应不同后续步骤、确定性分支或含循环的复杂多步过程时,使用图。图工程师实际决定的内容:

  • 节点边界:哪些工作属于确定性函数、LLM 调用、专业代理或人工审查步骤
  • 状态模式:每个节点可以读取或更新什么,以及并行更新如何合并
  • 路由条件:哪些证据将工作向前、向后、侧向发送或升级
  • 并发:哪些可以并行运行,哪些必须汇聚,以及哪些共享资源需要协调
  • 循环与出口:在哪里允许重试、允许多少次,以及什么使循环安全
  • 持久性:检查点在哪里发生,以及中断后执行如何恢复

上述画布使代理、技能和关系作为组合系统可被检查。这里的图工程意味着基于图的执行工程。它与知识图谱工程不同,后者图中的实体和关系表示数据。工作流图表示控制和状态转换。

§ 8

When a graph is worth the ceremony

Graphs are valuable when the process has meaningful branches, parallel work, approvals, recovery paths or multiple specialist agents. They are less useful when the job is simply "give one agent three tools and let it work." A graph can improve debugging, but it can also freeze assumptions too early. If the model must dynamically invent the plan, forcing every possible path into a diagram can make the system more brittle, not less

何时值得使用图工程

当流程包含有意义的分支、并行工作、审批、恢复路径或多个专业代理时,图是有价值的。当任务只是“给一个代理三个工具让它工作”时,图就没那么有用了。图可以改善调试,但也可能过早固化假设。如果模型必须动态制定计划,将每种可能路径强行塞进图里,反而会使系统更脆弱,而非更健壮。

§ 9

How the three layers work together in one real system

Consider a research-and-publishing agent responsible for producing a factual industry briefing

Notice the nesting: the graph runs inside the harness; one or more loops live inside the graph; and the harness supplies the state, tools and evaluators those loops need. The categories overlap because software layers overlap, but each still gives the team a different lever to pull when the system fails

Choose the engineering layer by diagnosing the failure

Symptom Start with Likely fix
The agent cannot access the right data or tool safely. Harness Tool contract, permissions, sandbox, context injection.
The agent forgets progress across sessions. Harness Durable state, checkpointing, progress artifacts, compaction.
The first attempt is often close but not reliable. Loop External grader, deterministic tests, feedback and bounded retry.
The agent keeps working after success or stops before proof. Loop Evidence-based terminal states and budget-aware stop rules.
Several specialists must run in a controlled order. Graph Explicit nodes, edges, routing conditions and joins.
Failures are hard to locate in a multi-step process. Graph + harness Stateful traces aligned with graph nodes and transitions.
The workflow changes too often for a fixed diagram. Simpler harness Keep control model-driven; delay graph formalization.

三层如何在一个真实系统中协同工作

考虑一个负责生成事实行业简报的研究与发布代理。

注意嵌套关系:图在工作台内部运行;一个或多个循环位于图内部;工作台为这些循环提供状态、工具和评估器。这些类别有所重叠,因为软件层本身就是重叠的,但当系统出现故障时,每一层都为团队提供了一个不同的杠杆。

通过诊断故障选择工程层

症状 从哪层入手 可能的修复
代理无法安全地访问正确的数据或工具。 工作台 工具契约、权限、沙箱、上下文注入。
代理跨会话忘记进度。 工作台 持久化状态、检查点、进度工件、压缩。
第一次尝试经常接近但不可靠。 循环 外部评分器、确定性测试、反馈和有限重试。
代理在成功后继续工作或在证明前停止。 循环 基于证据的终止状态和预算感知的停止规则。
多个专业代理必须以受控顺序运行。 显式节点、边、路由条件和汇聚。
在多步过程中难以定位故障。 图 + 工作台 与图节点和转换对齐的有状态追踪。
工作流变化太快,无法使用固定图。 更简单的工作台 保持控制由模型驱动;延迟图的形式化。
§ 10

The Expensive Mistakes Behind Weak Agent Architectures

Building a graph before understanding the work

Teams sometimes translate a business process into dozens of nodes before they have observed how a capable agent actually solves it. Start with traces from a simpler harness, then formalize the stable paths.

Letting the same model write and grade without safeguards

Self-review can help, but it is vulnerable to shared blind spots. Prefer deterministic checks where possible, separate reviewer context, and require human approval for high-impact actions.

Using "keep trying" as a loop specification

An unbounded retry loop is a cost leak. Every loop needs a measurable objective, fresh evidence, maximum attempts and a named escalation path.

Treating the harness as a dumping ground

More tools and memory are not automatically better. A crowded toolset raises selection errors, a noisy context raises confusion, and broad permissions raise risk.

Blaming the model for orchestration failures

A model cannot compensate reliably for stale state, ambiguous tool schemas, broken APIs or missing exit conditions. Improve the layer that owns the failure.

弱代理架构背后的昂贵错误

在理解工作之前构建图

团队有时会在观察一个能力足够的代理如何实际解决问题之前,就将业务过程翻译成几十个节点。应从更简单工作台的追踪开始,然后将稳定的路径形式化。

让同一个模型既写作又评分,缺乏防护

自我审查可能有帮助,但容易受到共同盲点的影响。尽可能优先使用确定性检查,分离审查者上下文,并对高影响动作要求人工审批。

将“继续尝试”作为循环规范

无界的重试循环是成本泄漏。每个循环都需要可衡量的目标、新鲜的证据、最大尝试次数和指定的升级路径。

将工作台视为垃圾倾倒场

更多的工具和记忆并不自动更好。拥挤的工具集增加选择错误,嘈杂的上下文增加混乱,广泛的权限增加风险。

将编排失败归咎于模型

模型无法可靠地补偿过时的状态、模糊的工具模式、损坏的API或缺失的退出条件。改进拥有该故障的层。

§ 11

A production-ready design checklist

  • Harness: Are tools narrow, documented and observable? Is state durable? Are permissions least-privilege? Can operators pause, inspect and resume a run?
  • Loop: What evidence proves success? What feedback is returned on failure? How many retries are allowed? What happens when the budget is exhausted?
  • Graph: Which paths must be deterministic? Where can work run in parallel? Which state is shared? Where are the human gates and recovery routes?
  • Evaluation: Can the team replay real traces, compare versions and attribute improvement to a specific change rather than intuition?
  • Operations: Are cost, latency, failure rate, intervention rate and task-level success monitored in production?

生产就绪设计检查清单

  • 工作台:工具是否狭窄、文档化且可观测?状态是否持久?权限是否最小特权?操作员能否暂停、检查和恢复运行?
  • 循环:什么证据能证明成功?失败时返回什么反馈?允许多少次重试?预算耗尽时怎么办?
  • :哪些路径必须是确定性的?哪些工作可以并行运行?哪些状态是共享的?人工门禁和恢复路线在哪里?
  • 评估:团队能否回放真实追踪、比较版本并将改进归因于特定变更而非直觉?
  • 运维:是否在生产中监控成本、延迟、失败率、干预率和任务级成功率?
§ 12

The Simplest Way to Remember the Difference

Engineering something to be a model to make it operate. Loop engineering methodology is iterative, verifiable and resumable. A complex execution path is made explicit and controllable through the use of graph engineering. None of the three is substituted by any of the others. Even if the harness has lost its state, a beautifully drawn graph is no sufficient. However, even with the best harness, if there is no evidence or stop rule, it is a waste of money! Carefully crafted loops are still hard to operate when branching, parallelism and approvals are embedded in the ad-hoc code. If these three layers are designed jointly, reliable agent systems will arise, provided that the team is aware of what each layer is supposed to solve

最简记忆法则

工作台工程是构建让模型运转的机制。循环工程的方法是迭代、可验证和可恢复的。通过图工程,复杂的执行路径变得显式且可控。三者中任何一个都不能被其他替代。即使工作台丢失了状态,一张画得漂亮的图也是不够的。然而,即使有最好的工作台,如果没有证据或停止规则,也是浪费钱!当分支、并行和审批嵌入临时代码时,精心设计的循环仍然难以操作。如果这三个层被联合设计,并且团队清楚每个层应该解决什么,可靠的代理系统就会诞生。

打开原文 ↗