
Context Rot: How Deep Agents Keeps Long-Running Agents Focused
Agentic AIA competitive-analysis agent that researches five companies every Monday will, by the time it formats its report, have carried five raw data exports, dozens of search results and two stale drafts through every model call. That is the example LangChain uses to explain context rot: the more history, noise and tool output a model carries, the worse its accuracy and reasoning tend to get. For developers building long-running agents, the fix LangChain describes is not a bigger window but moving context out of it without deleting anything.
How It Works
Deep Agents, LangChain’s open-source agent harness, implements five techniques as middleware, code that plugs into fixed points in the agent loop: before the run starts, around each model call, or around each tool call. Most of it is built in.
- Offloading. FilesystemMiddleware runs on every tool call. When a result exceeds roughly 20,000 tokens, the full output is written to
/large_tool_results/{tool_call_id}and the model sees only the first and last few lines plus the file path. It pages through the file withread_filewhen it needs a number. Oversized user messages, such as a pasted transcript, get the same treatment at 50,000 tokens. - Summarization. SummarizationMiddleware checks token usage before each model call. At 85% of the model’s context window by default, a separate LLM call condenses older messages into a summary while recent messages stay verbatim. The summarized messages are written to
/conversation_history/{session_id}.md, so the agent can read details back later, and each new summary includes the previous one. - Skills. A skill is a folder of instructions, optionally with scripts and reference docs. SkillsMiddleware reads only each SKILL.md header and adds names and descriptions to the system prompt; the full instructions load only when the model decides they’re relevant. LangChain says ten skills cost about as much context as ten one-liners.
- Subagents. Work known to be noisy runs in a subagent’s own context window. In the example, a market-researcher subagent reads the full report and returns three bullets, so the main agent never sees even a preview of the raw data. LangChain notes these small, well-scoped tasks can often run on a cheaper model and in parallel.
- Long-term memory. Memory is a markdown file. MemoryMiddleware loads it at the start of a run, injects it on every model call, and updates it when the agent learns something, such as a preference for bullet points.
What This Means for You
The design choice that travels beyond LangChain is recoverability. Truncation throws data away; offloading and recoverable summaries keep it on disk where the agent can fetch it. LangChain’s checklist of principles applies to any harness: offload large inputs instead of truncating, make compaction recoverable, load know-how on demand, isolate noisy work, own your memory, and make defaults customizable, since thresholds that suit one task rarely suit every task.
The memory point has a practical edge. Because Deep Agents stores memory as a plain file in storage the developer controls, the same memory can be pointed at a different model provider; the post shows the same AGENTS.md file used with an OpenAI model. In our reading, that makes a model switch less costly for teams that change providers. Stack Archive covered a related result in AI Agents Win by Throwing Away the Chat Log, and the risk of compression done badly in Your AI Agent Is Silently Ignoring Your Instructions.
The So What
Developers whose agents run long research or coding tasks should audit two things this week: whether large tool results are truncated rather than written to files the agent can reread, and whether task-specific instructions sit in the system prompt on every call. Move both out first. Then review the 85% summarization trigger against their own workloads, since LangChain itself says thresholds that suit one task rarely suit every task.
Source: LangChain Blog
Content created with AI assistance and reviewed for accuracy.
Join the conversation
Stack Insiders is our free community for readers who want to go deeper — share resources, ask questions, and connect with others across every vertical we cover.
Join Stack Insiders →