Context in Deep Agents: what an agent does when its memory fills

Context in Deep Agents: what an agent does when its memory fills
20,000tokens: above that, a tool response goes to disk
85%of the window: when Deep Agents truncates and summarizes by default
10-20%of the window: where to force summarization so you can measure it
3tips to evaluate your own strategies
What does your AI agent do when its memory fills up? How Deep Agents manages context
I also tell the story in this ten-minute video on my channel Articulator EN.

Why context needs managinglink

Agents are taking on longer and longer tasks. And the longer a task runs, the more the agent piles up in its context window: messages, tool calls, their results, files it has read…

That brings two problems. The first is obvious: the model's window is finite. The second shows up long before it fills: with so much text, the model makes worse use of what matters. That's context rot.

The two problems of long tasks: the window is finite and the context degrades.
Two problems: the window runs out and, before that, the context degrades.

The answer is context compression: shrinking what the agent keeps in its working memory without losing the details it needs to finish the task. That can mean summarizing what has already happened, removing stale information, or carefully deciding what stays and what goes.

Deep Agents and its filesystemlink

Deep Agents is LangChain's agent harness: open source and batteries included. With it, an agent can plan, spawn subagents and work with a filesystem to carry out complex, long-running tasks.

That filesystem is the key piece of all the context management. The agent has tools to list, read, write and search files, and even execute them. That way, whatever leaves the working memory isn't lost: it goes to disk, and the agent looks it up and retrieves it when it needs it.

The agent's tools over the filesystem: list, read, write, search and execute.
Whatever leaves working memory goes to disk, and the agent retrieves it when it needs it.

Three techniques, three rhythmslink

On top of that, Deep Agents compresses context with three techniques, each on its own schedule:

1. Large tool results

It offloads huge tool responses to disk as soon as they show up.

2. Large tool inputs

It removes the large arguments of old tool calls from the history once the context crosses a threshold.

3. Summarization

It summarizes the conversation when there's nothing left to offload.

The thresholds aren't a fixed number of tokens but fractions of each model's window. LangChain keeps model profiles that record the maximum each model accepts, and Deep Agents compresses when the context reaches a certain percentage of that figure.

The three techniques in Deep Agents, each on its own schedule: large results, large inputs and summarization.
Three techniques, from the cheapest to the most drastic.

1. Large results go to disklink

When the agent reads a huge file or queries a web service, the tool response can be so large that it doesn't even fit in the model's window.

So when Deep Agents detects a response of more than 20,000 tokens, it saves it to a file on disk. In the context it leaves only the path to that file and a preview with its first ten lines. If it needs more later, the agent reads the file again or searches it for what it's after: the full response is still within reach, but it no longer takes up working memory.

A tool response over the 20,000-token threshold is saved to disk; the path and a preview stay in the context.
Over 20,000 tokens: to disk, with the path and a preview left in context.

A detail I checked in the Deep Agents code: the 20,000-token threshold and the /large_tool_results/ folder match the article, but the current version no longer shows just the beginning; it shows the beginning and the end of the response.

2. Truncating large inputslink

The second technique looks at the other side: not at what tools return, but at what they receive. When the agent writes or edits a file, the tool call carries the full content inside it, and it stays in the conversation history.

But that content is already saved on disk. Keeping it in the history too is redundant: it could be five hundred lines of code taking up space, turn after turn. So, when the conversation goes past 85% of the window, Deep Agents truncates the oldest calls: it replaces their content with a pointer to the file on disk, and the active context slims down. The most recent ones stay as they are.

The context gauge passes 85%: the oldest write_file calls are truncated and left as pointers to the file.
Past 85%, old calls are reduced to a pointer to the file.

3. Summarizing without deleting anythinglink

When offloading to disk no longer frees up enough space, Deep Agents falls back to summarization, in two parts:

  1. A summary in memory: a language model writes a structured summary of the conversation, with the session intent, the artifacts created and the next steps. That summary replaces the full history in the agent's working memory.
  2. The full history, to disk: the original messages are written to a file as the canonical record of everything that happened. The summary points to where it is, in case it's needed again.
The structured summary with the session intent, artifacts created and next steps replaces the history in memory.
The structured summary replaces the history; the originals are saved to disk.

With this two-part approach, the agent knows where it stands and where it's heading thanks to the summary, and if it needs a specific detail it retrieves it by searching the disk. The article shows this with a real trace, where the model uses its read_file tool to bring back messages that had already been summarized.

How do you know it works?link

That's the machinery. But to me, the most interesting part of the article is the second one: how do you know it actually works? In real tasks, like those in Terminal Bench, compression only kicks in every now and then. With so few events, it's hard to isolate what each technique contributes.

LangChain's trick is to trigger it on purpose. If you summarize at 10 or 20% of the window, the agent performs somewhat worse overall, but it summarizes far more often. It's not a setting for production, it's for testing: with so many events, you can now compare different configurations, for example, two versions of your summarization prompt.

Summarizing at 10 or 20% of the window lowers performance a bit, but summarization events multiply.
Forcing summarization: slightly worse performance, far more events to study.

That's how they found a simple improvement: the agent worked better when they added two dedicated sections to the summarization prompt, the session intent and the next steps. In the LangChain PR that changed that prompt, the new one ends up with four sections: session intent, summary, artifacts and next steps.

The chartlink

The article illustrates this with a chart: runs of Claude Sonnet 4.5 on Terminal Bench 2, compressing at 25% of the window instead of the default 85%. Each line tracks the context tokens turn by turn.

  • The green line: past turn 20, a summary sinks the context in one go, from about 50,000 tokens to about 15,000.
  • The orange line: near turn 40, the drop is smaller. It's not a summary: a call that wrote a large file gets evicted from the context.
The tokens-per-turn chart, redrawn: the green line drops sharply because of a summary and the orange one falls less when a call is evicted.
Redrawn from the article's figure (the numbers are read by eye).

More events, more data to study them. I read the size of the drops from the figure, since the article only says "around turn 20" and "around turn 40".

Targeted evalslink

Beyond that, Deep Agents has a set of targeted evals: deliberately small tests, each one isolating a single compression mechanism. So when something breaks, the failure is obvious and easy to debug.

They're not trying to measure how well the agent solves tasks in general, but to check that the harness doesn't get in its way. Two examples:

Did the summary preserve the goal?

They force a summary halfway through a task and check whether the agent keeps going. Saving the state isn't enough: it also has to stay on course.

Can it recover what the summary took away?

They hide a fact at the start of the conversation, like a needle in a haystack, force a summary, and later the task requires that fact. It's no longer in the context: the agent can only find it by searching the disk.

The needle in a haystack: a fact hidden early in the conversation is summarized away and the agent finds it in the history saved to disk.
The needle in a haystack: the fact is no longer in context, but it is on disk.

They work like integration tests for context management. They don't replace full benchmarks, but they speed up every iteration a lot, and when something breaks you know which mechanism it was, instead of blaming the agent in general.

Three tips to evaluate your compressionlink

The article ends with three tips for anyone evaluating their own compression strategies:

1. Real benchmarks, then stress

Measure your harness on representative tasks to get a baseline. Then force compression, at 10 or 20% instead of 85%, and compare alternatives.

2. Test recoverability

Compression only helps if the critical stuff is within reach. Test that the agent keeps heading toward its goal and can pull up specific details, like the needle.

3. Watch for goal drift

The most treacherous failure: after a summary, the agent loses sight of what you wanted. It asks for clarifications it didn't need or declares the task done without finishing it.

Goal drift: after a summary, the agent asks for needless clarifications or declares the task done without finishing it.
Goal drift, the most treacherous failure.

There are subtler deviations, harder to pin on the summary. Forcing frequent summaries across a test suite helps bring them to light.

In shortlink

Deep Agents manages context in three tiers: it offloads huge responses to disk, truncates old calls and, as a last resort, summarizes without deleting anything. The whole harness is open source, so you can try it.

In one sentence

To know whether your compression works, don't wait for it to kick in: force it and test it with small tests.

What do your agents do when their memory fills up?

Sourceslink

Continue reading

Last posts -->

Have you seen these projects?

Gymnasia

Gymnasia Gymnasia
Expo
React Native
TypeScript
OpenAI
Anthropic

Fitness app with two agents that run entirely on the device, with no backend, so the user's data never leaves the phone. A BYOK conversational coach with local tools and a remote system prompt with offline fallback, plus a food estimator that pulls macronutrients out of a photo of the plate, with barcode scanning against OpenFoodFacts.

LangGraph Deep Researcher

LangGraph Deep Researcher LangGraph Deep Researcher
Python
LangGraph
FastAPI
React
TypeScript
Docker

Multi-agent research system built with LangGraph. A supervisor breaks your question down into topics and launches search sub-agents in parallel; each one compresses its findings before handing them to a writer agent that produces the final sourced markdown report. Live streaming over WebSockets, a configurable model per role and bring-your-own API keys that are never persisted server-side.

Tau

Tau Tau
Python
LangChain

Multi-agent tutoring system for secondary school students, with one agent per subject and course material written and validated by a team of teachers. It was used with real students at a private school in Spain and at a secondary school in Colombia.

View all projects -->
>_ Available for projects

Do you have an AI project?

Let's talk.

maximofn@gmail.com

Machine Learning and AI specialist. I develop solutions with generative AI, intelligent agents and custom models.

Do you want to watch any talk?

Last talks -->

Do you want to improve with these tips?

Last tips -->

Use this locally

Hugging Face spaces allow us to run models with very simple demos, but what if the demo breaks? Or if the user deletes it? That's why I've created docker containers with some interesting spaces, to be able to use them locally, whatever happens. In fact, if you click on any project view button, it may take you to a space that doesn't work.

Flow edit

Flow edit Flow edit

FLUX.1-RealismLora

FLUX.1-RealismLora FLUX.1-RealismLora
View all containers -->
>_ Available for projects

Do you have an AI project?

Let's talk.

maximofn@gmail.com

Machine Learning and AI specialist. I develop solutions with generative AI, intelligent agents and custom models.

Do you want to train your model with these datasets?

short-jokes-dataset

HuggingFace

Dataset with jokes in English

Use: Fine-tuning text generation models for humor

231K rows 2 columns 45 MB
View on HuggingFace →

opus100

HuggingFace

Dataset with translations from English to Spanish

Use: Training English-Spanish translation models

1M rows 2 columns 210 MB
View on HuggingFace →

netflix_titles

HuggingFace

Dataset with Netflix movies and series

Use: Netflix catalog analysis and recommendation systems

8.8K rows 12 columns 3.5 MB
View on HuggingFace →
View more datasets -->