上一篇随笔介绍了MCP如何连接模型应用与工具。工具接通之后,还会遇到另一个问题:一个任务做了十几步,中途窗口耗尽或服务重启,下一次怎样知道该继续哪里?
今天聊聊Agent Harness:支撑智能体执行的外围运行机制。可以把它理解为任务执行框架,负责把模型、工具、状态、验证和退出条件组织起来。本文聚焦工程思路,不绑定某个SDK版本;资料于2026年9月20日核对,引用的长期任务实践文章发布于2025年11月26日。
一、模型之外,还需要哪些东西
模型根据当前信息提出下一步行动;工具负责执行查询、测试等具体操作;Harness决定怎样提供上下文、检查调用、记录结果以及恢复任务。
例如,一个“检查接口文档与实现是否一致”的学习任务,可以拆成读取文档、提取接口、运行检查、整理差异四个环节。模型擅长理解文字和提出检查点;是否确实运行过检查、哪些接口已经覆盖,则需要外部状态记录。
MCP解决工具接入中的协议问题,Harness处理整个任务怎么运行。使用MCP不会自动得到任务恢复、预算管理或结果验证,这些能力需要应用自己实现或由所选框架提供。
二、让状态离开聊天记录
长对话里的一句“差不多完成了”很难供程序判断。更实用的方式是让每个任务带有明确状态和证据位置。
下面是自定义的教学数据格式,不是任何厂商API规范,其中的路径和任务均为虚构示例:
{
"task_id": "demo-api-check-01",
"status": "in_progress",
"steps": [
{"id": "read_docs", "status": "done", "evidence": "notes/docs.json"},
{"id": "check_routes", "status": "pending", "evidence": null}
],
"next_step": "check_routes",
"max_attempts": 3
}
这份记录回答三个问题:已经做过什么、依据放在哪里、下一步做什么。恢复时先读取记录,再检查证据文件与当前环境是否仍然一致。记录中写着done,只代表上次标记成功,不能保证外部系统一直保持原样。
对于会修改外部状态的操作,还需要幂等性:同一个任务重试时,不能重复创建订单或重复发送消息。可以为操作设置稳定的请求标识,并在服务端记录处理结果;具体做法取决于业务系统。

三、恢复任务是一套明确步骤
可以设计这样的运行顺序:加载任务记录,检查运行环境,选一个未完成步骤,执行并收集证据,通过验证后保存状态,然后再处理下一步。

图中Agent组织执行循环,Tools负责操作,Verify检查结果,State保存进度;预算耗尽或遇到阻塞时停止。
Anthropic的长期运行智能体实践介绍了用初始化环境、功能清单、进展记录和验证帮助后续会话继续工作的方式。这里借鉴“把进展留下来”的思路,具体状态格式与下面的演示由本文自行设计。
关键细节是保存的时机。假如工具执行成功后,状态保存失败,恢复时就可能再次执行。因此,不能只依靠一份本地文本消除重复动作。涉及真实业务写入时,应考虑事务、服务端幂等键或执行结果查询;本例仅用于理解流程。
四、用一个小程序检查完成条件
下面的Python示例只演示状态判定,不调用模型、不联网、不执行真实工具。它故意准备两个案例:有证据且验证通过,以及只有成功标记却缺少证据。
def can_finish(step):
return (
step["status"] == "done"
and bool(step.get("evidence"))
and step.get("verified") is True
)
cases = [
{"status": "done", "evidence": "report.json", "verified": True},
{"status": "done", "evidence": None, "verified": True},
]
for index, step in enumerate(cases, start=1):
print(f"case-{index}: {can_finish(step)}")
输出:
case-1: True
case-2: False
这个程序检查的是结构化字段。实际系统还要检查report.json是否存在、属于本次任务、内容是否满足验收要求。不能把“有一个文件名”当成真实验证,也不能让模型随意填一个verified就决定任务完成。
在接口检查场景里,较好的证据可能包括运行命令、目标版本、响应摘要以及失败用例。敏感响应应脱敏,日志也需要控制保存范围。
五、把自主程度放在合适的位置
对于步骤固定的工作,先使用可观察的工作流很容易定位问题。只有路径确实依赖现场信息时,再让模型选择下一步。Anthropic的智能体工程介绍区分了预定义流程与模型动态决定路径的系统;这是一种设计视角,不是行业唯一分类。
| 执行中断后从哪里继续 | 持久化步骤与证据索引 |
| 同一操作重复触发 | 幂等键、结果查询与去重 |
| 工具一直失败 | 超时、有限重试与明确阻塞状态 |
| 模型过早宣布完成 | 程序验证与可检查的验收条件 |
| 工具返回不可信文本 | 将返回值当数据,保持指令边界 |
这张表是本文的实现检查清单,不意味着加上这些字段就能得到可靠的生产系统。可以先选一个低风险、可重复验证的小任务,观察失败发生在哪一步,再完善机制。
六、怎样评价一次运行
完成率需要对应明确任务集合;正确率需要可以复查的结果标准;成本与耗时需要区分成功运行和多次重试。只展示一次顺利演示,很难判断系统能否稳定工作。
还可以记录恢复次数、重复操作次数,以及“宣布完成但验收失败”的次数。这些指标能帮助判断状态设计是否真正发挥作用。本文没有开展产品性能比较,因此不提供任何提升比例或榜单结论。
在个人学习项目里,可以从三个案例开始:正常完成、工具返回失败、保存状态后重新启动。先把这三种路径讲清楚,再扩展到并行任务和更多工具。
七、🧠 思维导图
#mermaid-svg-BzmzQtLl0QZ9Io2h{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-BzmzQtLl0QZ9Io2h .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-BzmzQtLl0QZ9Io2h .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-BzmzQtLl0QZ9Io2h .error-icon{fill:#552222;}#mermaid-svg-BzmzQtLl0QZ9Io2h .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-BzmzQtLl0QZ9Io2h .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-BzmzQtLl0QZ9Io2h .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-BzmzQtLl0QZ9Io2h .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-BzmzQtLl0QZ9Io2h .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-BzmzQtLl0QZ9Io2h .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-BzmzQtLl0QZ9Io2h .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-BzmzQtLl0QZ9Io2h .marker{fill:#333333;stroke:#333333;}#mermaid-svg-BzmzQtLl0QZ9Io2h .marker.cross{stroke:#333333;}#mermaid-svg-BzmzQtLl0QZ9Io2h svg{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-BzmzQtLl0QZ9Io2h p{margin:0;}#mermaid-svg-BzmzQtLl0QZ9Io2h .label{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;color:#333;}#mermaid-svg-BzmzQtLl0QZ9Io2h .cluster-label text{fill:#333;}#mermaid-svg-BzmzQtLl0QZ9Io2h .cluster-label span{color:#333;}#mermaid-svg-BzmzQtLl0QZ9Io2h .cluster-label span p{background-color:transparent;}#mermaid-svg-BzmzQtLl0QZ9Io2h .label text,#mermaid-svg-BzmzQtLl0QZ9Io2h span{fill:#333;color:#333;}#mermaid-svg-BzmzQtLl0QZ9Io2h .node rect,#mermaid-svg-BzmzQtLl0QZ9Io2h .node circle,#mermaid-svg-BzmzQtLl0QZ9Io2h .node ellipse,#mermaid-svg-BzmzQtLl0QZ9Io2h .node polygon,#mermaid-svg-BzmzQtLl0QZ9Io2h .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-BzmzQtLl0QZ9Io2h .rough-node .label text,#mermaid-svg-BzmzQtLl0QZ9Io2h .node .label text,#mermaid-svg-BzmzQtLl0QZ9Io2h .image-shape .label,#mermaid-svg-BzmzQtLl0QZ9Io2h .icon-shape .label{text-anchor:middle;}#mermaid-svg-BzmzQtLl0QZ9Io2h .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-BzmzQtLl0QZ9Io2h .rough-node .label,#mermaid-svg-BzmzQtLl0QZ9Io2h .node .label,#mermaid-svg-BzmzQtLl0QZ9Io2h .image-shape .label,#mermaid-svg-BzmzQtLl0QZ9Io2h .icon-shape .label{text-align:center;}#mermaid-svg-BzmzQtLl0QZ9Io2h .node.clickable{cursor:pointer;}#mermaid-svg-BzmzQtLl0QZ9Io2h .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-BzmzQtLl0QZ9Io2h .arrowheadPath{fill:#333333;}#mermaid-svg-BzmzQtLl0QZ9Io2h .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-BzmzQtLl0QZ9Io2h .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-BzmzQtLl0QZ9Io2h .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-BzmzQtLl0QZ9Io2h .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-BzmzQtLl0QZ9Io2h .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-BzmzQtLl0QZ9Io2h .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-BzmzQtLl0QZ9Io2h .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-BzmzQtLl0QZ9Io2h .cluster text{fill:#333;}#mermaid-svg-BzmzQtLl0QZ9Io2h .cluster span{color:#333;}#mermaid-svg-BzmzQtLl0QZ9Io2h div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-BzmzQtLl0QZ9Io2h .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-BzmzQtLl0QZ9Io2h rect.text{fill:none;stroke-width:0;}#mermaid-svg-BzmzQtLl0QZ9Io2h .icon-shape,#mermaid-svg-BzmzQtLl0QZ9Io2h .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-BzmzQtLl0QZ9Io2h .icon-shape p,#mermaid-svg-BzmzQtLl0QZ9Io2h .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-BzmzQtLl0QZ9Io2h .icon-shape .label rect,#mermaid-svg-BzmzQtLl0QZ9Io2h .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-BzmzQtLl0QZ9Io2h .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-BzmzQtLl0QZ9Io2h .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-BzmzQtLl0QZ9Io2h :root{–mermaid-font-family:\”trebuchet ms\”,verdana,arial,sans-serif;}
Agent Harness
模型与工具
持久化任务状态
证据与验收
中断恢复与幂等
预算与退出条件
可观察的执行记录
八、回顾今天的内容
总结要点
Harness把模型调用放进可持续执行的任务流程。关注状态、工具和验证之间的关系,可以更清楚地理解智能体应用的工程工作。
恢复能力来自可检查的记录和明确的执行边界。任务状态、结果证据与幂等设计需要一起考虑。
可靠性需要通过失败案例验证。先让小任务能够正确完成、明确失败并恢复,再逐步扩大自主范围。
下一篇随笔继续关注AI应用中的上下文管理,看看哪些信息值得带入下一次模型调用。
👉 如果你觉得这篇文章对你有所帮助,欢迎点赞、收藏、分享!😊
网硕互联帮助中心








评论前必须登录!
注册