跪拜 Guibai
← All articles
Frontend · JavaScript · Agent

DeepAgents Memory: Long-Term Files, Session Messages, and Summaries Are Three Different Things

By YIAN ·
Read original on juejin.cn ↗ Google Translate ↗ Alt translation

Agent memory breaks when one layer is forced to do another's job: a file that is never injected stays invisible, and a chat history that is never summarized blows the context window. Splitting persistence from visibility is what lets an agent recall across sessions while still working within a token limit.

Summary

DeepAgents keeps three separate memory layers. `MemoryMiddleware` reads configured files such as `AGENTS.md` and `preferences.md` and injects their contents into the system prompt on every `invoke()`. It never writes to those files; the agent must call `write_file` or `edit_file` to persist facts. `SummarizationMiddleware` deals with an entirely different problem: when a conversation grows past a trigger threshold (8 messages in the example), it archives the old raw messages to `conversation_history/*.md`, creates a summary `HumanMessage`, and only then sends the summary plus the most recent messages to the model. The full message list in state remains intact; only the model input gets compressed.

Because the two middlewares are often conflated, common bugs follow. Using `write_file` to update an existing memory file wipes prior content; `edit_file` is the partial update tool. Testing long-term memory inside the same session proves nothing, because the model sees earlier messages. After summarization, only saving `messages` back to the next call drops the `_summarizationEvent` and `_summarizationSessionId` metadata, leading to repeated summarization. Documents persist data while messages make data visible to the current model call; both are needed.

Takeaways
— MemoryMiddleware reads configured sources and appends their content to the system prompt on every invoke, but never writes to those files.
— SummarizationMiddleware triggers when messages reach 8, keeps the latest 4, and archives older messages to `conversation_history/*.md`.
— `write_file` overwrites an entire file; `edit_file` does partial string replacement, so existing memory files should be updated with `edit_file`.
— To verify long-term memory, query from a fresh session; same-session follow-ups can leak the answer through chat history.
— After summarization, `state.messages` still holds the full history; only the model input gets compressed to the summary plus post-cutoff messages.
— Saving only `messages` between invocations drops `_summarizationEvent` and `_summarizationSessionId`, causing duplicate summarization; preserve the full state or use a checkpointer with `thread_id`.
— Summary archive files do not automatically become long-term memory; agents need file tools to read them on demand.
— The recommended memory write rule: use `write_file` to create missing files, then `read_file` plus `edit_file` to update existing ones.
Conclusions

The distinction between persistence and visibility explains why many agent memory failures are design confusion rather than model incompetence: a fact can be on disk yet hidden from the model unless an explicit middleware injects it.

Recommending a deterministic `appendMemory` tool over natural-language instructions suggests agent memory is shifting from prompt discipline to code-enforced guarantees.

Inspecting `state.messages` after summarization can mislead developers into thinking compression failed; the compression only affects the model input, not the stored state.

Concepts & terms
MemoryMiddleware
A DeepAgents middleware that reads configured memory files and appends their contents to the system prompt on every invoke. It does not write or update those files; writing is left to file tools.
SummarizationMiddleware
A DeepAgents middleware that compresses long conversations by archiving older messages to Markdown, generating a summary message, and passing only the summary plus recent messages to the model on the next invoke.
Checkpointer
A LangGraph feature that persists the full agent state under a fixed `thread_id`, allowing a conversation to resume without manually carrying `messages` or internal metadata like summarization events.
Source: juejin.cn ↗ Google Translate ↗ Backup ↗