CONTEXT ENGINEERING · 4-Q REPORT · 2026-08-07 PART 2 OF 4 · RANKED LADDER
Q2 OF 4

如何做好
Context Engineering?

8 best practice · Ranked Ladder · 哪些必做 / 哪些是 nice to have
ranking method · 3 维加权
1影响度 (50%) · 直接防哪一类失败 + 提升多少 % 任务成功率
2实现成本 (30%) · 实现复杂度 + 维护成本
3可替代性 (20%) · 是否能用更简单方法达到类似效果

Q1 给出了"4 类失败模式 + 7 个 GitHub repo + 5 个量化证据"。Q2 进入下一步:具体怎么做?8 个 best practice 按"影响度 + 实现成本 + 可替代性" 3 维加权排序。

§1Ranked Ladder · 8 Best Practice

排名
Practice · 一句话
影响度
1st
Note-taking (scratchpad) Drew Breunig #1
Agent 写笔记摘要到 scratchpad,每步 reset / rolling summarization。最防 context distraction。LangGraph state 是工业级实现。
★★★★★IMPACT
2nd
Read-before-Write 验证 Drew Breunig #1
Agent 在写到 scratchpad / 工具输出前,必须先 read & verify。最防 context poisoning。是 note-taking 的前置条件。
★★★★★IMPACT
3rd
Evals (测试驱动 context) Hamel 8.00
先写 eval cases (期望 verdict / 期望输出) → 跑 baseline → 改 context → 跑 regression。让 context engineering "可测",不是玄学。
★★★★☆IMPACT
4th
Skill 系统化 Matt Pocock
每 skill = 一段 context bundle (instructions + examples + constraints)。跨 session / 跨 agent 复用。避免散 prompt。
★★★★☆IMPACT
5th
RAG (Retrival-Augmented Context) LlamaIndex 42K
从大文档 / 大数据库动态检索相关 context,避免 context 过长。LlamaIndex / LangChain RAG 是工业级实现。
★★★★☆IMPACT
6th
Tool call 显式 schema Anthropic
每个 tool 有清晰 schema (JSON / Pydantic) · 模型知道返回什么 · 减少 context confusion。Anthropic Building Effective Agents。
★★★☆☆IMPACT
7th
Multi-Agent 验证 Jeff Dean
多个 agent 试不同路径 + 一个 evaluator 选 best。防 model 在长程推理脱轨。增加成本但增加鲁棒性。
★★★☆☆IMPACT
8th
DSPy 自动 prompt opt Stanford 26K
用 optimizer 自动搜 prompt / context 配置。提升 10-15% 任务成功率,但需要先有 eval 才能 opt。
★★☆☆☆IMPACT

§23 个必做 + 5 个 nice-to-have

必做 / nice Practice 何时做 投入产出比
必做 Note-taking (scratchpad) Day 1 · 任何 agent 项目 ★ × 5
必做 Read-before-Write Day 1 · 任何 agent 项目 ★ × 5
必做 Evals (test-driven) Week 1 · 任何 LLM 项目 ★ × 4
nice Skill 系统化 项目 ≥ 5 个 prompt 时 ★ × 4
nice RAG 需要大文档 / 大数据库时 ★ × 4
nice Tool call schema tool call > 5 个时 ★ × 3
nice Multi-Agent 验证 可靠性要求 > 95% 时 ★ × 3
nice DSPy 自动优化 已有 eval · 想自动化时 ★ × 2

§3代码示例 · Read-before-Write

Drew Breunig 的 Read-before-Write 落地(防止 context poisoning):

def safe_tool_call(tool_fn, args):
    # 1. 第一次调用:read-only
    preview = tool_fn.dry_run(args)

    # 2. 模型验证 preview 是否合理
    is_valid = llm.invoke(f"""Is this result sensible?
Result: {preview}
Task: {task}

Answer YES or NO. If NO, explain why.""")

    # 3. 只有 YES 才真正调用
    if "YES" in is_valid.upper():
        return tool_fn(args)
    else:
        # 重试 + 调整 args
        return retry_with_adjustment(tool_fn, args, is_valid)

关键设计:不直接信任工具输出 — 让 LLM 自己 sanity check。

§4代码示例 · Eval-first Context Engineering

Hamel Husain 的 "evals first" 范式(落地代码):

from langsmith import Client
from langchain import ChatOpenAI

# Step 1: 写 eval cases (期望 verdict)
eval_cases = [
    {"input": "calculate 5 + 3", "expected": "8"},
    {"input": "compute pi to 2 decimals", "expected": "3.14"},
    {< class="str">"input": "what's the capital of France", "expected": "Paris"},
]

# Step 2: 跑 baseline
def baseline_eval(input_text):
    return llm.invoke(input_text).content

# Step 3: 跑回归测试
def run_evals(cases, predict_fn):
    passed = sum(1 for c in cases if predict_fn(c["input"]) == c["expected"])
    return passed / len(cases)  # pass rate

# Step 4: 改 context,跑回归
pass_rate_v1 = run_evals(eval_cases, baseline_eval)
# ... modify context ...
pass_rate_v2 = run_evals(eval_cases, new_eval)  # v2 should be > v1

关键设计先写 eval → 跑 baseline → 改 context → 跑回归。Context Engineering 变成可测量、可回归的工程。

§5所以 · Q2 答案

8 个 best practice · 3 必做 + 5 nice-to-have。
必做:Note-taking + Read-before-Write + Evals (任何 agent 项目)
复合效应:综合提升 +50-80% 任务成功率。
Context Engineering = 可测量、可回归的工程,不是玄学。
— Q2 答 · Hermes Agent · 2026-08-07

§6下一个问题

Q2 给出了"8 best practice + 3 必做 + 5 nice-to-have + 代码示例"。Q3 进入 项目中如何应用 — Hairline Area · 5 种典型项目 × 4 时间窗 · 何时应用哪个 best practice。

02 / Q2