
TL;DR: We've added a tool to the Deep Agents SDK (Python) and CLI that allows models to compress their own context windows at opportune times.简而言之:我们在 Deep Agents SDK (Python) 和 CLI 中添加了一个工具,允许模型在合适的时机自行压缩其上下文窗口。
Motivation动机
Context compression is an action that reduces the information in an agent’s working memory. Older messages are replaced by a summary or condensed representation of an agent’s progress that preserves what’s relevant to a task. This action is often necessary to accommodate finite context windows and reduce context rot.上下文压缩是一种旨在减少智能体工作记忆中信息量的操作。它将旧消息替换为对智能体进展的摘要或浓缩表示,从而保留与任务相关的内容。为了适应有限的上下文窗口并减少上下文衰减,此操作通常是必要的。
Agent harnesses often control this by compacting at a fixed token threshold (deepagents uses model profiles to compact at 85% of any given model’s context limit). This design is suboptimal because there are good times and bad times to compact:智能体控制框架通常通过在固定的 Token 阈值下进行压缩来管理这一点(deepagents 使用模型配置文件,在达到给定模型上下文限制的 85% 时进行压缩)。这种设计并不理想,因为压缩时机有好坏之分:
- It is not ideal to compact when you’re in the middle of a complex refactor;在进行复杂的重构过程中进行压缩并不理想;
- It is better to compact when you are starting a new task or otherwise believe that prior context will lose relevance.而在开始新任务或认为之前的上下文不再重要时进行压缩效果更好。
Many interactive coding tools feature a /compact command or similar, which allows users to manually trigger a context compression step at opportune times. We take this one step further in the latest release of deepagentsand expose a tool to the agent that lets it trigger context compression itself. This enables more opportunistic compaction without requiring your application’s users to be aware of finite context windows or issue specific commands.许多交互式编码工具都具备 /compact 命令或类似功能,允许用户在合适的时机手动触发上下文压缩步骤。在最新发布的 deepagents 中,我们更进一步,向智能体开放了一个工具,使其能够自行触发上下文压缩。这实现了更具主动性的压缩,而无需应用程序用户时刻关注有限的上下文窗口或手动输入特定命令。
This tool is currently enabled in Deep Agents CLI and opt-in in the deepagents SDK.该工具目前已在 Deep Agents CLI 中启用,并在 deepagents SDK 中作为可选功能提供。
We are generally bullish on the idea that harnesses should, where possible, “get out of the way” and take advantage of improvements in the underlying reasoning models. This is an instance of the bitter lesson: can we give agents more control over their own context to avoid tuning their harness by hand?我们总体上坚信,框架应尽可能“隐形”,并充分利用底层推理模型的改进。这就是“苦涩的教训”(bitter lesson)的一个实例:我们能否让智能体更好地控制自己的上下文,从而避免手动调整框架?
When should we compact?何时应该进行压缩?
There is a variety of situations that could warrant a context compression action.有多种情况可能需要进行上下文压缩操作。
At clean task boundaries:在清晰的任务边界处:
- A user signals that they are moving on to a new task for which earlier context is likely irrelevant用户表示正在转向一项新任务,之前的上下文可能不再相关
- The agent has finished a deliverable and the user acknowledges task completion智能体已完成交付成果,且用户确认任务已完成
After extracting a result from a large amount of context:从大量上下文中提取结果后:
- The agent has obtained a fact, conclusion, summary, or other result by consuming a significant amount of context, as in a research task智能体通过处理大量上下文(如研究任务)获得了事实、结论、摘要或其他结果
Before consuming a large amount of new context:在消耗大量新上下文之前:
- The agent is about to generate a long draft智能体即将生成长篇草稿
- The agent is about to read a large amount of new context智能体即将读取大量新上下文
Before entering a complex multi-step process:进入复杂的多步骤流程之前:
- The agent is about to start a lengthy refactor, migration, multi-file edit, or incident response智能体即将开始冗长的重构、迁移、多文件编辑或事件响应
- The agent has produced a plan and is about to begin executing the steps智能体已制定计划并准备开始执行步骤
A decision has been made that supersedes prior context:做出了推翻先前上下文的决定时:
- New requirements have come to light that invalidate previous context出现了使先前上下文失效的新需求
- There are many tangents or dead-ends that can be reduced to a summary存在许多可以浓缩为摘要的离题内容或死胡同
Enumerating all possible scenarios is not practical, but our observation is that people and LLMs can identify these scenarios and compact at opportune times, saving the need for a compaction step later on when the context window is nearing its limit. You can read the guidance we provide the model around this tool in its system prompt.列举所有可能的情况并不现实,但我们的观察表明,人类和大型语言模型能够识别这些场景并在合适的时机进行压缩,从而避免了在上下文窗口接近极限时被迫进行压缩。您可以在系统提示词中查看我们围绕此工具为模型提供的指导建议。
What happens when the tool is called?调用该工具时会发生什么?
The tool is parametrized the same as the existing Deep Agents summarization middleware: we retain recent messages (10% of available context) and summarize what comes before. Recent messages, including the call to the compaction tool and associated response, are retained in the recent context.该工具的参数设置与现有的 Deep Agents 摘要中间件相同:我们保留最近的消息(可用上下文的 10%),并对之前的内容进行摘要。最近的消息(包括调用压缩工具及其响应)将保留在近期上下文中。

See example trace.查看示例追踪。
How to use如何使用
The tool is implemented as a separate middleware, so you can enable it by adding it to the middleware list in create_deep_agent:该工具作为独立的中间件实现,因此您可以通过将其添加到 create_deep_agent 中的中间件列表来启用它:
from deepagents import create_deep_agent
from deepagents.backends import StateBackend
from deepagents.middleware.summarization import (
create_summarization_tool_middleware,
)
backend = StateBackend # if using default backend
model = "openai:gpt-5.4"
agent = create_deep_agent(
model=model,
middleware=[
create_summarization_tool_middleware(model, backend),
],
)See the SDK docs for more details.详情请参阅 SDK 文档。
In the CLI, simply call /compact when you’re ready to trim context or move onto a new task.在 CLI 中,只需在准备修剪上下文或转向新任务时调用 /compact 即可。
Our experience with this feature我们对该功能的实践经验
We tuned this feature to be conservative. Deep Agents preserves all conversation history in its virtual filesystem, allowing for recovery of context post-summarization, but an erroneous context compression step is disruptive. We tested:我们将此功能调整得较为保守。Deep Agents 会在其虚拟文件系统中保留所有对话历史记录,允许在摘要后恢复上下文,但错误的上下文压缩步骤会造成干扰。我们测试了:
- A custom evaluation suite, in which we used (our own) LangSmith traces to inject follow-up prompts to threads that do and do not warrant compaction;一套自定义评估套件,我们使用(我们自己的)LangSmith 追踪向需要和不需要压缩的线程注入后续提示;
- Terminal-bench-2, in which we did not observe any instances of autonomous compaction;Terminal-bench-2,其中我们没有观察到任何自主压缩的实例;
- Our own coding tasks in Deep Agents CLI.我们在 Deep Agents CLI 中进行的编码任务。
In practice agents are conservative about triggering compaction, but when they do they tend to choose moments where it clearly improves the workflow.在实践中,智能体对触发压缩持保守态度,但当它们确实触发时,往往会选择那些能明显改善工作流的时刻。
Autonomous context compression is a small feature, but it points at a broader direction for agent design: giving models more control over their own working memory and fewer rigid, hand-tuned rules in the harness. If you’re building long-running or interactive agents, try it out in the Deep Agents SDK or CLI and let us know your feedback and what patterns you’d like to see it handle next.自主上下文压缩是一个小功能,但它指向了智能体设计的一个更广阔的方向:让模型更好地控制自己的工作记忆,减少框架中僵化、手动调整的规则。如果您正在构建长期运行或交互式的智能体,请在 Deep Agents SDK 或 CLI 中尝试一下,并让我们了解您的反馈以及您希望它接下来处理哪些模式。





