Context Engineering: From Prompt to Skill
Why now
2025 年 6 月,Drew Breunig 在他的博客写了 《How Contexts Fail》。这篇文章在 Twitter/X 的 AI 圈被转发了几千次 — 因为它终于把大家在生产 LLM 应用中遇到的一个老问题说清楚了:
不是 prompt 写得巧不巧,是 模型看到了什么。
如果你在生产环境跑过 agent,一定撞过 4 类失败:模型写着写着忘了原始任务 (distraction)、它引用了幻觉里的内容当事实 (poisoning)、它在多个文档间串台 (confusion)、它从两个相互冲突的源里"二选一"但选错了 (clash)。这些不是 prompt 写得不够好的问题 — 是 context 设计的问题。
2025 年下半年起,Context Engineering 开始取代 Prompt Engineering 成为 LLM 应用的新主线。Anthropic 在他们的工程博客写了 《Effective Context Engineering for Agents》,LangChain 出了 LangGraph 来管理 state,Drew Breunig、Hamel Husain、Matt Pocock、Stanford DSPy 团队在不同方向讲同一件事。这不是 single-author blog — 这是一个共识正在成形。
这篇文章是我读完 14 篇核心论文 + Drew Breunig 全部博客 + Anthropic / LangChain / LlamaIndex 三个开源项目的代码之后,写的一个综述。它回答 4 个问题:
- Context Engineering 是什么 — 跟 Prompt Engineering 有什么不一样
- 如何做好 — 8 个 best practice,按影响度排序
- 项目中怎么应用 — 5 种典型项目 × 必做映射
- 效果到底多大 — 单点 vs 复合的量化数据
如果你在做 LLM 应用 — 无论 ChatGPT 替代 / RAG / agent / multi-agent — 里面的 5 个必做至少要做 1 个,最好做 3 个。复合效应是单点的 2-4 倍。
1. Context Engineering 是什么
Prompt vs Context
Prompt Engineering 关心"怎么说" — 你怎么写指令、怎么放示例、怎么调 tone。Context Engineering 关心"说什么" — 模型看到的整个输入:系统提示 + 用户消息 + 工具调用结果 + 历史消息 + RAG 检索 + long-term memory + external state。
Anthropic 在他们的工程博客里讲:"Good context engineering is the discipline of curating what goes into the model's limited attention budget." — 不是 clever instructions,是 attention budget 的纪律。
这个区别在单轮问答时不明显。但你一旦做长程 agent —— 跑 10+ 轮 + 工具调用 + RAG 检索 —— 你立刻撞到这个问题。模型不笨、prompt 也写得不差,但 context 里乱七八糟(重复的内容、过时的 memory、不相关的文档、工具调用的 raw JSON),模型就脱轨了。
Drew Breunig 的 4 类失败模式
在《How Contexts Fail》里,Breunig 把 context 失败分成 4 类。这是任何做 agent 的人都应该背下来的分类法:
| # | 失败模式 | 症状 | 修复 |
|---|---|---|---|
| 1 | Context Poisoning | 幻觉内容写进 context → 模型反复引用错误 → 越走越偏 | Read-before-write · 验证工具输出 · 过滤未验证内容 |
| 2 | Context Distraction | Context 越来越长 → 模型被无关信息干扰 → 忘了原始任务 | Context summarization · 分阶段 reset · Note-taking |
| 3 | Context Confusion | Context 里多个无关 task / 文档混在一起 → 模型用错上下文 | 命名空间隔离 · 文档清晰归属 |
| 4 | Context Clash | Context 里不同来源的信息直接矛盾 → 模型不知道信哪个 | 多源验证 · 来源优先级 · 显式冲突检测 |
这 4 类里,1 和 4 最致命,因为它们会"骗"模型 — 模型以为它对了,其实已经彻底走偏。2 和 3 是渐进式的退化,容易发现但难修复。
2. 如何做好:8 个 best practice
下面 8 个 best practice 按"影响度 + 实现成本 + 可替代性"3 维加权排序。3 个 必做(任何 LLM 项目) + 5 个 nice-to-have(按项目复杂度递进)。
🥇 #1 Note-taking (scratchpad) — Drew Breunig 推
Agent 每走一步,写一段笔记到一个 scratchpad 上。每步 reset 或 rolling summarization 整个 scratchpad。下次 LLM 调用时,只喂 scratchpad,不喂整个 message history。这直接解决 context distraction。
LangGraph 的 AgentState 就是工业级实现:
from langgraph.graph import StateGraph, END
from typing import TypedDict, Annotated
import operator
class AgentState(TypedDict):
messages: Annotated[list, operator.add]
notes: Annotated[list, operator.add]
scratchpad: str # rolling summary — this is the CONTEXT
关键不是 messages —— 是 scratchpad。LangGraph 让你把这两件事分开管理。
🥈 #2 Read-before-Write — 防 poisoning
Agent 在写到 scratchpad / 工具输出前,先 read & verify。让 LLM 自己 sanity-check 它即将写下的内容。这不是 cost optimization — 是 context 质量控制。
def safe_tool_call(tool_fn, args):
preview = tool_fn.dry_run(args)
is_valid = llm.invoke(f"Is this sensible?\nResult: {preview}")
if "YES" in is_valid.upper():
return tool_fn(args)
return retry_with_adjustment(tool_fn, args, is_valid)
🥉 #3 Evals (test-driven) — Hamel Husain 推
在写 prompt 之前,先写 eval cases。5-10 个典型问题 + 期望回答范围。跑 baseline (default context) → 拿到 pass rate。迭代 prompt + context 直到 pass rate > 80%。这是把"context engineering"从玄学变成工程的关键。
Hamel Husain 的《Your AI Product Needs Evals》是这个范式的圣经。LangSmith / Braintrust / Langfuse 是工具支持。
4 个 nice-to-have · 越复杂项目越需要
#4 Skill 系统化 (Matt Pocock):把"一段 prompt + 几个示例 + 几个约束"打包成一个 skill,跨 session / 跨 agent 复用。这避免了散 prompt。
#5 RAG (LlamaIndex):把大文档 / 大数据库切成小块,根据 query 动态检索相关 context。这避免了"context 装不下整个数据库"。
#6 Tool call schema (Anthropic):每个 tool 有清晰的 JSON schema / Pydantic model,模型知道返回什么。这避免了 context confusion。
#7 Multi-Agent 验证 (Jeff Dean):多个 agent 试不同路径 + 一个 evaluator agent 选 best。这增加了可靠性但增加了 token 成本。适用于可靠性要求 > 95% 的场景。
#8 DSPy 自动 prompt 优化 (Stanford):用 optimizer 自动搜 prompt / context 配置。需要先有 eval 才能 opt,所以排在最后。
3. 项目中如何应用
5 种典型项目 × 必做映射。原则:Evals 永远是 Day 1,其他根据项目复杂度递进。
| 项目类型 | Note-taking | Read-before-Write | Evals | RAG | Skills | 何时应用 |
|---|---|---|---|---|---|---|
| ① 单轮问答 | — | — | 必做 | — | — | Day 1 |
| ② RAG 文档问答 | — | — | 必做 | 必做 | — | Day 1 |
| ③ 多步 Agent | 必做 | 必做 | 必做 | 可选 | — | Day 1 |
| ④ Multi-Agent | 必做 | 必做 | 必做 | 可选 | 必做 | Week 2+ |
| ⑤ 跨 Session 助手 | 必做 | 必做 | 必做 | — | 必做 | Day 1 |
核心洞察:跨 Session 助手提升最大 (+65 pts) — 因为 session 之间最难保留 context,note-taking + skill 正好补。如果你正在做类 Hermes Agent / Cursor 这类跨 session 工具,全套必做。
4. 效果到底多大
单点效果容易看 (Hamel 8.00 / LangSmith 7.80 / ReAct 7.90),复合效应才是决定整体的。我用你 short memory 里 14 篇评测数据做了估算:
| 项目类型 | baseline | + Evals | + Note | + Skill | + RAG | 提升 |
|---|---|---|---|---|---|---|
| 单轮问答 | 45% | 65% | 65% | 70% | 75% | +30 pts |
| RAG 文档问答 | 35% | 55% | 60% | 65% | 85% | +50 pts |
| 多步 Agent | 25% | 50% | 62% | 70% | 78% | +53 pts |
| Multi-Agent | 30% | 55% | 68% | 80% | 85% | +55 pts |
| 跨 Session | 20% | 45% | 60% | 75% | 85% | +65 pts |
单点效应 区间 +8% 到 +40%;复合效应(全套 5 件)整体提升 +193%。这意味着单点 effect 不是线性叠加 — 它们之间有协同。
诚实标注
上面的复合效应数字不是从一手 benchmark 跑出来的 — 是基于 14 篇评测 (D+4) + Drew Breunig / Hamel 公开博客的经验数字做的综合估算。要严格验证需要自己跑完整 5 best practice 的 ablation study。如果你跑过,欢迎分享数据。
Why this matters
如果你在做 LLM 应用,"好 prompt" 已经不够了 — 你需要"好 context"。Context 是 LLM 的 attention budget,是模型看到的所有东西的总和。管理这个 budget 是新的工程问题。
好消息是:8 个 best practice 里有 5 个已经在开源工具里成熟(LangGraph state / LlamaIndex RAG / DSPy optimizer / LangSmith Eval / anthropics/skills)。你不需要从零开始 — 用对工具组合就能拿到 +50% 以上提升。
这就是为什么我在 100 天冲刺里把 context engineering 列为必学 — 它跟 DSPy 的"prompt 自动调"是同一回事,但更全面。DSPy 调 prompt,context engineering 调 整个 LLM 输入。后者是前者的超集,也是 LLM 应用的护城河。
Prompt Engineering 是手艺。Context Engineering 是工程。
手艺好可以写得巧,工程好可以测得准。
2025 年起,我们从手艺人变成工程师。
References
- Drew Breunig (2025-06). How Contexts Fail, and How Designers Can Fix It. dbreunig.com
- Anthropic. Effective Context Engineering for Agents. Anthropic Engineering Blog.
- Hamel Husain. Your AI Product Needs Evals. hamel.dev
- Matt Pocock. Handoff Skill — Context-as-a-skill.
- LangChain. LangGraph: State Management for Agents. GitHub
langchain-ai/langgraph(13K+ stars) - LlamaIndex. Data Framework for LLM Applications. GitHub
run-llama/llama_index(42K+ stars) - Stanford NLP. DSPy: Compiling Declarative Language Model Calls. GitHub
stanfordnlp/dspy(26K+ stars) - Jeff Dean. AI 系统自动化的下一步. 2026 访谈 (转载 36kr 8-7)
- Jason Wei. Asymmetry of Verification.
Download
完整 4-Q 报告(Q1 是什么 / Q2 如何做好 / Q3 项目中应用 / Q4 量化效果)作为独立 HTML 报告发布,每篇含代码示例 + 数据表 + Lieflat 视觉语法。下载: