Hierarchical Memory and Background Reflection for AI Agents
This article explains why storing more data does not give AI agents useful memory. It covers how agent memory differs from ordinary context, a four-layer memory architecture, a background reflection process for consolidating and correcting notes, criteria for choosing what to remember, and methods for evaluating a memory system.
As AI agents begin to carry out multistep tasks, a single context window can no longer cover the full process. Whether an agent is running ongoing database analysis, tracking a long-term project, or exploring a complex environment, it needs to remember what has already happened, which conclusions have been verified, which strategies failed, and where to pick up next time.
However, letting an agent remember more does not mean it actually has memory. Unfiltered records can grow quickly, and errors can persist for a long time. The result may be higher retrieval costs, a context filled with irrelevant content, and agents that repeat the same mistakes. A usable memory system therefore depends less on storage capacity and more on three questions: what is worth remembering, how memories should be organized, and how to correct them when errors are found.
1. How Agent Memory Differs from Ordinary Context
From an engineering perspective, agent memory usually has three characteristics:
It comes from actual experience. Memory is not knowledge built into a model during pretraining. It is information the agent gathers while executing tasks, interacting with users, or calling tools.
It persists across sessions. After a session ends, the information remains in external storage and can be retrieved and used in later tasks.
It exists in a readable and updatable form. Memory is generally not written by changing model parameters. It is stored as text, structured data, vector records, or interlinked knowledge entries that the agent can read later.
For that reason, the following types of content usually do not count as cross-session memory:
- Temporary context in the current conversation window;
- One-time summaries generated when the context gets too long;
- Knowledge written into model parameters through retraining or fine-tuning;
- Execution traces that are only logged and never used by later tasks.
This distinction matters. Context answers “what the current task can see,” while memory answers “what future tasks can inherit.”
2. A Four-Layer Memory Architecture: From Raw Records to Automatic Injection
A robust design can split agent memory into four layers.
1. Raw Experience Layer
This is the lowest layer of data. It includes complete session transcripts, tool call results, environment feedback, user instructions, and intermediate states from task execution.
Raw records are complete, which makes them useful for tracing and correction later. They also contain a lot of noise, so loading them directly into context every time is not practical. This layer is better queried on demand using keywords, timestamps, task identifiers, or semantic search.
2. Memory Note Layer
The agent can distill important information into standalone notes, for example:
- Long-term preferences that users state explicitly;
- Business rules that have been verified;
- Success conditions for a particular way of operating;
- Reasons a given strategy failed;
- The current task’s goal, progress, and to-do items.
Notes should describe information that can be reused in the future, rather than copy conversation text. For example, “the user mentioned liking concise formatting in one conversation” has little value. “When writing technical proposals, prefer hierarchical headings, tables, and example code” is better suited as a long-term preference.
3. Structural Organization Layer
As the number of notes grows, a pile of separate files or records becomes hard to search. At that point, the system needs indexes, directories, tags, cross-references, or topic categories.
For example, the store might be organized like this:
Memory Store/├── User Preferences/├── Project Background/├── Verified Rules/├── Failure Cases/├── Current Task State/└── Overview Index/
The overview index does not need to hold every detail. It should describe where each type of information lives, what it applies to, and when it was last updated. This keeps the content loaded at startup small while helping the agent find key memories quickly.
4. Automatic Injection Layer
The top layer is what gets provided to the agent automatically at the start of each new session. This should not be the full memory store. It should contain only the summaries, rules, and index entries that the current task is most likely to need.
Too much injected content crowds out the useful context for the current task. Priority should go to:
- Stable background about the current user or project;
- Historical conclusions directly relevant to the current task;
- Known risks and errors that must not be repeated;
- Instructions on how to search the memory store.
These four layers form a process of progressive compression and refinement. The lower layers keep facts, and the upper layers provide actionable knowledge.
3. Why “Record While Executing” Is Not Enough
While an agent is working, it usually has to decide on the spot whether to save a piece of information. At that moment, it does not know what problems it will face later, so two kinds of errors are common.
The first is over-recording. The agent writes every action, observation, and tool return into memory, producing large amounts of duplicate information. Some experiments show that agents that over-record can generate nearly one million characters in a short period and still fail to complete the task effectively.
The second is under-recording. The agent saves only its final answer and skips the conditions it verified during reasoning, the failure paths it took, and the regularities it observed in the environment. The next time a similar problem appears, it has to start trial and error from scratch.
A better approach combines immediate recording with delayed consolidation:
- During execution, save potentially valuable raw facts first;
- After the session ends, decide which content remains valid over time;
- Merge duplicate notes;
- Downgrade or delete content that is no longer valid;
- Add evidence and applicable conditions to key conclusions.
4. Background Reflection: Consolidating Memory the Way Dreams Do
The consolidation that happens after a session ends can be called “background reflection.” It is not a mysterious capability. It is an independent, asynchronous processing pipeline, much like the way a person’s memories are sorted while they dream.
The basic workflow is:
- Retrieve the raw record of the session;
- Read the temporary notes created during the task;
- Check the notes against the raw record for accuracy;
- Decide whether each piece of information has long-term reuse value;
- Merge duplicate content and split mixed topics;
- Attach sources, timestamps, and scope of applicability to important conclusions;
- Update the index or knowledge structure;
- Mark outdated, conflicting, or unverified content.
The most critical step is checking the raw record. If consolidation works only from notes that were already generated, errors keep getting copied forward. Only by keeping the original experience can the system find cases where a note does not match the facts.
For example, suppose an agent recorded that “after repeated failures, switch to a different approach immediately.” The raw record shows that the working condition was actually “three consecutive failures, all occurring at the same step.” Background reflection can revise the vague conclusion into a conditional rule, which lowers the chance of misuse later.
5. Selection Matters More Than Quantity
High-quality memory should meet at least the following criteria.
Stability
The information should remain true for a long time. Project constraints, long-term user preferences, and verified business rules usually deserve saving more than one-time states.
Reusability
Can one memory help with many future tasks? If it applies only to a single operation, it belongs in task state, not long-term memory.
Verifiability
Does the conclusion have clear evidence behind it? For rules that the agent summarized on its own, record the source, the number of times it was verified, and the last updated time.
Information Density
Memory should express clear rules in as few words as possible, without copying whole conversations. High density does not mean extreme compression. It means removing irrelevant process and keeping conditions, conclusions, and exceptions.
Timeliness
Some information becomes invalid as project stages, user needs, or external conditions change. Memory therefore needs update times, validity scope, or expiration policies.
One general scoring idea can be used:
Memory value = stability × reusability × reliability × information density- redundancy cost - staleness risk
This does not have to become an exact number. It can serve as a judgment framework for prompt rules or post-processing logic.
