Context Engineering with Deep Agents: How To Remember Without Keeping Everything In Context
Long-running agents suffer "context rot" as tool output, history and instructions accumulate, so Deep Agents moves context out of the active window via middleware hooks in the agent loop rather than deleting it. FilesystemMiddleware offloads tool results over ~20,000 tokens (and user messages over 50,000) to files and shows only a preview. SummarizationMiddleware condenses older messages at 85% of the context window while keeping recent ones verbatim and writing the originals to recoverable files. SkillsMiddleware uses progressive disclosure to load task instructions only when relevant, subagents isolate noisy work in separate contexts, and MemoryMiddleware persists learned preferences in a plain markdown file that survives switching model providers.
Related posts
- Context Management for Deep Agents
LangChain mediumAdeptThe Deep Agents SDK manages long-running agent context with three compression techniques triggered at fractions of the model's context window. Tool results over 20,000 tokens are offloaded to a filesystem and replaced with a path plus a 10-line preview. Old write/edit tool inputs are replaced with file pointers once context passes 85%, and summarization runs as a last resort, producing a structured summary while saving the full history to disk for later retrieval. To evaluate these mechanisms, the team forces compression early (10-25% of the window) to generate more events and uses targeted evals such as needle-in-the-haystack recovery to catch goal drift and lost details.
- Organizing Context in a Multi-Agent Harness
LangChain mediumAdeptDeep Agents adds subagent context modes: "isolated" (the default, a fresh context window with only the supervisor's task description) and "fork" (the subagent inherits the supervisor's full conversation history). A forked subagent has the trailing tool call excised and the task rewritten as a user message, and its final message returns as the tool result. Because the shared prefix stays cacheable, forking avoids repeated file reads and context gathering. Use fork for workers continuing a diagnosed task or memory agents analyzing the conversation, and isolated for independent reviewers and parallel researchers, where inherited reasoning would anchor judgment or duplicate history.
- Context Engineering (Agent Memory) for AI Agents Starts with Retrieval
LangChain mediumAdeptAgent retrieval fails in three ways: inefficient searching, too much context at once, and irrelevant or stale context. The fix is to keep searches small and keep raw results out of the main agent's context window. Deep Agents does this with glob/grep before reads, paged read_file (100 lines by default with offset/limit), and automatic offloading of tool results over ~20k tokens to files with a preview. For custom RAG tools, the recommended pattern is "retrieve, offload, delegate": write results to files, return only paths, and have parallel subagents read each file and return key facts with sources and dates. Other advice covers using grep with synonyms before semantic search, agentic rather than single-shot retrieval, skills for progressive disclosure, and treating retrieved text as untrusted.
- Revamping Skills in Deep Agents
LangChain mediumAdeptDeep Agents now supports binding tools to Agent Skills, pinning skills at runtime, and reloading skills mid-thread, extending progressive disclosure for libraries that grow to thousands of skills. Bound tools, listed under `metadata.include_tools` or resolved from a label by a function that can gate on runtime permissions, stay out of context until the agent reads the skill. On newer Anthropic and OpenAI models they are injected via a new system message so the prompt cache prefix stays intact. Pinned skills (e.g. parsed from `/meeting-prep`) insert instructions before the first model call, saving a `read_file` round trip. Setting `skills_metadata` to None forces a rescan, at the cost of invalidating the cache when skills change.
- Managed Deep Agents delivers a better user experience for agents in production
LangChain mediumAdeptManaged Deep Agents 0.8 adds user-level memory, user-owned credentials, HTTP webhook channels, Slack file transfer and built-in Parallel web search. User memory is mounted at /memories/user/ keyed to the caller's authenticated identity, separate from shared agent memory at /memories/agent/, both backed by LangSmith Context Hub. Default access policies allow user memory only in Slack one-to-one DMs, so personal context does not leak into group threads or HTTP runs. Connections can be scoped per agent (e.g. web search) or per user (e.g. GitHub, Salesforce) so tools act with the caller's own permissions.