Harness Engineering: How to Build AI Workflows That Never Fall ApartHarness 工程:如何搭建永不散架的 AI 工作流
Opening开篇
Most people respond to a broken AI output by rewriting the prompt
Then they switch models
Then they upgrade to a larger context window
AI 输出出了问题,大多数人的第一反应是改提示词。
不行就换模型。
再不行就升级到更大的上下文窗口。
The model still forgets instructions
It still recommends the wrong tool
It still skips verification
It still claims success on broken work
可模型还是会忘掉指令,
还是会推荐错的工具,
还是会跳过验证,
活儿明明没干好,还是会说「搞定了」。
The problem sits outside the model weights
The problem sits in the execution environment
That environment is the harness
Designing it is harness engineering
问题不在模型权重里,
而在模型运行的环境里。
这个环境就叫 harness,
设计这套环境,就是 harness 工程(harness engineering)。
“Of course, you need an interface, you need a harness to use them”
「当然,你需要一个界面,需要一个 harness,才能把它们用起来。」
The model acts as a reasoning engine
A model can predict the next logical step
It cannot build a stable operating system on its own
模型扮演的是推理引擎。
它能推断出下一步合乎逻辑的动作,
但靠它自己,搭不出一套稳定的操作系统。
The harness controls what the model reads, what tools it can trigger, what data survives context resets, and when the execution must halt
harness 决定了这些事:模型读什么、能调用哪些工具、上下文重置后哪些数据还留着,以及什么时候必须停下来。
Prompt engineering adjusts the wording
Harness engineering builds the infrastructure where that wording executes
提示词工程调的是措辞;
harness 工程搭的是让这些措辞真正跑起来的基础设施。
Put a frontier model in an empty chat box, and it generates casual prose
Put that identical model in a repository with terminal access, automated test suites, project maps, and isolated workspaces, and it ships working software
把一个最前沿的模型丢进空白聊天框,它只会写些闲聊式的文字。
把同一个模型放进代码仓库,给它终端权限、自动化测试、项目地图和隔离的工作区,它就能交付真正能跑的软件。
The weights stay identical
The harness changes everything
权重一模一样,
变的是 harness,而它改变了一切。
OpenAI recorded the identical pattern when training agents on Codex
Their early runs failed because the environment was underspecified
The engineering team avoided asking the model to try harder
They built explicit capability checks and mechanical boundaries
OpenAI 在 Codex 上训练智能体时,也记录到了完全相同的规律。
他们早期的运行之所以失败,是因为环境没定义清楚。
工程团队没有去要求模型「再努力一点」,
而是搭了明确的能力检查和机械式的边界。
When an agent fails repeatedly, stop editing adjectives in the system prompt
Inspect the operating system around the model
智能体反复失败时,别再去改系统提示词里的形容词了,
去检查模型周围的那套「操作系统」。
The Three Layers of a Reliable AI System可靠 AI 系统的三层结构
Builders often treat prompt chains, agent loops, and multi-agent graphs as competing ideas
They represent three distinct layers of one system
很多开发者把提示词链、智能体循环和多智能体图当成互相竞争的思路。
其实它们是同一个系统里的三个不同层次。
1. The Harness Layer (Environment and Memory)1. Harness 层(环境与记忆)
The harness consists of everything existing outside model weights
It manages filesystem access, environment variables, authentication tokens, and durable files
harness 指的是模型权重之外的一切。
它管着文件系统访问、环境变量、身份认证令牌,以及需要长期保存的文件。
Andrej Karpathy established the defining mental model:
The model is the CPU
The context window is the RAM
A raw CPU without storage or an operating system crashes instantly
Andrej Karpathy 给出了一个奠定基调的思维模型:
模型是 CPU,
上下文窗口是内存(RAM)。
一颗光秃秃的 CPU,没有存储、没有操作系统,一启动就会崩。
When users dump twenty pages of background text into a single prompt, they flood the RAM
The model loses attention on critical rules
用户把二十页背景资料一股脑塞进一条提示词,就等于把内存撑爆了,
模型也就顾不上那些关键规则了。
A clean harness stores project knowledge in external markdown files
It loads only the exact snippet needed for the immediate step
干净的 harness 会把项目知识存放在外部的 Markdown 文件里,
每一步只加载眼下正好需要的那一小段。
2. The Loop Layer (Evidence and Convergence)2. 循环层(证据与收敛)
A model outputting code or text needs automated validation
Asking the model "Are you sure this is right?" creates a circular hallucination loop
The model evaluates its own probabilistic prediction and confirms its initial bias
模型输出代码或文字之后,需要自动化的验证。
去问模型「你确定这是对的吗?」,只会掉进一个自我循环的幻觉圈。
模型评估的是它自己的概率预测,结果只会坐实它一开始的偏见。
The loop layer introduces objective sensors:
循环层引入的是客观的「传感器」:
- Test runners (pytest, vitest, cargo test)测试运行器(pytest、vitest、cargo test)
- Format validators (JSON schema checkers, linters)格式校验器(JSON Schema 检查器、代码规范检查工具 linter)
- Execution budgets (maximum four attempts before human review)执行预算(最多尝试四次,之后转人工审查)
The model suggests the correction
The harness runs the test and supplies the raw terminal output
Execution continues only when mechanical evidence confirms the fix
模型负责提出修改方案,
harness 负责跑测试,并把终端的原始输出交给模型。
只有机械化的证据确认修复有效,流程才继续往下走。
3. The Graph Layer (Work Allocation)3. 图层(任务分配)
Long tasks crumble when forced into a single endless conversation thread
The graph layer distributes work across isolated execution contexts
长任务硬塞进一条没完没了的对话里,迟早会垮。
图层会把工作分配到彼此隔离的多个执行上下文里。
One worker analyzes the task and writes an implementation blueprint
A second worker writes the code in an isolated directory branch
A third adversarial worker reviews the diff against project requirements
第一个工作者分析任务,写出实现蓝图;
第二个工作者在隔离的目录分支里写代码;
第三个是专门「唱反调」的工作者,对照项目需求审查代码改动(diff)。
Context stays clean
No single prompt carries the burden of the entire project lifecycle
上下文保持干净,
没有哪一条提示词需要扛起整个项目的生命周期。
The 5 Core Jobs of a Personal AI Harness个人 AI Harness 的 5 项核心职责
You do not need a massive enterprise platform to apply harness engineering
A personal harness requires five mechanical rules in your everyday workflows
想用上 harness 工程,并不需要庞大的企业级平台。
个人 harness 只要求你在日常工作流里落实五条机械式的规则。
1. Turn Every Request into a Bounded Contract1. 把每个请求都变成有边界的「合同」
Conversational prompts allow models to silently redefine success
When an ambiguous prompt encounters friction, the model solves an easier adjacent task and reports victory
聊天式的提示词,会让模型悄悄改写「成功」的定义。
含糊的提示词一遇到阻力,模型就会转去解决一个更简单的相邻问题,然后宣布大功告成。
A production contract sets hard boundaries before generation starts:
一份生产级的合同,会在开始生成之前就划好硬性边界:
- Deliverable: Exact file name and format交付物:确切的文件名和格式
- Constraints: Dependencies allowed, style requirements, token budget约束:允许使用的依赖、风格要求、token 预算
- Non-Goals: Explicit list of items the model must avoid touching非目标:明确列出模型不许碰的东西
- Acceptance Criteria: The exact condition required to close the task验收标准:任务算完成必须满足的确切条件
Without an explicit contract, the model decides when it finishes
With a contract, the criteria decides when the model finishes
没有明确的合同,什么时候算做完由模型说了算;
有了合同,由验收标准说了算。
2. Provide a Map, Not an Encyclopedia2. 给地图,别给百科全书
Context degradation strikes every frontier model over long horizons
Dumping an entire project documentation file into chat burns tokens and dilutes focus
任务一拉长,每个前沿模型都会出现上下文退化。
把整份项目文档倒进聊天框,既烧 token,又分散注意力。
Elite builders create a compact root map
The map outlines where documentation, schemas, and source files live
The model scans the map, selects the single relevant file, and reads that file alone
高手会做一份精简的根目录地图,
标明文档、数据结构(schema)和源文件分别放在哪儿。
模型先扫一遍地图,挑出唯一相关的那个文件,只读那一个。
A compact map preserves working memory
A giant prompt dump exhausts it
精简的地图能保住工作记忆,
一大坨提示词只会把它耗光。
3. Externalize Memory into Durable Files3. 把记忆外置到持久文件里
Chat history serves as temporary scratch space
The conversation thread is not a database
When a session runs long, attention fades and context windows fill up
聊天记录只是临时草稿纸,
对话线程不是数据库。
会话一长,注意力就会涣散,上下文窗口也会被塞满。
Store architecture decisions, user preferences, and unresolved bugs in a dedicated project file
In Claude Code and Cursor, this file is CLAUDE.md or .cursorrules
把架构决策、用户偏好和还没解决的 bug,存进一个专门的项目文件里。
在 Claude Code 和 Cursor 里,这个文件就是 CLAUDE.md 或 .cursorrules。
When starting a fresh session, the model reads the file and resumes immediately
The work survives chat wipes, rate limits, and model switches
开新会话时,模型读一遍这个文件,马上就能接着干。
聊天记录被清空、撞上速率限制、换了模型,工作进度都不会丢。
4. Build Sensors Before Granting Autonomy4. 先装传感器,再放权
An agent cannot repair errors it cannot observe
Telling a model "write bug-free code" changes nothing
Providing a command that outputs exit code zero or a stack trace gives the model ground truth
智能体看不到的错误,它就修不了。
跟模型说「写出没有 bug 的代码」,什么也改变不了。
给它一条命令,运行后要么返回退出码 0、要么吐出报错堆栈,模型才有了真实依据。
Sensors turn subjective quality into verifiable evidence:
传感器能把主观的「质量好坏」变成可验证的证据:
- Compilers and type checkers that fail on syntax errors遇到语法错误就报错的编译器和类型检查器
- Automated test scripts that verify data transformations验证数据转换是否正确的自动化测试脚本
- Checklist validators that confirm all required fields exist确认所有必填字段都在的清单校验器
The model creates the artifact
The sensor generates the evidence
The harness decides if the run passes
模型负责产出成品,
传感器负责生成证据,
harness 负责判定这次运行是否通过。
5. Apply the Dual-Encoding Rule5. 执行「双重编码」规则
Writing a rule in plain English leaves room for probabilistic failure
The model reads the instruction, parses twenty other constraints, and skips the rule under load
只用大白话写一条规则,总会留下概率性失败的空间。
模型读了这条指令,又要消化另外二十条约束,一忙起来就把它跳过了。
Dual-encoding solves this permanently:
First, explain the rule in text so the model understands the goal
Second, place a mechanical gate that blocks execution if the rule breaks
双重编码能一劳永逸地解决这个问题:
第一,用文字解释这条规则,让模型明白目的是什么;
第二,设一道机械闸门,规则一旦被打破就阻止执行。
If an agent must avoid deleting files, tell it in the prompt
Then restrict its terminal permissions so rm -rf throws an operating system error
比如智能体不能删文件,那就先在提示词里告诉它,
再限制它的终端权限,让 rm -rf 直接被操作系统报错拦下。
The prompt guides the intention
The mechanical gate protects the system
提示词引导意图,
机械闸门守住系统。
How to Set Up a Personal Micro-Harness如何搭建一个个人「微型 Harness」
Here is a five-minute harness structure you can paste into any repository, Claude Project, or Cursor workspace
Save this content as HARNESS.md in your project root:
下面是一个五分钟就能搭好的 harness 结构,可以直接粘贴到任何代码仓库、Claude Project 或 Cursor 工作区里。
把下面的内容保存为项目根目录下的 HARNESS.md:
# Project Operating Contract
## Operating Boundaries
- Allowed scope: Modify only files explicitly assigned in the current task
- Forbidden actions: Never delete existing tests, never add unverified dependencies
## Project Map
- /src: Application source code
- /tests: Validation test suites
- /docs/decisions.md: Durable log of accepted architectural choices
## Verification Protocol
Before marking any task complete, run these checks in order:
1. Run local linter: `npm run lint`
2. Run test suite: `npm test`
3. Verify output format matches the requested schema
## Failure Escalation
If a test fails twice on the same error:
- Stop autonomous editing
- Print the exact error log
- State the proposed fix and wait for user confirmation# 项目运行合同
## 操作边界
- 允许范围:只修改当前任务中明确指定的文件
- 禁止行为:永远不要删除现有测试,永远不要添加未经验证的依赖
## 项目地图
- /src:应用源代码
- /tests:用于验证的测试套件
- /docs/decisions.md:已采纳架构决策的持久记录
## 验证流程
在把任何任务标记为完成之前,按顺序运行以下检查:
1. 运行本地 linter:`npm run lint`
2. 运行测试套件:`npm test`
3. 确认输出格式符合要求的 schema
## 失败升级
如果同一个错误让测试连续失败两次:
- 停止自主修改
- 打印完整的错误日志
- 说明建议的修复方案,等待用户确认This short file replaces twenty lines of repetitive prompting in every chat
It establishes project scope, controls file access, and forces mechanical verification
这个小文件能替你省掉每次聊天都要重复敲的二十行提示词。
它划定项目范围、控制文件访问,并强制执行机械化的验证。
The Two-Pass Prompting Workflow两轮提示工作流
When working on difficult reasoning tasks in standard chat interfaces, use a two-pass harness loop
Most users ask for an answer and accept the first draft
Production workflows split generation and critique into separate passes
在普通聊天界面里处理高难度推理任务时,可以用一个「两轮」harness 循环。
大多数人问出答案,就直接接受第一版草稿;
生产级的工作流则会把「生成」和「挑错」拆成两轮分开做。
Pass 1: The Execution Pass第一轮:执行
Draft the technical specification for [Feature X].
Follow the constraints listed in HARNESS.md.
Do not evaluate your own output yet.
Output the raw draft and state all assumptions made.为【功能 X】起草技术规格说明。
遵守 HARNESS.md 中列出的约束。
先不要评估你自己的输出。
输出原始草稿,并列出你做出的所有假设。Pass 2: The Adversarial Review Pass第二轮:对抗式审查
Review the draft above as a skeptical senior reviewer.
Check against these specific failure points:
1. Are edge cases handled when input arrays are empty?
2. Does the logic introduce extra dependencies?
3. Does the solution violate any constraint from HARNESS.md?
List every failure point found.
Then output the revised version fixing those gaps.请以一位持怀疑态度的资深审查者身份,审查上面的草稿。
对照以下具体失败点逐一检查:
1. 输入数组为空时,边界情况处理了吗?
2. 逻辑有没有引入额外的依赖?
3. 方案有没有违反 HARNESS.md 中的任何约束?
列出找到的每一个失败点。
然后输出补上这些漏洞后的修订版。Separating the creator role from the reviewer role cuts hallucinated claims in half
The model cannot defend its own errors when explicitly instructed to attack them
把创作者和审查者的角色分开,能让幻觉式的断言减少一半。
当你明确要求模型去攻击自己的错误时,它就没法再替这些错误辩护。
Why the Harness Compounds为什么 Harness 会产生复利
Models update every few months
Prompts that work today often produce different outputs on the next model checkpoint
Harness engineering preserves your engineering effort
模型每隔几个月就更新一次。
今天好用的提示词,换到下一个模型版本上,输出往往就变了样。
harness 工程能让你的工程投入保值。
When Anthropic releases an updated Claude or OpenAI launches a new reasoning model, your prompts might require minor adjustments
Your test suites, project maps, permission boundaries, and durable state files remain intact
They work on the new model immediately
Anthropic 发布新版 Claude、或者 OpenAI 推出新的推理模型时,你的提示词也许要小改一下,
但你的测试套件、项目地图、权限边界和持久状态文件都原封不动,
换上新模型立刻就能用。
The model provides raw cognitive compute
The harness provides the rails that turn compute into reliable execution
模型提供原始的认知算力,
harness 提供轨道,把算力变成可靠的执行。
Stop tweaking adjectives in your prompts
Start building the harness around them
别再折腾提示词里的形容词了,
动手在它们外面搭 harness 吧。