有三个问题:
- 提示词工程 Prompt Engineering 是什么?
- 上下文工程 Context Engineering 是什么?
- Harness Engineering 是什么?
提示词工程 Prompt Engineering 是什么?
Prompt(提示词)是与AI大模型(LLM)交互的输入。
不管是与AI大模型的单轮交互,还是多轮交互,本质上都是输入Promp给AI大模型,然后得到AI大模型输出的结果。
单轮交互与多轮交互的区别就在于,多轮交互会把会话的历史消息作为Promp的输入给了AI大模型。
AI大模型,本质上就是一个包含训练好的神经网络的各个神经元的权重文件,作为输入Prompt能激发对应的这个神经网络内对应的神经元,然后得到对应的输出。
即Prompt设计的好坏,直接决定了AI大模型输出结果的好坏。
所以你想让AI大模型做好什么,那就得把任务表达对,且尽可能清晰和详细。
那么一个好的Prompt一般有哪些组成部分?
- “角色设定”
- “语气和约束”
- “任务明细”: 将复杂请求拆解为有序步骤。
- “给出与预期输出格式匹配的范例”
毛坯与精装的Prompt:
# Naive approach
prompt = "Fix the bug in my code"
# Prompt-engineered approach
prompt = """You are a senior Python engineer reviewing a production bug.
Context:
- The bug causes a KeyError on line 47 of orders.py
- It only occurs during weekend batch processing
- The system uses PostgreSQL with a read replica
Your task:
1. First, identify the root cause without changing any code
2. Describe what data condition triggers the error
3. Propose a fix that maintains backward compatibility
4. List any tests that should be added
Do not modify any files until I confirm your diagnosis."""
上下文工程 Context Engineering 是什么?
AI大模型的输入是有上限的,这个上限也叫上下文窗口。既然有上限,那么就需要管理 AI大模型的输入。
Context Engineering(上下文工程)是建立在Prompt Engineering之上的,维护着Agent每次调用AI大模型的输入的信息。
Agent(智能体)的组成:
- AI大模型(LLM):即推理能力。
- 感知(Perception):即
触发,传感器数据、用户输入、定时任务等等。 - 记忆(Memory): 即存储。分为:短期记忆(当前会话的内容) & 长期记忆(历史会话的内容,知识库。。。)。
- 决策(Decision Making):即一些规则,处理流程的编排。
- 行动(Action): 即
执行层,可以输出文本、调用API、发送指令等等。
不同的Agent的情况不同,Context Engineering(上下文工程)维护的信息一般有哪些:
- Agent的基本信息(Agents.md,比如:“角色设定”、“语气和约束”等等);
- 可以调用的工具(比如:文件、skill、MCP) & 调用记录和结果;
- 当前会话的消息;
- 命中的历史消息 or 知识,即长期记忆的;
Harness Engineering 是什么 ?
当Agent去做一个长任务时候,结果并不是稳定的,也就是在做的过程中会由于一些动态信息的影响(用户输入的信息、工具调用的结果、任务的难易等等)而导致走偏了,所以就需要及时的去纠正它。
Harness --- 驾驭 Harness Engineering --- 驾驭AI大模型的工程,即在
上下文工程的基础上,通过设定规则去纠正,让Agent能稳定的执行任务。
Harness Engineering 核心思想是:AI的能力极限不只在模型本身,更在于如何设计和管理模型运行的外部系统。即为AI大模型打造的“工业级操作系统”。
Harness Engineering 包含的核心功能模块:
- “游戏规则”: 即制定权限和边界。比如:操作权限控制、一些代码操作需要审核(可以让其它Agent去审核)。
- “标准工具箱”: 标准化的工具。比如:文件读写、搜索。
- “操作日志”: 记录AI的每一步操作。可追溯、可审计。
## 生产 TypeScript monorepo 的 Harness 设置清单
### Prompt 层
- [ ] System prompt defines role, scope, and what agent should NOT do
- [ ] Output format is constrained for structured responses
### Context 层
- [ ] AGENTS.md under 60 lines, universally applicable only
- [ ] Skills for: debugging, refactoring, PR creation, dependency auditing
- [ ] MCP servers: only 2-3 active at a time, disable unused ones
- [ ] Memory: conversation buffer + structured long-term rules store
### Harness 层
- [ ] Pre-commit hook: biome + typecheck
- [ ] PostToolUse hook: surface linter errors to agent on every file write
- [ ] Stop hook: run changed test files only, return errors
- [ ] Coverage hook: alert if coverage drops below threshold
- [ ] Loop detection: flag if same file edited 3+ times in same session
- [ ] Sub-agent patterns defined for: research, codebase tracing, QA
- [ ] Escalation rule: if blocked for 3+ tool calls, stop and ask
Sub-agent 是个好东西
Sub-agents 是上下文的防火墙。Chroma 的研究表明模型性能随上下文长度增长而下降,当问题与相关上下文之间的语义相似度低时,下降更为陡峭。Sub-agents 从结构层面解决了这一问题:每个 Sub-agent 获得一个全新的、小的、高相关性的上下文窗口,父 Agent 只接收压缩后的结果——中间的工具调用、grep 输出、文件读取全部隔离在 Sub-agent 内部。
多次(比如:超过 15 次)工具调用才能解决的任务,可直接交给 Sub-agent。
参考
-
Prompt、Context、Harness:AI Agent 工程的三层架构解析 https://developer.aliyun.com/article/1725017
-
Harness Engineering是什么?和提示词工程和上下文工程有什么关系?https://www.bilibili.com/video/BV1dpQTB3EXg/?spm_id_from=333.1007.top_right_bar_window_custom_collection.content.click&vd_source=539dc150870d618de4193749eb731a05
注意:本文归作者所有,未经作者允许,不得转载