跳到主要内容Skip to main content

上下文工程怎么做How to Do Context Engineering

微型指南 · 约 8 分钟Guide · about 8 min · 引用核验 2026-11-03(仅引用链接与日期) · refs checked 2026-11-03 (reference links and dates only)

上下文工程不是「多塞资料」,而是决定每一轮该让模型看到什么、看不到什么,以及怎么压缩。Context engineering is not "stuff more material in". It decides what the model sees each turn, what it does not see, and how to compress the rest.

怎么做How to

先定必放项:当前目标、硬约束、可用工具、输出格式、一两个示例;其余都是可选项。

删掉无关历史与重复工具输出:同一份文件读三次,只保留最新一次与差异。

压缩用摘要或结构化状态:把长对话压成「已确认事实 / 待办 / 未决问题」,而不是逐字截断。

工具输出要治理:分页、截断、只取相关段落,并保留来源引用,避免把整页塞进上下文。

给记忆划边界:会话内状态、跨会话事实、外部检索三层分开,不要把检索结果当记忆。

验收看两件事:任务成功率是否上升、每轮 token 是否下降;两者都变好才算有效。

Fix the must-haves first: current goal, hard constraints, available tools, output format and one or two examples; everything else is optional.

Drop irrelevant history and repeated tool output: if the same file was read three times, keep the latest read and the diff.

Compress with summaries or structured state: turn a long conversation into "confirmed facts / open tasks / unresolved questions" rather than truncating verbatim.

Govern tool output: paginate, truncate, extract only relevant passages and keep source references instead of pasting whole pages.

Draw memory boundaries: in-session state, cross-session facts and external retrieval are three layers; do not treat retrieval results as memory.

Acceptance looks at two numbers: task success rate up and tokens per turn down; only both count as effective.

例子与判断Example and interpretation

以下是教学情境,不是本站运行或实测记录。A teaching scenario, not a measured or executed site test.

任务只需核对活动日期,不必放入全部会议历史。保留已确认资料、相关限制和必要出处,移出无关聊天;若移走了一个限制,要确认它已写入可恢复的记录。Checking an event date does not need the entire meeting history. Keep confirmed facts, relevant constraints and source references; remove unrelated chat. Ensure removed constraints remain recoverable when still needed.

出处Sources
  • Anthropic · Effective context engineering for AI agentsAnthropic · Effective context engineering for AI agents ↗
  • Lost in the Middle(arXiv 2307.03172)Lost in the Middle: How Language Models Use Long Contexts ↗
  • LangGraph · PersistenceLangGraph · Persistence ↗