DeepAgents Memory: Long-Term Files, Session Messages, and Summaries Are Three Different Things
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.
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.
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.