LangChain Deep Agents 上下文管理技巧
LangChain 博客发文介绍其开源智能体框架 Deep Agents 如何应对“上下文腐化”:长任务中指令、工具输出和文件不断累积,超出模型上下文窗口后会导致准确率和推理能力下降。文章列出 Deep Agents 采用的五种方法:将大型工具结果卸载到文件系统、在接近上限时压缩较早消息、按需加载技能、用子智能体隔离嘈杂工作,以及把长期记忆保存为纯文件。LangChain 称记忆存放在用户自己控制的存储中,因此切换模型或服务商后仍可保留已学内容,相关阈值和中间件行为均可配置。
文章给出了文件卸载、摘要阈值、子智能体和文件记忆等具体机制,智能体开发者即使不使用 Deep Agents 也可以借鉴这些上下文工程做法。
Key Takeaways
- "Context rot" happens as long-running agents build up more context than the model can hold, resulting in accuracy drops.
- Deep Agents, LangChain's open source agent harness, uses five techniques to move context out of the active window (without deleting it) to prevent context rot.
- An agent's memory in Deep Agents is stored as plain files in storage you control, so you can switch models or providers while keeping everything it has learned.
If you've ever crammed for an exam the night before, you know the feeling: the more you study, the more the early chapters start to slip.
Agents have the same problem. As they take on longer tasks, they take on more and more information (instructions, tool results, files etc.); eventually this becomes far too much context to comfortably fit in a model's context window. The more history, noise, and tool output a model carries, the worse its accuracy and reasoning tend to get.
In the world of agent engineering, we call this context rot. This blog covers techniques to manage your agent’s context window and how the Deep Agents harness uses a concept called middleware to inject them into your agent’s run.