Skip to the yard
Waxsealai

Deep Agents Uses Five Techniques to Keep Long-Running AI Work in Context

Shipped
Shipper
Michael Brown
Readers
739

Deep Agents manages growing agent histories with file offloading, summaries, on-demand skills, subagents and portable memory.

Deep Agents Uses Five Techniques to Keep Long-Running AI Work in Context

Long-running agents need access to useful history without carrying every tool result and instruction in every model call. LangChain’s open source Deep Agents harness addresses that tradeoff with five techniques: offloading large inputs to files, summarizing older conversation, loading skills on demand, assigning noisy tasks to subagents, and saving selected information as long-term memory.

The underlying problem is often called context rot. As an agent gathers instructions, files and tool results, its active context grows. A large history can crowd out relevant details and make accurate responses harder. Simply retrieving only the information needed for a particular step helps, but it does not remove irrelevant work already accumulated during a long run. A harness has to manage both what enters the context window and what remains there.

Deep Agents wraps a model with middleware: code that can inspect or change context at points in the agent loop, such as before a run, around a model call or after a tool returns. The harness applies these interventions as the task progresses, so the model can work with a smaller active context while retaining access to stored detail.

Keep large results available without feeding them all to the model

In a competitive analysis workflow, an agent might review five companies, collect market data and news, then produce a report in a team template. Each company’s export could contain thousands of rows. Passing every export, search result and draft through every later call wastes context, especially when the final step is formatting.

Deep Agents’ FilesystemMiddleware handles large tool results. When a result exceeds roughly 20,000 tokens, the middleware writes the full output to /large_tool_results/{tool_call_id} before it enters the conversation. The model receives a short preview, containing the first and last few lines, along with the file path. If it later needs a particular figure, it can use read_file to inspect the stored result.

Oversized user messages, such as a pasted call transcript, follow a similar path at a higher threshold: 50,000 tokens. They are saved under /conversation_history/. This preserves the complete input for later retrieval while limiting what the model must process immediately. It also avoids the information loss that comes from simply cutting off the end of a long result.

Summarize accumulated history, with a route back to detail

Offloading controls sudden jumps in context size, but ordinary conversation still accumulates. Research notes, intermediate decisions and draft revisions can gradually fill the window. Deep Agents’ SummarizationMiddleware checks token usage before each model call and, by default, triggers when usage reaches 85% of the model’s context window.

At that point, a separate model call condenses older messages into a summary. The older messages leave the active context, while the most recent messages stay verbatim. The next call sees the summary first and the recent conversation after it, preserving fresh details without paraphrasing them.

The summarized material is also written to /conversation_history/{session_id}.md. If the agent needs an exact detail that the summary left out, it can read the saved history. As the run continues and context usage rises again, a later summary incorporates the previous one. Developers can also trigger summarization when the task changes topic, and customize the summarization prompt to suit their workflow.

That recoverability matters in the competitive analysis example. A compact summary may be enough to draft the report, while a specific number or research note could still be retrieved from the stored conversation if needed.

Load task instructions only when they apply

Some instructions matter only for particular jobs. A quarterly business review template, for example, may be needed once a quarter but not during routine research. Keeping the full template in the system prompt makes it part of every model call, even when it has no bearing on the task.

Deep Agents represents specialized guidance as a skill: a folder containing instructions and, optionally, scripts or reference documents. SkillsMiddleware scans the skills folder before a run and reads the header of each SKILL.md. It adds the skill names and short descriptions to the system prompt. When a skill is relevant, the model can load its full instructions using read_file.

This staged approach is progressive disclosure. The model starts with a brief menu of available capabilities and retrieves the detailed recipe only when useful. In the example, the agent can research companies without carrying the report template; it loads the relevant instructions when it is time to format the quarterly review.

Move noisy research into a separate context

File offloading is useful when one result is too large to keep in the conversation. Subagents address a different case: work that is predictably noisy, such as researching a company through many searches and pages. A subagent performs that work in its own context window and returns a smaller result to the main agent.

For market research, a developer could define a market-researcher subagent with access to pull_market_report, instruct it to return the three most notable developments, and tell it not to return the raw report. The subagent reads the full report in its own run; the main agent receives only the three bullets. It does not need to process even a preview of the raw export.

SubAgentMiddleware lets developers customize the subagent’s prompt and available tools. Since these tasks can be scoped narrowly, they may also run on a less expensive model and in parallel. Whether that is practical depends on the task and the quality required from each result.

Carry selected memory between runs

Long-term memory is for information that should persist across conversations, such as a user’s preference for bullet-point summaries. In Deep Agents, memory is stored in a Markdown file in a storage backend the developer controls. MemoryMiddleware reads the file at the start of a run, adds its contents to the system prompt on model calls, and can update it when the agent learns something worth keeping.

Because the memory lives in a file rather than inside a model provider’s private state, the same stored information can be used with a different model. A Deep Agents setup can point to a file such as /memories/AGENTS.md while changing the model configuration. That makes preferences and other saved guidance portable across providers, provided the new setup can access the same backend and store.

Persistent memory still needs judgment: an agent may learn information that is useful only once, or save a preference that later changes. The design puts the memory in inspectable storage, but teams still need to decide what belongs there and how updates should be handled.

Design choices for context management

The five techniques address different sources of context growth. Offloading preserves large raw inputs while showing the model a preview. Summarization compresses older interaction while keeping the original history available. Skills defer specialized instructions. Subagents isolate work likely to generate a lot of intermediate material. Long-term memory carries selected knowledge into later runs.

These mechanisms are most useful when their defaults fit the workload. The 20,000-token tool-result threshold and 50,000-token message threshold are starting points, as is the 85% summarization trigger; developers can customize behavior for their models and tasks. Retrieval remains part of the picture too: the agent needs a way to find the right stored material when the preview or summary is insufficient.

Deep Agents can be installed with uv add deepagents. The broader engineering lesson is to treat context as a managed resource: keep the active window focused, retain raw information in retrievable storage, and load task-specific knowledge when it becomes relevant. For long-running workflows, the quality of those decisions can shape how reliably an agent carries a task from research through its final output.

Source: Michael Brown · www.langchain.com

Same vesselRelated cargo