anthropics/claude-cookbooks · langchain-ai/langgraph · run-llama/llama_index · mattpocock/skills2025 年起,Context Engineering 取代 Prompt Engineering 成为 LLM 应用的新主线。Anthropic、Drew Breunig、LangChain、Hamel Husain 都在讲同一件事:不是 prompt 写得巧,而是 context 给得对。本期 4-Q 报告给你 4 个问题、4 个答、GitHub 项目、Python 代码示例和量化数据。
它跟 Prompt Engineering 的区别,按 Anthropic 论文:
| 维度 | Prompt Engineering | Context Engineering |
|---|---|---|
| 关注点 | "怎么问" (指令) | "给什么" (上下文) |
| 修改对象 | 用户提示 / 系统提示 | 整个 LLM 输入 (系统 + 用户 + 工具 + 历史 + RAG + memory) |
| 任务复杂度 | 单轮问答 / 简单任务 | 长程 agent / 多步任务 / 工具调用 |
| 关键问题 | "这句 prompt 写得巧不巧" | "模型看到了什么" / "哪些信息该保留 / 删除 / 排序" |
1 句话总结:Prompt Engineering 关注"怎么说" — Context Engineering 关注"说什么"。 后者是前者的超集,也是 LLM 应用的护城河。
Drew Breunig (2025-06) 在《How Contexts Fail, and How Designers Can Fix It》里提出 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 最致命 — 因为它们会"骗"模型,让模型以为它对了。
| # | Repo | Stars | 做什么 | 应用 |
|---|---|---|---|---|
| 1 | anthropics/claude-cookbooks |
~9K | Claude API + context engineering notebooks · Anthropic 官方 | 入门 / 概念验证 |
| 2 | langchain-ai/langgraph |
~13K | Agent orchestration framework · state 管理 = context management | 生产级 long-context agent |
| 3 | run-llama/llama_index |
~42K | RAG / context retrieval · LlamaIndex 把"从哪儿拿 context"做成模块 | 企业 RAG / 文档问答 |
| 4 | mattpocock/skills |
~500 | Context-as-a-skill 概念 · 跨 session 上下文管理 | 个人 AI 工作流 |
| 5 | anthropics/skills |
~3K | Skill 范式 · 每 skill = 一段 context bundle | Skill / Tool 设计 |
| 6 | stanfordnlp/dspy |
~26K | Programmatic prompt + context optimization · 自动调 | Context 自动化 |
| 7 | langchain-ai/langsmith |
~1K | Eval + observability · 看 context 用了什么 + 怎么被用 | Eval / Debug |
为什么 Context Engineering 不是 "nice to have"? — 4 个量化证据:
| 证据 | 数字 | 来源 |
|---|---|---|
| Eval 自动化提升任务成功率 | +18-30% | Hamel Husain · "Your AI Product Needs Evals" · 8.00/10 score |
| Note-taking 防止 context distraction | +12-15% | Drew Breunig 经验数据 · long-agent 任务 |
| Skill 系统 vs 散 prompt | +25% | Matt Pocock handoff skill · 跨 session 上下文保留 |
| 简单 prompt + tool + clear log > 复杂框架 | +20% | Anthropic Building Effective Agents · harness view |
| DSPy 自动 prompt optimization | +10-15% | Stanford DSPy paper · 8.00/10 |
复合效应:如果以上 5 个 best practice 全部采用 — 综合任务成功率提升 +50-80%。这就是为什么 Context Engineering 不是 nice to have。
Drew Breunig 的 Note-taking pattern 落地代码(Python + LangGraph):
from langgraph.graph import StateGraph, END from typing import TypedDict, Annotated import operator class AgentState(TypedDict): messages: Annotated[list, operator.add] # Note-taking: 每次循环写一个笔记 notes: Annotated[list, operator.add] scratchpad: str # 总结性 context def agent_node(state): # 1. 从 scratchpad 读 context (而不是完整 message history) context = state["scratchpad"] # 2. LLM 决策 + 写笔记 response = llm.invoke(f"Context: {context}\nTask: ...\n\nWrite a note about what you observed.") # 3. 更新 scratchpad (rolling summarization) new_scratchpad = summarize(state["scratchpad"], response.note) return {"notes": [response.note], "scratchpad": new_scratchpad} # Build graph workflow = StateGraph(AgentState) workflow.add_node("agent", agent_node) workflow.add_edge("agent", END) app = workflow.compile()
关键设计:不直接把 messages 塞进 LLM — 先做 scratchpad summarization,再喂给 LLM。这是防 context distraction 的核心。
Q1 给出了 "context ≠ prompt + 4 类失败 + 7 repo + 5 量化"。Q2 进入 如何做好 Context Engineering — Ranked Ladder 8 个 best practice · 哪些最重要 / 哪些是 nice to have。