I. Why Are Memory Retrieval and Updates So Important?
An agent's memory system typically includes short-term memory and long-term memory, but the existence of memory data does not mean the agent can use it effectively.
Suppose the user previously told the agent:
The backend project mainly uses Go, and the database is PostgreSQL.
A few days later, the user asks:
Help me design an order service.
The agent needs to accomplish the following:
- Determine whether the current task requires historical memory.
- Search the memory store for information related to the order service.
- Find the technology stack the user provided earlier.
- Determine whether this information is still valid.
- Inject the relevant memories into the current model context.
- Update existing memories based on information generated by the new task.
If retrieval fails, the agent cannot leverage historical information.
If outdated information is retrieved, the agent may make incorrect decisions.
If the update mechanism is flawed, duplicate, conflicting, and erroneous information will keep accumulating.
Therefore, the memory system actually contains two interrelated core processes:
- Memory Retrieval: finding the information needed for the current task from existing memories.
- Memory Update: modifying, merging, supplementing, or deleting existing memories based on new information.
Together, the two constitute the dynamic management mechanism of agent memory.
II. The Overall Architecture of Memory Retrieval and Updates
From a system architecture perspective, memory retrieval and updates are usually coordinated centrally by an independent Memory Manager.
The main components are as follows:
| Component | Core Responsibility |
|---|---|
| Memory Manager | Centrally manages the memory lifecycle |
| Query Analyzer | Analyzes the current task and retrieval intent |
| Query Rewriter | Converts user requests into retrieval queries |
| Memory Retriever | Recalls candidate memories from different stores |
| Reranker | Re-ranks candidate memories |
| Memory Validator | Validates the validity and credibility of memories |
| Context Builder | Organizes relevant memories into the model context |
| Memory Extractor | Extracts candidate memories from new interactions |
| Memory Merger | Handles duplicate and similar memories |
| Conflict Resolver | Resolves conflicts between old and new memories |
| Memory Store | Persists memories |
| Lifecycle Manager | Handles expiration, archiving, and deletion |
The overall workflow:
flowchart TD
A["Agent Runtime"] --> B["Memory Manager"]
B --> C["Memory Retrieval Module"]
B --> D["Memory Update Module"]
C --> E["Query Analysis"]
E --> F["Query Rewrite"]
F --> G["Multi-channel Memory Recall"]
G --> H["Candidate Memory Reranking"]
H --> I["Validity Check"]
I --> J["Context Assembly"]
D --> K["Candidate Memory Extraction"]
K --> L["Memory Classification and Evaluation"]
L --> M["Deduplication and Merging"]
M --> N["Conflict Detection"]
N --> O["Memory Persistence"]
O --> G
J --> P["Model Reasoning and Task Execution"]
P --> D
style A fill:#DCEFE5,stroke:#4F8B70,color:#24563F
style B fill:#EAF3EC,stroke:#83A995,color:#315D49
style C fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
style D fill:#EAE8FA,stroke:#A49BD5,color:#51477D
style E fill:#F5F8F4,stroke:#B5CBBB,color:#40594A
style F fill:#F5F8F4,stroke:#B5CBBB,color:#40594A
style G fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
style H fill:#EAE8FA,stroke:#A49BD5,color:#51477D
style I fill:#F7F4E8,stroke:#C7B77A,color:#675A2D
style J fill:#DCEFE5,stroke:#4F8B70,color:#24563F
style K fill:#EAF3EC,stroke:#83A995,color:#315D49
style L fill:#F5F8F4,stroke:#B5CBBB,color:#40594A
style M fill:#EAE8FA,stroke:#A49BD5,color:#51477D
style N fill:#F7F4E8,stroke:#C7B77A,color:#675A2D
style O fill:#DCEFE5,stroke:#4F8B70,color:#24563F
style P fill:#DCEFE5,stroke:#4F8B70,color:#24563F
It should be noted that memory retrieval and memory updating are not two completely independent stages.
Retrieval results may reveal that a memory has expired or conflicts with another, thereby triggering an update; new facts produced during task execution may also affect the next retrieval after being updated.
III. The Complete Process of Memory Retrieval
Memory retrieval is not simply performing a single vector search.
A complete retrieval process typically includes:
- Retrieval trigger decision.
- Query intent analysis.
- Query rewriting.
- Multi-channel recall.
- Candidate result merging.
- Relevance reranking.
- Validity and permission checks.
- Context assembly.
1. Retrieval Trigger Decision
Not all tasks need to read long-term memory.
For example:
| User Request | Requires Retrieval | Reason |
|---|---|---|
| Explain what TCP is | Usually no | General knowledge question |
| Continue the previous project analysis | Yes | Depends on historical task context |
| Generate code according to my coding standards | Yes | Depends on user preferences |
| Query the current order status | Should query the business system | Memory cannot replace real-time data |
| Summarize the document just uploaded | Usually does not require long-term memory | The current context already contains the data |
Retrieval triggering can combine rules and models.
Rule-based Triggering
Use explicit conditions to decide whether to retrieve:
- The user mentions words such as "before," "last time," or "continue."
- The current task includes personalized user requirements.
- The task is associated with a specific project.
- The current session lacks necessary background information.
Model-based Decision
The model analyzes the current task to determine whether historical information is needed and which type of memory is required.
For example:
{
"need_memory": true,
"memory_types": [
"preference",
"semantic",
"episodic"
],
"reason": "当前任务需要了解用户的技术栈和历史项目背景"
}
In production environments, model-based decisions should not be the only entry point for retrieval. For project constraints, permission boundaries, and explicit task associations that must be followed, deterministic rules should be used as safeguards.
2. Query Intent Analysis
After determining that retrieval is needed, the system must understand what the current task is actually trying to look up.
For example:
Help me design an order service based on the previous tech stack.
This sentence contains at least three retrieval intents:
- What tech stack has the user used before?
- Does the current project have an established architecture?
- Does the user have specific code or design preferences?
The retrieval intents can be structured as follows:
{
"intent": "project_design",
"entities": [
"order service",
"current project"
],
"memory_types": [
"preference",
"fact",
"semantic"
],
"constraints": {
"scope": "current_project"
}
}
The goal of intent analysis is to convert a natural-language request into an explicit retrieval task.
3. Query Rewriting
The user's original request is not necessarily suitable for direct use in vector retrieval.
For example:
Continue in the same way as before.
This sentence lacks specific retrieval semantics.
The system needs to combine the current session and task state to rewrite it into a clearer query:
Retrieve the tech stack, architecture design constraints,
coding standards, and completed design plans previously confirmed for the current project.
Query rewriting can include:
- Coreference resolution.
- Entity completion.
- Synonym expansion.
- Keyword extraction.
- Time range supplementation.
- Project and user scope restriction.
- Memory type specification.
An important principle:
Query rewriting can only supplement retrieval semantics; it must not invent user facts out of thin air.
For example, just because the user asks to design an order service, the system must not assume on its own that the project uses Go and then use Go as a retrieval condition.
4. Multi-path Memory Recall
Long-term memory may be distributed across different storage systems.
Different types of memory require different retrieval methods.
| Retrieval method | Suitable content | Main characteristics |
|---|---|---|
| Exact query | User ID, project ID, memory ID | Accurate and controllable |
| Keyword retrieval | Error codes, class names, service names | Suitable for exact terms |
| Vector retrieval | Semantic knowledge, historical experience | Suitable for semantically similar content |
| Structured filtering | Type, time, scope, status | Suitable for conditional constraints |
| Graph relationship retrieval | Entity relationships, dependency relationships | Suitable for related information |
For example, if the current task is to design an order service, the system can perform the following at the same time:
- Vector retrieval: find memories related to the order architecture.
- Keyword retrieval: search for keywords such as
order-serviceandPostgreSQL. - Structured query: restrict to the current user and project.
- Graph relationship retrieval: find historical relationships between the order service and the payment service.
The value of multi-path retrieval lies in complementarity, not simply in increasing the number of queries.
5. Merging Candidate Results
Multi-path retrieval may return duplicate records.
For example:
Vector retrieval:
The user prefers to use Go for backend development.
Keyword retrieval:
The backend development language is Go.
Structured query:
backend_language = Go.
These results may point to the same memory.
Deduplication and merging need to be performed based on memory ID, entity, content, and source.
At the same time, necessary source information should be retained to avoid losing credibility and time information after merging.
6. Relevance Reranking
Recall results are not necessarily sorted by actual value.
For example, when the user asks to design an order service, the candidate memories are as follows:
| Memory | Vector similarity | Actual relevance |
|---|---|---|
| The current project's order architecture | 0.92 | Very high |
| Historical order failure records | 0.88 | High |
| The user's preference for page color | 0.85 | Low |
| The design specification for the payment service | 0.81 | High |
Sorting solely by vector similarity may place the page color preference near the top.
Therefore, a reranking model or a composite scoring mechanism is needed.
A common scoring model is:
Where:
- S_{semantic}: Semantic relevance.
- S_{keyword}: Keyword match score.
- S_{scope}: Degree to which the memory matches the scope of the current user, project, or task.
- S_{recency}: Temporal recency.
- S_{importance}: Memory importance.
- S_{confidence}: Memory confidence.
The weights of each factor should be adjusted according to the business scenario.
Particular attention should be paid to the following:
- Memories whose scope does not match should not enter the current context even if they are semantically similar.
- High-risk business facts cannot be judged based on similarity alone.
- Long-term stable facts do not necessarily lose value just because they are older.
- Temporal recency should be judged together with the type of information.
7. Memory Validity Verification
After retrieving memories, it is also necessary to determine whether they are still valid.
Check:
- Whether it has expired.
- Whether it has been updated.
- Whether a newer version exists.
- Whether it has been explicitly revoked by the user.
- Whether it matches the current project.
- Whether it has a trustworthy source.
- Whether the current user has permission to access it.
For example:
Old memory: The order service uses MySQL.
New memory: The current project has migrated to PostgreSQL.
If only the old memory is retrieved, the Agent may adopt an incorrect database design.
Therefore, the memory system needs to support versioning and validity status determination.
8. Context Assembly
Retrieval results cannot be indiscriminately inserted into the Prompt.
The Context Builder needs to:
- Filter irrelevant memories.
- Merge duplicate information.
- Label information sources.
- Distinguish facts from speculation.
- Preserve necessary temporal information.
- Control the number of Tokens.
- Mark untrustworthy content.
Example:
[Relevant Memories]
Current project information:
- Backend language: Go
- Database: PostgreSQL
- Service architecture: Microservices
User preferences:
- Prefer explicit error handling approaches.
Historical experience:
- Database migrations need to check time zones and field precision.
[Memory Metadata]
- Source: historical conversations and project configuration
- Status: valid
- Scope: current project
[Current Task]
Design the order service.
Retrieval results should serve as reference data, not as high-priority instructions that can override system instructions.
9. Complete Flowchart of Memory Retrieval
flowchart TD
A["Receive user task"] --> B["Determine whether retrieval is needed"]
B --> C{"Whether memory is needed"}
C -->|"No"| D["Build context directly"]
C -->|"Yes"| E["Analyze retrieval intent"]
E --> F["Query rewriting"]
F --> G["Multi-path memory recall"]
G --> H["Merge candidate results"]
H --> I["Relevance reranking"]
I --> J["Permission and validity verification"]
J --> K{"Whether valid memory exists"}
K -->|"No"| D
K -->|"Yes"| L["Deduplication and conflict checking"]
L --> M["Token budget control"]
M --> N["Context assembly"]
N --> O["Model inference and execution"]
D --> O
style A fill:#DCEFE5,stroke:#4F8B70,color:#24563F
style B fill:#EAF3EC,stroke:#83A995,color:#315D49
style C fill:#F7F4E8,stroke:#C7B77A,color:#675A2D
style D fill:#F5F8F4,stroke:#B5CBBB,color:#40594A
style E fill:#EAF3EC,stroke:#83A995,color:#315D49
style F fill:#F5F8F4,stroke:#B5CBBB,color:#40594A
style G fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
style H fill:#F5F8F4,stroke:#B5CBBB,color:#40594A
style I fill:#EAE8FA,stroke:#A49BD5,color:#51477D
style J fill:#F7F4E8,stroke:#C7B77A,color:#675A2D
style K fill:#F7F4E8,stroke:#C7B77A,color:#675A2D
style L fill:#EAE8FA,stroke:#A49BD5,color:#51477D
style M fill:#F5F8F4,stroke:#B5CBBB,color:#40594A
style N fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
style O fill:#DCEFE5,stroke:#4F8B70,color:#24563F
IV. Key Algorithms and Techniques for Memory Retrieval
1. Vector Similarity Search
Vector retrieval is a common recall method in long-term memory systems.
First, the memory text is converted into a vector using an Embedding model:
The current query is also converted into a vector:
Then the similarity between the two is calculated.
Common metrics include:
Cosine similarity
Dot product
Euclidean distance
Different Embedding models may have different requirements for vector normalization and similarity computation, so the choice needs to be made according to the model and the configuration of the vector database.
2. ANN Approximate Nearest Neighbor Search
When the memory scale is small, the distance between the query vector and all memory vectors can be computed directly.
But when the number of vectors reaches the millions or more, the cost of computing one by one increases significantly.
ANN (Approximate Nearest Neighbor) reduces retrieval overhead through index structures.
Common algorithms:
| Algorithm | Core idea | Characteristics |
|---|---|---|
| HNSW | Hierarchical small-world graph | Fast query speed, relatively high memory usage |
| IVF | Cluster first, then search a subset of inverted lists | Suitable for large-scale vector collections |
| PQ | Vector quantization and compression | Reduces storage cost |
| DiskANN | Disk-based approximate nearest neighbor | Suitable for large-scale disk-based retrieval |
The basic idea of HNSW is to build a multi-layer graph structure.
The upper-layer graphs are responsible for quickly locating regions, while the bottom-layer graph is responsible for fine-grained search.
Query vector
│
▼
Fast region location in upper-layer graphs
│
▼
Descend layer by layer into lower-layer graphs
│
▼
Neighbor search at the bottom layer
│
▼
Return Top-K candidates
3. BM25 Keyword Search
BM25 is a classic relevance algorithm for full-text search.
It mainly considers:
- The frequency with which a query term appears in a document.
- How rare the query term is across the entire document collection.
- Document length.
Its typical form is:
Where:
- f(q_i,D): the term frequency of the query term in the document.
- |D|: document length.
- avgdl: average document length.
- k_1,b: tuning parameters.
- IDF: inverse document frequency.
BM25 is particularly well suited to retrieving specific technical terms, error codes, service names, and configuration items.
4. Hybrid Retrieval
Vector retrieval and keyword retrieval each have their own advantages.
Therefore, practical systems usually adopt hybrid retrieval.
One common approach is weighted fusion:
However, note that the score distributions of different retrieval algorithms may be inconsistent, and it cannot be assumed directly that the two scores share the same scale.
Another approach is RRF (Reciprocal Rank Fusion), which fuses results by ranking:
Where:
- rank_i(d): the rank of the document in the i-th retrieval result.
- k: smoothing parameter.
RRF does not depend on a unified scale for the original retrieval scores, making it suitable for fusing multiple ranking results.
5. Reranking Models
Recall models are responsible for quickly finding candidate memories from large amounts of data.
Reranking models, in turn, are responsible for making more fine-grained relevance judgments on the candidate results.
Common implementations:
- Cross-Encoder.
- A specially trained Reranker.
- LLM-based relevance judgment.
A Cross-Encoder typically takes the query and the candidate document as input together and computes relevance directly.
It is usually more fine-grained than pure vector similarity, but its computational cost is also higher.
Typical architecture:
flowchart TD
A["User Query"] --> B["Embedding Retrieval"]
A --> C["BM25 Retrieval"]
B --> D["Candidate Set"]
C --> D
D --> E["RRF or Score Fusion"]
E --> F["Top-K Candidates"]
F --> G["Cross-Encoder Reranking"]
G --> H["Final Relevant Memories"]
style A fill:#DCEFE5,stroke:#4F8B70,color:#24563F
style B fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
style C fill:#EAE8FA,stroke:#A49BD5,color:#51477D
style D fill:#F5F8F4,stroke:#B5CBBB,color:#40594A
style E fill:#EAF3EC,stroke:#83A995,color:#315D49
style F fill:#F5F8F4,stroke:#B5CBBB,color:#40594A
style G fill:#EAE8FA,stroke:#A49BD5,color:#51477D
style H fill:#DCEFE5,stroke:#4F8B70,color:#24563F
V. Core Mechanisms of Memory Updates
The goal of memory updates is not simply to add new records, but to keep the memory store accurate, relevant, and maintainable at all times.
1. Trigger Sources for Memory Updates
Memory updates can come from multiple channels:
| Source | Example |
|---|---|
| Explicit user statement | User modifies long-term preference |
| New conversation content | User adds project background |
| Tool execution result | Latest project configuration is retrieved |
| Task execution result | A solution has been validated |
| External data change | Business rules or technical documentation updated |
| Scheduled maintenance task | Clean up expired and duplicate memories |
Different sources have different levels of credibility.
For example:
- Information explicitly confirmed by the user usually has higher credibility.
- Data verified by authoritative business systems is suitable for updating the corresponding business facts.
- Information inferred by the model itself should not be written directly as established fact.
- Content from external web pages and documents must undergo source and security checks.
2. Candidate Memory Extraction
An Agent generates a large amount of information during task execution, but not all information is worth updating long-term memory.
The memory extractor needs to identify:
- New facts.
- New preferences.
- New experiences.
- State changes that have occurred.
- Validated operational procedures.
- Information explicitly revoked by the user.
For example:
User: The project previously used MySQL, but it has now migrated to PostgreSQL.
Extracted result:
{
"memory_type": "project_fact",
"entity": "current_project",
"attribute": "database",
"old_value": "MySQL",
"new_value": "PostgreSQL",
"operation": "update",
"source": "user_statement"
}
3. Memory Importance Assessment
Candidate memories need to undergo importance assessment.
Common dimensions:
| Dimension | Assessment Criteria |
|---|---|
| Importance | Whether it affects future tasks |
| Stability | Whether it may remain valid long-term |
| Reusability | Whether it can be used across multiple tasks |
| Credibility | Whether the source is reliable |
| Timeliness | Whether it has a validity period |
| Scope | Whether it applies to a user, a project, or a single task |
| Sensitivity | Whether it involves information that should not be stored |
A composite score can be constructed:
Where:
- I: importance level.
- S: stability level.
- R: reusability level.
- C: credibility level.
This score is mainly used to assist decision-making and cannot replace user authorization, privacy policies, or security rules.
4. Memory Classification
Different types of memory should adopt different update methods.
For example:
| Memory Type | Update Strategy |
|---|---|
| User preferences | Merge, overwrite, or isolate by scope |
| Project facts | Update by version and time |
| Semantic knowledge | Summarize, supplement, and manage versions |
| Episodic memory | Usually append event records |
| Procedural memory | Update operational procedures after validation |
It is especially important to distinguish facts from events.
A user switching databases is a change of fact, and the current valid state can be updated.
A particular database migration succeeding is a historical event, which should normally be retained as episodic memory rather than directly overwritten.
5. Deduplication and Merging
Long-term memory often contains content with the same meaning but different wording.
For example:
Memory A: The user's backend mainly uses Go.
Memory B: The user likes to use Golang to develop services.
Memory C: The user's server-side language is Go.
These three records may express the same information.
The system can identify this through:
- Exact text matching.
- Keyword matching.
- Vector similarity.
- Entity and attribute comparison.
- LLM semantic judgment.
The merged memory:
{
"type": "preference",
"key": "backend_language",
"value": "Go",
"sources": [
"memory_A",
"memory_B",
"memory_C"
],
"confidence": 0.98
}
When merging, the source and historical information must be preserved to avoid losing evidence.
6. Conflict Detection
Memory conflicts are one of the most error-prone issues in long-term memory management.
For example:
Old memory: The current project uses MySQL.
New memory: The current project uses PostgreSQL.
This could be because:
- The user changed the technology choice.
- The project underwent a database migration.
- The two pieces of information correspond to different projects.
- The new information is only a temporary test environment configuration.
- The new information itself is inaccurate.
Therefore, the new information cannot be used to directly overwrite the old information.
You need to compare:
- Whether the entities are the same.
- Whether the attributes are the same.
- Whether the scopes are the same.
- Whether the time ranges overlap.
- Whether the information sources are reliable.
- Whether the new information explicitly indicates a change.
- Whether there is an authoritative data source that can verify it.
7. Conflict Handling Strategies
| Strategy | Use Case |
|---|---|
| New version overwrites | A clear state change occurs within the same scope |
| Retain historical versions | Information changes need to be tracked |
| Split by scope | Different projects or environments use different configurations |
| Multiple values coexist | The information itself allows multiple values |
| Confidence adjustment | Both old and new information lack sufficient evidence |
| Manual confirmation | The conflict has major impact and cannot be judged automatically |
For example:
Project A: uses Go.
Project B: uses Rust.
This is not a conflict; the scope should be split.
Whereas:
Project A:
The old version used MySQL.
The new version has migrated to PostgreSQL.
This is a temporal state change within the same project; the historical version should be retained and the current valid state updated.
VI. Transaction and Version Mechanisms for Memory Updates
When multiple Agent instances modify memory simultaneously, a simple read-modify-write can cause data overwrites.
For example:
Initial memory: version 1
Instance A reads version 1.
Instance B reads version 1.
Instance A updates to version 2.
Instance B updates to version 3.
If instance B directly overwrites instance A's result, the update may be lost.
Therefore, production-grade memory systems need to consider concurrency control.
1. Optimistic Locking
Use a version number to determine whether the data has changed.
For example:
UPDATE agent_memories
SET
memory_value = '{"database": "PostgreSQL"}',
version = version + 1
WHERE id = 1001
AND version = 3;
If the number of affected rows is 0, it means the data version has already changed and must be re-read and processed.
2. Memory Version Management
It is recommended that long-term memory contain at least:
{
"memory_id": "mem_1001",
"version": 4,
"status": "active",
"content": "当前项目使用 PostgreSQL",
"source": "user_confirmation",
"valid_from": "2026-11-20T10:00:00Z",
"valid_to": null,
"created_at": "2026-11-20T10:00:00Z"
}
Version management can support:
- Tracking memory changes.
- Restoring historical versions.
- Comparing old and new information.
- Handling concurrent updates.
- Auditing memory sources.
3. Transactions and Consistency
A memory system may maintain the following at the same time:
- Memory records in a relational database.
- Embeddings in a vector database.
- Full-text search indexes.
- Hot memories in cache.
These systems usually do not share the same database transaction.
Therefore, you cannot assume that a single memory update can atomically modify all storage systems.
A common design is as follows:
- Commit the memory change in the primary database.
- Write an Outbox event in the same transaction.
- Have a background consumer read the event.
- Update the vector index and full-text index.
- Update or invalidate the relevant caches.
- Record the synchronization status and support retries.
This approach can reduce data inconsistency caused by failed cross-storage updates.
4. Memory Update Flow
flowchart TD
A["Receive new information"] --> B["Extract candidate memories"]
B --> C["Classify type and assess importance"]
C --> D{"Is saving allowed?"}
D -->|"No"| E["Discard or keep only short-term record"]
D -->|"Yes"| F["Read related existing memories"]
F --> G["Deduplicate and merge semantically"]
G --> H{"Is there a conflict?"}
H -->|"No"| I["Create or supplement memory"]
H -->|"Yes"| J["Analyze scope and source"]
J --> K{"Can it be resolved automatically?"}
K -->|"Yes"| L["Apply update or version change"]
K -->|"No"| M["Mark as pending confirmation"]
I --> N["Commit primary database transaction"]
L --> N
M --> O["Retain original valid state"]
N --> P["Write Outbox event"]
P --> Q["Synchronize vector and full-text indexes"]
Q --> R["Update cache"]
R --> S["Update complete"]
style A fill:#DCEFE5,stroke:#4F8B70,color:#24563F
style B fill:#EAF3EC,stroke:#83A995,color:#315D49
style C fill:#F5F8F4,stroke:#B5CBBB,color:#40594A
style D fill:#F7F4E8,stroke:#C7B77A,color:#675A2D
style E fill:#F7EAE7,stroke:#C98D7C,color:#754438
style F fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
style G fill:#EAE8FA,stroke:#A49BD5,color:#51477D
style H fill:#F7F4E8,stroke:#C7B77A,color:#675A2D
style I fill:#EAF3EC,stroke:#83A995,color:#315D49
style J fill:#F5F8F4,stroke:#B5CBBB,color:#40594A
style K fill:#F7F4E8,stroke:#C7B77A,color:#675A2D
style L fill:#EAF3EC,stroke:#83A995,color:#315D49
style M fill:#F7EAE7,stroke:#C98D7C,color:#754438
style N fill:#DCEFE5,stroke:#4F8B70,color:#24563F
style O fill:#F5F8F4,stroke:#B5CBBB,color:#40594A
style P fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
style Q fill:#EAF3EC,stroke:#83A995,color:#315D49
style R fill:#F5F8F4,stroke:#B5CBBB,color:#40594A
style S fill:#DCEFE5,stroke:#4F8B70,color:#24563F
7. Memory Forgetting and Cleanup Mechanisms
Memory updates include not only additions and modifications but also deletions and archiving.
As long-term memory continues to grow, it is necessary to prevent low-value information from accumulating indefinitely.
1. Time-Based Expiration
Set a validity period for memories that have a clear time limit.
For example:
- Temporary project status.
- Short-term task arrangements.
- Temporary business activities.
- Phased configuration.
Expired memories do not necessarily need to be physically deleted immediately.
You can first mark them as invalid so they are excluded from normal retrieval, and then archive or delete them according to the retention policy.
2. Access Frequency and Importance Decay
Memory retrieval priority can be adjusted based on access frequency and time.
For example:
Where:
- Importance(m): The importance of the memory.
- Recency(m): The most recent update or usage time.
- Frequency(m): Access frequency.
Note that low access frequency does not mean a memory has no value.
For example, a user's long-term security constraints may rarely be accessed, but they must still be retained.
3. Merging and Compression
Multiple duplicate memories can be summarized into more concise knowledge.
For example:
Memory A: The user's backend uses Go.
Memory B: The user's API service uses Go.
Memory C: The user prefers Go for server-side development.
This can be summarized as:
The user's backend technology preference: Go.
But the source, scope, and necessary historical records need to be retained.
4. Explicit Deletion
When a user requests deletion of a memory, a complete data deletion policy must be executed.
This may involve:
- Primary memory records.
- Corresponding vectors in the vector database.
- Full-text index.
- Cache.
- Derived summaries.
- Backup and archive data.
For systems with compliance requirements, deletion audit records must also be provided.
5. Forgetting and Cleanup Process
flowchart TD
A["Periodic memory maintenance task"] --> B["Scan memory records"]
B --> C["Check expiration"]
C --> D{"Has it expired?"}
D -->|"Yes"| E["Mark as invalid"]
D -->|"No"| F["Evaluate importance and usage"]
F --> G{"Need to merge or archive?"}
G -->|"Yes"| H["Summarize/compress or archive"]
G -->|"No"| I["Retain valid memories"]
E --> J["Check whether physical deletion is needed"]
H --> K["Update indexes and metadata"]
J --> L{"Is deletion allowed?"}
L -->|"Yes"| M["Clean up storage and indexes"]
L -->|"No"| N["Retain in isolation or archive area"]
M --> O["Record cleanup result"]
N --> O
K --> P["Complete maintenance"]
I --> P
O --> P
style A fill:#DCEFE5,stroke:#4F8B70,color:#24563F
style B fill:#EAF3EC,stroke:#83A995,color:#315D49
style C fill:#F5F8F4,stroke:#B5CBBB,color:#40594A
style D fill:#F7F4E8,stroke:#C7B77A,color:#675A2D
style E fill:#F7EAE7,stroke:#C98D7C,color:#754438
style F fill:#EAF3EC,stroke:#83A995,color:#315D49
style G fill:#F7F4E8,stroke:#C7B77A,color:#675A2D
style H fill:#EAE8FA,stroke:#A49BD5,color:#51477D
style I fill:#DCEFE5,stroke:#4F8B70,color:#24563F
style J fill:#F5F8F4,stroke:#B5CBBB,color:#40594A
style K fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
style L fill:#F7F4E8,stroke:#C7B77A,color:#675A2D
style M fill:#F7EAE7,stroke:#C98D7C,color:#754438
style N fill:#F5F8F4,stroke:#B5CBBB,color:#40594A
style O fill:#DCEFE5,stroke:#4F8B70,color:#24563F
style P fill:#DCEFE5,stroke:#4F8B70,color:#24563F