AI news ·
LangChain deep agents uses offloading, summarization, and subagents to prevent context rot in long-running AI tasks
LangChain's Deep Agents prevents context rot by offloading large tool results to disk once they exceed 20,000 tokens and summarizing older messages when usage hits 85% of the context limit.

Long-running AI agents face a reliability problem called context rot. As an agent accumulates instructions, tool outputs, and conversation history, it eventually carries more information than the model's context window can handle. The result: accuracy and reasoning degrade as the model struggles to prioritize what matters. LangChain's open-source Deep Agents harness addresses this by moving context out of the active window without deleting it, using a set of techniques built into the agent's runtime loop.
The approach matters for product teams building agents that handle multi-step research, data analysis, or reporting tasks. Without context engineering, an agent that starts strong will produce worse output the longer it runs. Deep Agents stores memory as plain files in storage you control, so teams can switch models or providers while preserving everything the agent has learned.
How middleware manages context during an agent run
An agent runs in a loop: the model decides what to do, calls a tool, reads the result, and repeats until the task finishes. Middleware is code that plugs into specific points in that loop - before the agent starts, around each model call, or around each tool call. At each point, middleware can read the context and change it before the next step runs. Every context engineering technique in Deep Agents is a piece of middleware hooked into one of those points.
The techniques fall into two categories: preventing unnecessary information from entering the context window, and managing what is already there. Most of this is built in, so developers do not need to write middleware to use these techniques unless they want to customize something specific.
Five techniques that prevent context rot
Offloading keeps large tool results and user messages out of the active context. When a tool result exceeds roughly 20,000 tokens, FilesystemMiddleware writes the full output to disk and shows the model only a short preview plus the file path. Oversized human messages get the same treatment at a 50,000-token threshold. The agent can page through the full file later using a read_file tool if it needs more detail.
Summarization compacts what has already accumulated. SummarizationMiddleware checks token usage before each model call. Once usage crosses 85% of the context limit, a separate LLM call condenses older messages into a single summary. The summarized messages are written to a recoverable file at /conversation_history/{session_id}.md, so the agent can retrieve details later. As the run continues and usage climbs again, the next summary includes the previous one.
Skills load task-specific instructions only when the model decides they are relevant. A skill is a folder of instructions with optional scripts and reference docs. SkillsMiddleware scans the skills folder at startup and reads only the header of each SKILL.md file. On every model call, it adds those names and short descriptions to the system prompt. The full instructions enter context only when the model reads the skill file. This progressive disclosure means ten skills cost about as much context as ten one-liners.
Subagents isolate noisy work in a separate context window. SubAgentMiddleware lets developers customize the system prompt and tools available to subagents. The main agent hands off a scoped task - such as researching a single company across dozens of searches - and receives only the final result. The main agent never sees the intermediate noise. Subagent tasks can often run on cheaper models and in parallel.
Long-term memory persists what the agent learns across conversations. Memory is a markdown file that MemoryMiddleware reads at the start of a run, injects into the system prompt on every model call, and updates when the agent learns something worth keeping. Because the file lives in storage you control, you can point a different model at the same memory file next week and it will still follow previously learned preferences.
A real-world example: competitive analysis agent
Consider an agent that researches five competitors every Monday and writes a report. A single run pulls market data exports with tens of thousands of rows, searches news, reads articles, takes notes, and drafts and formats the report. By the final step, the agent has carried five raw exports, dozens of search results, and two stale drafts through every model call - most of it irrelevant for formatting.
With offloading, each oversized market report gets saved to disk and the model sees only a preview. Summarization condenses the growing conversation when it nears the limit. A subagent handles the noisy research for each company and returns three bullets. Skills load the report template only when formatting begins. The result: the main agent carries only what it needs at each step.
Why this matters for product development
Building agents that degrade over long runs is a fast path to unreliable output and frustrated users. The techniques in Deep Agents give development teams a set of defaults that prevent context rot without requiring custom middleware for every project. Large inputs get offloaded rather than truncated, compaction remains recoverable, task know-how loads on demand, noisy work stays isolated, and memory persists in storage the team controls. Thresholds for offloading and summarization are configurable, because defaults that suit one task rarely suit every task. Teams evaluating agent frameworks can use these five techniques as a checklist for whether a harness will hold up under real workloads.