Stage 3: Context and Memory
Course: DeerFlow Source Code Class · Stage Test Prerequisite: Stage 2 Corresponding Overview: Context section of
core-mechanisms.mdGoal of this stage: Distinguish current messages, compressed results, checkpoints, and long-term memory.
1. What this stage addresses
Section titled “1. What this stage addresses”The model does not automatically “remember items.” Each round only sees the projected context:
ThreadState.messages Dynamic Date/Memory/Skill Reminder Current tool results - Old content over budget → This model requestDeerFlow uses various middleware to handle “what to inject,” “what to compress,” “how many tokens to spend,” and “which results can be replayed.”
2. Remember one judgment first
Section titled “2. Remember one judgment first”The current context is the projection, the checkpoint is the recovery point, and the memory is the cross-session fact.
The three have different lifecycles. If you treat memory as the current window, it will be repeatedly injected; If you treat checkpoint as chat history, events and deliveries will be lost; If you treat journal as model context, audit data will be stuffed into the prompt.
3. Source code main storyline
Section titled “3. Source code main storyline”agents/thread_state.pyDefines the graph state, with the core being message and runtime fields.DynamicContextMiddlewareInject dynamic reminders such as date and memory before and after the first HumanMessage to keep system prompts stable.MemoryMiddlewareRecovers facts that can be retained long-term inafter_agent;agents/memory/manager.pyResponsible for the backend.SummarizationMiddlewareMeasure and compress history before model calls to generate summaries to continue the conversation.TokenBudgetMiddlewareSet a hard upper limit on the entire round budget;TokenUsageMiddlewareRecord actual usage.DurableContextMiddlewareMake critical dynamic contexts recoverable, rather than existing only in a single request.- Worker saves graph status in Checkpoint;
RunJournalSaves running events and delivery facts.
4. Key Objects and Contracts
Section titled “4. Key Objects and Contracts”| Object | Save content | Typical error |
|---|---|---|
ThreadState.messages |
Current Graph Message | Treats the UI list as the model context |
| summarization Summary | Compressed Continuing Dialogue View | Treat the summary as the original archive |
| checkpoint | Graph State and Identity Recovery | Replace with a business database |
| memory | Cross-session preferences/facts | Full injection per round |
RunJournal |
Events, tokens, tools, delivery | Let the model read directly |
| token budget | This round’s cost cap | Usage limit is limited only at the UI layer |
5. An observable link
Section titled “5. An observable link”checkpoint read history → DynamicContext / memory injection → token measurement → Summarization Compress Tool Result Budget → Model Request → usage record New checkpointKey observation: messages shortening does not necessarily mean data loss; it may be a projection before sending; Checkpoint still retains recoverable state, journal still retains facts.
6. Experiment
Section titled “6. Experiment”Experiment A: Create a single compression
Section titled “Experiment A: Create a single compression”Lower the summarization threshold and send a sufficiently long conversation. Reference acceptance: If a summary or compressed tag appears in the model request, the original event can still be found as evidence on the journal/checkpoint link.
Experiment B: Compare memory switches
Section titled “Experiment B: Compare memory switches”Close MemoryMiddleware and repeat the same session. Reference observation: The current message remains, but the cross-session preference injection disappears; This indicates that memory and transcript are two separate paths.
Experiment C: Validation tool outputs budget
Section titled “Experiment C: Validation tool outputs budget”Allows the tool to return the extra-large text. Reference acceptance: The model sees truncated/summarized results, while the tool receipt can still locate the original output and truncate the facts.
Experimental Reference Answers
Section titled “Experimental Reference Answers”- The request side should have a summary or compressed tag; The original history should still be kept by journal/checkpoint/event chain; compression cannot destroy audit evidence.
- After closing memory, the current transcript still exists, but cross-session preference injection disappears; This proves that memory is an additional injection channel, not the session message itself.
- Model-side outputs should be bounded, and receipt/logs can still locate the original result and truncate facts; Truncation is explicit information, not silent loss.
7. Failure and recovery
Section titled “7. Failure and recovery”| Failure | Where the Facts Are | Principle of Restoration |
|---|---|---|
| Compression model failure | Original message and compression event | Retain the original history, retry or downgrade |
| Context still overruns | Token estimation and provider error | Remeasure, don’t pretend compressed |
| memory write failure | Current candidate fact | Do not block the main run, record failure |
| Checkpoint Corruption | Journal and Event | Rebuild from recoverable points, do not forge messages |
| Dynamic context duplication | Injecting middleware and first message | Deduplication, no modifying the original history |
8. Pass through customs and self-test
Section titled “8. Pass through customs and self-test”- Why does
messagesbeing short mean losing history? - Can Checkpoint, Journal, and Memory replace each other?
- Why should dynamic contexts avoid breaking system prompt prefixes as much as possible?
- Do tool output budgets and context compression solve the same problem?
Reference answers
Section titled “Reference answers”messagesis the projection sent to the current model round, which may have undergone summarization, tool output truncation, or dynamic injection. The recovery state is still at the checkpoint, and the process facts remain in the journal; Shortening the projection does not mean the canonical history has been deleted.- No. Checkpoint manages graph recovery, Journal manages process and auditing, Memory manages cross-session preferences; They face different lifecycle and consistency goals.
- Stable prefixes benefit prompt cache, reproducibility, and auditing. Placing variable content like dates and memories in controlled locations can avoid rewriting the entire system prompt every round.
- No. Context compression handles window pressure for message history; Tool output budget limits single tool results and aggregated results. Both affect tokens, but the target and recovery semantics differ.
9. Source code coordinates
Section titled “9. Source code coordinates”backend/packages/harness/deerflow/agents/thread_state.pybackend/packages/harness/deerflow/agents/middlewares/dynamic_context_middleware.pybackend/packages/harness/deerflow/agents/middlewares/summarization_middleware.pybackend/packages/harness/deerflow/agents/middlewares/memory_middleware.pybackend/packages/harness/deerflow/agents/memory/manager.pybackend/packages/harness/deerflow/runtime/journal.py
10. Next
Section titled “10. Next”Entering Stage 4: After the model sees the context, it decides to call the tool. Where does the tool ultimately run?