框架內的語境工程:克服長時任務中語境超載與目標遺失的四種機制
An agent, in its simplest form, is an LLM calling tools in a loop.That loop works for short jobs.Give it a task that runs for an hour and 200 tool calls, and it breaks in 2 predictable ways.
The AWS Samples design guide for autonomous cloud coding agents names them directly: shallow agents suffer from context overflow, get distracted (goal loss), and do not maintain state over long periods.The layer that fixes this is not the model.
It is the harness, which AWS describes as managing everything but the model.This article opens up that layer.Compaction, memory strategy, context budgeting, and todo-state are the machinery that turns a shallow loop into a deep agent.
We look at how LangChain Deep Agents, Claude Code, Manus, OpenAI Codex, and Amazon Bedrock AgentCore implement each one, with the actual thresholds they ship.Why a bigger window does not fix it The obvious fix is a larger context window.The evidence says 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’s context engineering guide explains the mechanism: attention creates n² pairwise relationships for n tokens, so every added token depletes a finite “attention budget.” Context is a resource with diminishing returns, not a bucket.For an agent loop, this is worse than it sounds.
Manus reports that a typical task needs around 50 tool calls, and that 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, which is exactly where recall degrades.
Goal loss is not only a model bug.It is the expected outcome of an unmanaged context on a long enough task.Mechanism 1: Context budgeting and offloading The first job of a harness is deciding what never enters the window at all.Deep Agents ships 2 offloading rules with hard numbers.
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 full file contents already live on disk, are truncated to a pointer.Only after offloading runs out of room does the harness fall back to summarization.
Claude Code applies the same budgeting to what loads before the first prompt.Auto memory is capped at the first 200 lines or 25KB.MCP tool schemas stay deferred by default, with only tool names listed, and full schemas load on demand via tool search.
After compaction, any re-read file over 5,000 tokens comes back as a path reference rather than content.The context window simulation in the Claude Code docs makes the payoff concrete: a research subagent reads 6,100 tokens of files and returns a 420-token result to the parent.
That subagent pattern is budgeting at the architecture level.Anthropic’s guide notes that each subagent may burn tens of thousands of tokens exploring, but returns a distilled summary, often 1,000 to 2,000 tokens.
The AWS AgentCore walkthrough builds exactly this: a coordinator spawns 3 browser subagents in parallel, each in its own MicroVM, and an analyst subagent receives only their structured findings.
AWS reports a 4 to 6 minute expected runtime, and notes that sequential processing would take up to 3x longer.Mechanism 2: Compaction When offloading is not enough, the harness summarizes.
Compaction is the practice of taking a conversation nearing the window limit, summarizing it, and reinitiating a new context with the summary.It is also where goal loss most often happens, because a lossy summary can drop the one constraint that mattered.
The implementations differ in what they promise to keep.Claude Code’s compaction prompt preserves architectural decisions, unresolved bugs, and implementation details while discarding redundant tool outputs.
Right after compaction it re-reads up to 5 of the files modified most recently, reloads the rules matching those files, and re-injects invoked skill bodies, capped at 5,000 tokens per skill and 25,000 total.
The docs are explicit that detailed instructions from early in the conversation may be lost, which is why persistent rules belong in the project-root CLAUDE.md, which is re-injected from disk.
Users can steer the pass with /compact focus on the auth bug fix or move the trigger point with /autocompact.Deep Agents made goal preservation a structural feature.Its summary is a structured document with dedicated fields for session intent, artifacts created, and next steps.
The LangChain team added those fields after forced-summarization experiments showed the change improved performance.The full original transcript is also written to the filesystem, so a fact that was summarized away can be recovered by readfile later.Compaction has moved into the API layer too.
OpenAI’s Responses API offers server-side compaction via contextmanagement with a compactthreshold, plus a standalone /responses/compact endpoint that returns a compacted context window containing an opaque encrypted compaction item; OpenAI instructs developers to pass that returned window unchanged into the next call.
OpenAI says Codex relies on this mechanism to sustain long-running coding tasks.The Claude Developer Platform exposes a compact20260112 context-management edit with custom instructions and a pauseaftercompaction option for inserting content before the model continues.
When you write custom instructions there, they replace the default prompt entirely, so a compaction prompt is a real engineering artifact, not a setting.Mechanism 3: Todo-state and recitation Compaction protects the goal at the moment of summarization.Todo-state protects it on every turn in between.
Manus described the trick plainly: its agent creates a todo.md and rewrites it step by step, checking items off.Rewriting the list recites the 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.
No architecture change is required.It is natural language used to bias the model’s own attention.The evidence on todo-state is not one-sided.Deep Agents shipped a write_todos tool by default until v0.
7 in July 2026, when LangChain made TodoListMiddleware opt-in after its evals across 3 task categories showed slightly better reward and lower cost with todos disabled.LangChain still recommends turning it back on for long multi-step tasks, less capable models, and UIs that show progress.
Claude Code keeps a todo list and re-injects the plan written in plan mode from disk after compaction.Anthropic’s guide calls the general pattern structured note-taking: the agent writes a NOTES.md or TODO file outside the window and reloads it.
Its Claude Plays Pokémon example maintained tallies across thousands of game steps, then read its own notes after each context reset and resumed multi-hour sequences.The pattern behind all of these is that the goal exists as a mutable artifact, not only as a message in history.
Messages age and get summarized.A file that is rewritten every few turns is always recent, always short, and survives any reset.Whether that is worth its per-turn token cost depends on the model and the task length, which is exactly what the Deep Agents evals measured.
Mechanism 4: Memory strategy across sessions The last piece is what persists after the task ends.Claude Code re-injects the project-root CLAUDE.md and auto memory from disk after every compaction.
AgentCore Memory stores events and runs configured extraction strategies in the background, so a coordinator can call a recall tool on the next run instead of re-researching.
AWS warns that without at least 1 extraction strategy configured, raw events are stored but nothing is extracted for retrieval.Anthropic’s file-base
Related
相關文章

無問芯穹與華環電子簽署戰略合作,共同探索國產異構算力AI基礎設施新方向
無問芯穹與華環電子簽署戰略合作協議,雙方將結合各自在AI軟體平台、網路通信與硬體研發的優勢,共同探索國產異構算力基礎設施的協同方案。此次合作聚焦於智算中心解決方案及「Token工廠」新模式,目標是推動計算、網路與AI原生基礎設施深度融合,為AI規模化應用提供高效穩定的支撐。
Jina AI Releases jina-ocr-v1: A 3.4B MoE Document Parser With Built-In Speculative Decoding for Low-Budget GPUs
Jina AI, part of Elastic, has released jina-ocr-v1, an end-to-end visual document parser. It takes PDFs, scans, tables, charts or invoices and returns clean Markdown in 1 pass. The model has 3.

優步全球範圍裁員 10%,被裁員工稱 AI 已大舉滲透日常工作
作者:清源 責編:清源 評論: 9 月 18 日消息,據《商業內幕》今天(18 日)晚間報道,在優步(Uber),AI 已經滲透到員工工作的許多環節,從回答 Slack 裡的內部問題,到替乘客行程中聯繫客服時收到的消息撰寫回復。6 名近期遭裁員的員工透露,過去幾個月,AI 在工作中的使用範圍明顯擴大,其中一些人甚至會通過提示詞讓 AI 完成相當一部分任務。

智譜 ZCode 被質疑“偷傳代碼”:官方回應稱問題已修復,將開源代碼庫、引入第三方審查
作者:清源 責編:清源 評論: 感謝網友 咩咩洋 的線索投遞!9 月 18 日消息,針對社區中有關代碼庫數據上傳的討論,智譜旗下編程產品 ZCode 今天(18 日)通過智譜官方群組向受影響用戶致歉,併發布回應稱已第一時間完成自查,相關問題目前已經修復。

月之暗面遞表之後,Kimi 的成色要被驗算三遍
舒澤品牌手記2026.09.18 18:16 · 來自浙江全文4982字00:00 / 14:05Anthropic 的 30 萬次指控,會成為招股書的第幾頁?文 | 舒澤品牌手記9月17日,月之暗面發佈了一套金融行業解決方案。按官方披露,中信建投、中金公司、易方達等數十家金融機構已經在用 Kimi 處理投研建模、風險排查和盡調材料——研究人員把管理層報表、審計報告和盡調文件交給 Kimi,拿回一份可以繼續調整假設的 Excel 模型。同一天,深圳商報記者就港股上市進展、股東架構調整等事項向月之暗面發去採訪函。

Calibre上手 AI 互動寫作:電子書管理器搖身變成"文字冒險遊戲引擎"
這個遊戲默認藏而不發,不會跟著 Calibre 啟動就冒出來。用戶得主動在"首選項 — 工具欄和菜單"裡把它請到主工具欄,才算真正激活。它的玩法很清晰:由 AI 在後臺搭起並掌管一個虛構世界,用戶通過不斷輸入文字來推著故事往前走,等於把"讀電子書"這件事,翻轉成了"和 AI 一起寫故事"。