脚手架内部的上下文工程:战胜长程任务中上下文溢出与目标迷失的 4 大机制
Context Engineering Inside the Harness: 4 Mechanisms That Beat Context Overflow and Goal Loss on Long-Horizon Tasks
文章背景与核心概要
在面对需要执行数十步甚至上百步的复杂长程任务时,仅依靠“大语言模型 + 简单调用循环”构建的 AI 智能体 (AI Agent) 往往会因为“上下文溢出”和“目标迷失”而功亏一篑。许多人寄希望于模型上下文窗口的无限扩张,但实测表明,随着输入长度增长,模型的注意力机制会发生严重的“上下文腐烂”,在长文本中迷失关键线索。真正的解法不在于模型本身,而在于包裹在模型外层的系统架构——“脚手架 (Harness)”。本文系统梳理了 LangChain Deep Agents、Claude Code、Manus、OpenAI Codex 以及 Amazon Bedrock AgentCore 等顶尖工业级框架的设计哲学,深入解析了它们在长程任务中赖以维持系统稳定、控制上下文预算与防范目标丢失的 4 大核心工程机制。
在长程任务中,以简单循环运行的大语言模型 (Large Language Model, LLM) 智能体往往不可避免地会由于“上下文溢出”与“目标迷失”而走向失败。单纯扩大上下文窗口绝非万灵药,因为随着输入长度的不断攀升,模型的注意力表现会逐步退化。相反,真正的解决之道深植于“脚手架 (Harness)”——即围绕在模型周围的系统架构层之中。本文将深入探讨现代智能体框架 (如 LangChain Deep Agents、Claude Code、Manus、OpenAI Codex 以及 Amazon Bedrock AgentCore) 所采用的 4 大核心机制,解析它们如何在长时间跨度的长程操作中维护状态、规划上下文预算并保护智能体目标不被遗忘。
Summary: On long-horizon tasks, LLM agents running in simple loops inevitably fail due to context overflow and goal loss. Expanding the context window is not a silver bullet because attention degrades as input length grows. Instead, the solution lies within the harness—the architectural layer surrounding the model. This article explores four core mechanisms used by modern agent frameworks (such as LangChain Deep Agents, Claude Code, Manus, OpenAI Codex, and Amazon Bedrock AgentCore) to maintain state, budget context, and protect agent goals over extended operations.
为什么更大的上下文窗口无法根治问题
Why a Bigger Window Does Not Fix It
面对上下文长度的限制,人们最容易想到的直观解法就是直接把上下文窗口做大。然而,大量实证研究表明,这种方法的效果远不及预期。Chroma 的上下文腐烂 (Context Rot) 报告在评估了包括 GPT-4.1、Claude 4、Gemini 2.5 和 Qwen3 在内的 18 款大语言模型后发现,哪怕是在最简单的检索任务中,随着输入长度的增加,模型的表现也会变得越来越不可靠。
The obvious fix for context limits is a larger context window. However, empirical evidence shows it helps less than expected. Chroma’s Context Rot report evaluated 18 LLMs (including GPT-4.1, Claude 4, Gemini 2.5, and Qwen3) and found that performance grows increasingly unreliable as input length grows, even on simple retrieval tasks.
Anthropic 发布的上下文工程指南深入解释了这一底层机理: * 注意力机制对于 \(n\) 个 Token 会产生 \(n^2\) 对成对关系; * 每新增一个 Token,都会消耗模型有限的“注意力预算”; * 上下文是一种边际收益递减的稀缺资源,而不是一个深不见底的无限容积桶。
Anthropic’s context engineering guide explains the mechanism: * Attention creates \(n^2\) pairwise relationships for \(n\) tokens. * Every added token depletes a finite "attention budget." * Context is a resource with diminishing returns, not an infinite bucket.
对于智能体的运行循环而言,实际情况比理论听起来还要严峻得多。Manus 的实测数据显示,一个典型的复杂任务通常需要大约 50 次工具调用,输入与输出的 Token 比例接近惊人的 100:1。每一次工具返回的环境观察结果都会直接涌入上下文并堆积在那里。随着对话拉长,最初的用户指令被不断推向窗口中央——而这恰恰是模型召回与注意力衰减最严重的“迷失在中间 (Lost-in-the-Middle)”盲区。因此,目标迷失绝不仅仅是模型偶尔犯错的小毛病,而是在长程任务中放任上下文野蛮生长所必然导致的工程必然。
For an agent loop, this is worse than it sounds. Manus reports that a typical task needs around 50 tool calls, and the input-to-output token ratio runs near 100:1. Each observation lands in context and stays there. The original instruction drifts toward the middle of the window—the exact zone where recall degrades. Goal loss is not just a model bug; it is the expected outcome of an unmanaged context on long-running tasks.
机制 1:上下文预算控制与数据卸载
Mechanism 1: Context Budgeting and Offloading
脚手架的首要职责,就是严格把关并裁决哪些内容绝不能进入上下文窗口。
The first job of a harness is deciding what never enters the window at all.
- Deep Agents:内置了两条极为严苛的数据卸载规则:
- 当工具返回的内容超过 20,000 个 Token 时,系统会直接将其写入底层文件系统,并在上下文中替换为该文件路径以及前 10 行内容的简短预览。
- 当会话上下文占用到模型窗口的 85% 时,较早前的写入与编辑工具调用记录 (由于其实际文件内容早已保存在磁盘上) 会被截断为轻量级的指针引用。只有当数据卸载手段用尽且空间依然不足时,系统才会动用会话摘要作为兜底方案。
- Claude Code:同样实践了极其类似的预算管控原则:
- 自动记忆 (Auto-memory) 的体积被严格限制在前 200 行以内或 25KB 以下;
- MCP 工具定义 (Tool Schema) 默认保持延迟加载状态,仅在需要时通过工具搜索按需加载完整 Schema;
- 在完成上下文压缩后,任何重新读取且体积超过 5,000 Token 的文件,都只会以路径引用的形式返回,而非直接灌入原始内容。
- Deep Agents: Ships with two strict offloading rules:
- When a tool response exceeds 20,000 tokens, it is written to the filesystem and replaced with a file path plus a preview of the first 10 lines.
When session context crosses 85% of the model’s window, older write and edit tool calls (whose file contents already live on disk) are truncated to a pointer. Summarization is only used as a fallback when offloading runs out of room.
Claude Code: Applies similar budgeting principles:
- Auto-memory is capped at the first 200 lines or 25KB.
- MCP tool schemas remain deferred by default, loading full schemas on demand via tool search.
- After compaction, any re-read file over 5,000 tokens returns as a path reference rather than raw content.
子智能体架构模式
The Subagent Pattern
上下文预算控制同样发生在系统架构设计层面。Anthropic 指出,尽管子智能体 (Subagent) 在自主探索过程中可能会消耗数万个 Token,但它们最终仅向主流程汇报提炼后的精简摘要 (通常只有 1,000 到 2,000 个 Token)。
Budgeting also happens at the architecture level. Anthropic notes that while subagents may burn tens of thousands of tokens exploring, they return a distilled summary (typically 1,000 to 2,000 tokens).
无独有偶,在 Amazon Bedrock AgentCore 实战演练 中,系统采用了一个协调者智能体来并行启动三个浏览器子智能体 (各自运行在独立的 MicroVM 虚拟机中)。随后,专门的分析师子智能体仅接收它们提炼后的结构化调研结论,相比于传统的单线程顺序执行,大幅缩短了预期的运行时间并节约了主干上下文。
Similarly, the Amazon Bedrock AgentCore walkthrough uses a coordinator that spawns three browser subagents in parallel (each in its own MicroVM). An analyst subagent then receives only their structured findings, cutting expected runtimes down significantly compared to sequential processing.
机制 2:上下文压缩
Mechanism 2: Compaction
当数据卸载手段依然无法腾出足够的空间时,脚手架就必须对历史记录进行浓缩提炼。上下文压缩 (Compaction) 会抓取即将达到窗口极限的历史会话,将其提炼为结构化摘要,并利用该摘要重新开启一个全新的纯净上下文窗口。然而,这也是“目标迷失”最容易滋生的重灾区——如果产生的信息摘要存在有损压缩,遗漏了核心约束条件或初衷,智能体就会瞬间偏离轨道。
When offloading is insufficient, the harness must summarize. Compaction takes a conversation nearing the window limit, summarizes it, and reinitiates a new context window with the summary. However, this is also where goal loss frequently occurs if a lossy summary drops crucial constraints.
- Claude Code 的压缩机制:精准保留关键的架构决策、尚未修复的缺陷以及具体实现细节,同时坚决剔除冗余的工具输出。压缩完成后,它会立即重新读取最多 5 个最近修改的文件,重新加载匹配的规则规范,并重新注入先前调用的技能正文 (单项技能上限为 5,000 Token,总量上限为 25,000 Token)。开发者可以通过
/compact命令手动介入,或使用/autocompact开启自动化压缩。 - Deep Agents 的压缩机制:把“维护核心目标”作为一项刚性结构特性来对待。压缩生成的摘要并非散乱的自然语言,而是规范格式化文档,内含专门的字段记录会话初衷、已生成工件以及下一步规划。与此同时,完整的历史轨迹会被完整写入文件系统,以便后续需要找回遗漏细节时,智能体可以通过
read_file随时回溯。 - API 原生层面的压缩:OpenAI 的 Responses API 通过
context_management配置和/responses/compact端点提供了服务端原生支持的压缩能力。Codex 在处理长程编程任务时便重度依赖这一机制。与此同时,Claude 开发者平台 (Claude Developer Platform) 也开放了compact_20260112上下文管理接口,支持自定义提炼指令以及pause_after_compaction(压缩后暂停) 选项。
- Claude Code Compaction: Preserves architectural decisions, unresolved bugs, and implementation details while discarding redundant outputs. It immediately re-reads up to 5 recently modified files, reloads matching rules, and re-injects invoked skill bodies (capped at 5,000 tokens per skill, 25,000 total). Users can steer this via
/compactor trigger automatic compaction with/autocompact.- Deep Agents Compaction: Treats goal preservation as a structural feature. Summaries are formatted documents with dedicated fields for session intent, artifacts created, and next steps. The full transcript is written to the filesystem so dropped facts can be recovered via
read_file.- API-Layer Compaction: OpenAI’s Responses API offers server-side compaction via
context_managementand a/responses/compactendpoint. Codex relies on this mechanism for long-running coding tasks. Meanwhile, the Claude Developer Platform exposes acompact_20260112context-management edit with custom instructions and apause_after_compactionoption.
机制 3:待办状态与目标朗读复述
Mechanism 3: Todo-State and Recitation
上下文压缩虽然能在历史精简时守护住核心目标,但在日常交替的每一次交互轮次中,真正捍卫目标的却是待办状态 (Todo-State)。
Compaction protects the goal during summarization, but todo-state protects it on every turn in between.
- Manus 的应对哲学:智能体会创建一个
todo.md任务清单文件,并在每完成一步时逐步重写该文件,顺次划掉已完成事项。这种“重写清单”的操作,巧妙地将当前全局目标“朗读复述”到了上下文窗口的最末端,使其稳稳停留在模型近期注意力最聚焦的黄金区间,彻底缓解了“迷失在中间”带来的意图飘移。 - 收益与成本的权衡:在 2026 年 7 月发布的 v0.7 版本 之前,Deep Agents 一直默认搭载
write_todos工具,随后 LangChain 将TodoListMiddleware调整为了手动开启的按需选项。跨三个维度的评测表明,在应对简单任务时,关闭待办清单反而能取得略高的奖励并降低 Token 消耗。不过,LangChain 仍然强烈建议在处理长程多步复杂任务、调用能力偏弱的模型或在前端 UI 需要实时展示进度时保持开启。 - 结构化笔记策略:Anthropic 的工程指南重点推荐了在窗口之外维护
NOTES.md或TODO文件的模式。例如在著名的 Claude Plays Pokémon (Claude 玩宝可梦) 项目中,智能体在成千上万个游戏步骤中持续记录计数与状态,每次上下文重置后通过主动读取自己的笔记,便能丝滑无缝地接续耗时数小时的复杂长操作。
- The Manus Approach: The agent creates a
todo.mdfile and rewrites it step-by-step, checking items off. Rewriting the list recites objectives into the end of the context, pushing the global plan into the model's recent attention span and reducing "lost-in-the-middle" drift.- The Trade-offs: Deep Agents shipped a
write_todostool by default until v0.7 in July 2026, when LangChain madeTodoListMiddlewareopt-in. Evals across three categories showed slightly better rewards and lower costs with todos disabled for simple tasks. However, LangChain still recommends turning it on for long multi-step tasks, less capable models, and UIs displaying progress.- Structured Note-Taking: Anthropic's guide highlights patterns like maintaining a
NOTES.mdorTODOfile outside the window. For example, in Claude Plays Pokémon, the agent maintained tallies across thousands of game steps, reading its own notes after each context reset to resume multi-hour sequences.
其背后的核心法则在于:终极目标应当作为可动态变更的工件 (Artifact) 独立存在,绝不能仅仅作为历史记录中一条随着时间流逝终将被淹没或被摘要稀释的过眼云烟。
The core principle: The goal should exist as a mutable artifact, not just a message in history that eventually gets buried or summarized away.
机制 4:跨会话的记忆持久化策略
Mechanism 4: Memory Strategy Across Sessions
系统拼图的最后一块,在于如何严谨管理任务执行完毕之后依然需要沉淀下来的知识。
The final piece of the puzzle manages what persists after a task ends.
- Claude Code 在每次上下文压缩后,都会自动从磁盘重新将项目根目录的
CLAUDE.md以及自动记忆 (Auto-memory) 重新注入上下文; - AgentCore Memory 负责存储历史事件,并执行配置好的后台提炼策略,从而让协调者智能体能够在后续运行中调用召回工具检索过往经验。(注:AWS 明确告诫,若未配置提炼策略,系统只会盲目存储原始事件,后续检索将一无所获)。
- Claude Code re-injects the project-root
CLAUDE.mdand auto-memory from disk after every compaction.- AgentCore Memory stores events and runs configured background extraction strategies so a coordinator can call a recall tool on subsequent runs. (Note: AWS warns that without an extraction strategy, raw events are stored but nothing is retrieved).
持久记忆的隐形成本
The Cost of Persistent Memory
持久化的上下文绝非免费的午餐。ETH Zurich 的一项实证研究 尖锐地指出,类似 AGENTS.md 这样的代码库上下文文件往往会在并未显著提升任务成功率的前提下,大幅推高推理开销 (由大语言模型自动生成的上下文文件使成本攀升了 20%–23%,即便是开发者精心编写提交的文件也会增加高达 19% 的成本)。
Persistent context is not free. An ETH Zurich study found that repository context files like
AGENTS.mdoften increase inference costs without meaningfully boosting task success (LLM-generated files increased costs by 20%–23%, and developer-committed files by up to 19%).
工程最佳实践:务必让持久记忆保持极致精炼 (例如将 CLAUDE.md 的行数严格压制在 200 行以内),并将具体的参考资料解耦分流至技能 (Skill) 或基于路径的作用域规则中,做到仅在触发对应场景时才按需加载。
Best Practice: Keep persistent memory lean (e.g., keep
CLAUDE.mdunder 200 lines) and offload reference materials into skills or path-scoped rules that load only when needed.
验证脚手架是否真正锁定了目标
Testing Whether the Harness Actually Holds the Goal
上下文工程的价值只有在一种前提下才能真正体现:即智能体能够顺利走完全部任务流程,并且能够召回那些早已从当前上下文窗口中隐去的关键细节。为了对此进行严格验证,主流框架的架构师们专门设计了针对性的评估基准: * 任务中途强制压缩评估 (Mid-task Summarization Evals):在任务执行中途人为强制触发压缩摘要,检验智能体能否不受干扰、继续坚定不移地朝着原始目标推进; * 大海捞针测试 (Needle-in-a-Haystack Tests):确保那些已被浓缩或卸载到外部的事实,依然可以通过文件系统搜索被高保真地重新唤醒。
Context management is only useful if the agent can finish the task and recover details it no longer sees. Framework builders run targeted evaluations to test this: * Mid-task Summarization Evals: Triggering summarization mid-task to verify if the agent continues toward its objective. * Needle-in-a-Haystack Tests: Ensuring summarized facts can still be successfully recovered through filesystem search.
如果你正在运行一套智能体脚手架,却从未在受控测试环境中故意触发过一次强制上下文压缩,那么你根本无法预知自己的摘要提示词在暗中丢弃哪些致命细节。
If you run an agent harness and have not forced a compaction in a test environment, you do not yet know what your summary prompt is dropping.
核心要点总结
Key Takeaways
- 脚手架架构是重中之重:简单循环的浅层智能体必然会倒在上下文溢出与目标迷失之下。真正的解法必须在脚手架工程中落地,而不是指望模型窗口无限变大;
- 预防性预算优于事后压缩:Deep Agents 坚决将超过 20,000 Token 的工具结果卸载至磁盘,并在窗口占用达到 85% 时主动剔除旧有的代码编辑上下文;
- 结构化定向压缩:上下文压缩绝不能听凭自然语言随意概括,必须强制明确保留核心要素 (如会话初衷、下一步动作以及最近修改的文件列表);
- 权衡待办事项的复述成本:尽管待办列表能够将全局目标实时刷新在上下文末端,但严苛的基准测试表明,它也会带来额外的 Token 开销,在应对简单任务时并非不可或缺;
- 持久记忆有成本:常驻的代码库上下文文件可能使推理成本攀升多达 23%,却未必能换来性能的稳步提升;因此务必保持极度精炼与按需加载。
- The Harness is Critical: Shallow agents fail from context overflow and goal loss. The fix belongs in the harness, not the model.
- Budget First: Deep Agents offloads tool results over 20,000 tokens and evicts old edits when crossing 85% of the window.
- Structure Compaction: Compactions must explicitly name what they keep (e.g., session intent, next steps, or recently modified files).
- Weigh Todo-Recitation: While todo lists keep goals fresh at the end of the context window, evals show they carry a token cost that isn't always necessary for simpler tasks.
- Memory has a Tax: Persistent repository context files can increase inference costs by up to 23% without guaranteed performance gains; keep them concise.
本文取材自 MarkTechPost 发表的原始文章。
Adapted from the original article published on MarkTechPost.