Context Management for Deep Agents: A Technical Guide

Context Management for Deep Agents

Long-running agents fail in a specific way: the context window fills with the debris of past steps — full file reads, redundant tool arguments, stale conversation — and the model’s answers degrade. This is context rot. The LangChain Deep Agents SDK counters it with a filesystem abstraction the agent can offload to and retrieve from, plus a set of compression techniques that fire as the window fills.

The principle behind all of them is the same: reduce what’s in working memory while keeping everything recoverable from disk.

Three compression techniques

They trigger at rising thresholds of the context window, escalating from cheap to aggressive.

1. Offload large tool results. When a tool returns a large response — say, reading a big file — the SDK writes the full result to the filesystem and leaves only a file-path reference plus a short preview in active context. If the agent needs the detail, it reads it back with read_file or search. The result exists; it just isn’t clogging the window.

2. Offload large tool inputs. Write and edit operations leave large, already-persisted arguments sitting in history. Once context crosses a high threshold (around 85%), the SDK truncates those older tool calls and replaces them with a pointer to the file on disk — pruning data that’s redundant because it already lives elsewhere.

3. Summarize. When offloading isn’t enough, the SDK falls back to summarization, in two parts. An LLM writes a structured summary of the session — intent, artifacts created, next steps — and that replaces the full message history in working memory. Meanwhile the complete original transcript is written to the filesystem as the canonical record. The agent keeps high-level awareness of its goal while retaining the ability to search back for any specific detail.

Evaluating compression

Broad benchmarks are a poor way to test this, because compression events fire only occasionally in a normal run — you can’t isolate their effect. Two targeted approaches work better.

Stress-test by forcing it. Lower the compression threshold artificially — say to 20% of the window instead of 85%. That makes compression fire constantly, so you can isolate its impact and compare configurations (for instance, two summarization prompts) cleanly.

Test one mechanism at a time. A needle-in-the-haystack test plants a key fact early in a conversation, forces a summarization event, and then asks the agent to recall the fact later. If it succeeds, you’ve proven it can recover information from the filesystem after that fact left active context.

When you evaluate, focus on three things:

  • Baseline — run the agent on representative tasks first, so you know what “working” looks like before compression.
  • Recoverability — confirm critical information stays reachable after being offloaded or summarized.
  • Goal drift — watch for the agent losing the user’s original intent after a summarization. This is the most insidious failure mode, and forcing frequent summarization is the fastest way to surface it.

Compression plus this kind of targeted evaluation is what lets an agent run for hours on a complex task without its context quietly rotting out from under it.

This entry was posted in . Bookmark the permalink.