I. Why Does an Agent Need Memory?
Large language models themselves generally do not have the ability to persistently retain conversation state across requests. Each time the model is called, it generates output primarily based on the context of the current input.
This means that if an Agent relies only on the current prompt, it will encounter several problems:
- Insufficient Context Continuity: In multi-turn conversations, previous task goals and constraints are easily lost.
- Loss of Long-running Task State: When executing complex tasks, completed steps, pending items, and intermediate results cannot be reliably retained.
- User Information Cannot Be Reused: The user's preferences, habits, and long-term goals cannot continue to be used in subsequent sessions.
- Repeated Reasoning and Repeated Execution: It cannot remember previously attempted methods, reasons for failure, and operations already completed.
- Experience Cannot Accumulate: Every task starts from scratch, and experience cannot be accumulated across tasks.
Therefore, an Agent needs to establish an independent memory system outside the model context.
From an engineering perspective, memory is not simply about storing chat logs; rather, it is about building a set of mechanisms around the lifecycle of information:
- Information acquisition: Extract information from conversations, tool results, and task execution.
- Information organization: Identify information types, importance levels, and scope of applicability.
- Information storage: Write to short-term or long-term storage.
- Information retrieval: Recall relevant memories based on the current task.
- Information update: Handle conflicts between new and old information.
- Information forgetting: Delete expired, incorrect, or no longer needed information.
II. Overall Classification of Agent Memory
Based on the retention period and scope of use of information, Agent memory is generally divided into short-term memory and long-term memory.
1. Short-term Memory
Short-term memory is used to store information needed during the current session or the execution of the current task.
Typical content includes:
- The most recent turns of conversation.
- The current task goal.
- Constraints the user has just raised.
- The Agent's current execution plan.
- Tool call results.
- Subtasks that have not yet been completed.
- The intermediate reasoning state of the current task.
- Recent exceptions and retry records.
The core goal of short-term memory is to ensure that the current task can be executed continuously.
It usually has the following characteristics:
| Characteristic | Description |
|---|---|
| Lifecycle | Current session or task cycle |
| Update frequency | High-frequency updates |
| Data scale | Relatively small but may continue to grow |
| Access method | Direct reads, context concatenation, state queries |
| Storage method | In-memory storage, Redis, session database, Checkpoint |
| Primary use | Conversation continuity, task state maintenance |
Short-term memory does not mean that the data must be stored in memory.
For example, an Agent task that runs for several hours can persist its execution state to Redis or a database. As long as this information serves the current task, it still falls within the scope of short-term memory.
2. Long-term Memory
Long-term memory is used to store information that remains valuable across sessions and tasks.
Typical content includes:
- The user's long-term preferences.
- The user's stable work habits.
- The user's long-term goals.
- Confirmed business rules.
- Experience from historical tasks.
- Frequently used tools and workflows.
- Validated facts and knowledge.
- User profiles formed through multiple interactions.
The core goal of long-term memory is to enable the Agent to accumulate reusable information across different sessions.
| Characteristic | Description |
|---|---|
| Lifecycle | Cross-session, long-term retention |
| Update frequency | Relatively low |
| Data scale | Continuously growing |
| Access method | Keyword retrieval, vector retrieval, structured queries |
| Storage method | Relational databases, document databases, vector databases |
| Primary use | Personalization, experience reuse, long-term knowledge accumulation |
Long-term memory is not the same as permanent memory.
Information may expire over time, or become invalid because user preferences change. Therefore, long-term memory must have mechanisms for updating, validating, and deleting information.
3. Differences Between Short-term and Long-term Memory
| Comparison Dimension | Short-term Memory | Long-term Memory |
|---|---|---|
| Primary goal | Maintain continuity of the current task | Support information reuse across tasks |
| Time range | Current session or task | Multiple sessions and tasks |
| Data type | Conversations, plans, states, intermediate results | Preferences, facts, experience, knowledge |
| Update frequency | High frequency | Relatively low frequency |
| Storage scale | Constrained by context and task scale | Continuously scalable |
| Retrieval method | Direct reads, state recovery | Semantic retrieval, keyword retrieval |
| Data processing | Sliding windows, summarization, state updates | Deduplication, generalization, merging, expiration handling |
| Typical storage | Redis, Checkpoint | PostgreSQL, MongoDB, vector databases |
Note: Short-term and long-term are classified according to the purpose and lifecycle of memory, not according to the underlying storage medium.
The same Redis instance can store both short-term task state and long-term user preferences; the same PostgreSQL database can likewise store both session records and long-term memory.
III. The Internal Structure of Short-Term Memory
Short-term memory should not be just an ever-growing array of chat messages.
For an agent capable of tool calling and task planning, short-term memory needs to manage at least the following categories of information.
1. Conversation Context
Stores the content of interactions between the user and the agent.
For example:
{
"conversation_id": "conv_1001",
"messages": [
{
"role": "user",
"content": "Help me analyze the architecture of this project"
},
{
"role": "assistant",
"content": "I'll start by analyzing the project directory and dependencies"
}
]
}
Conversation context typically includes:
- User messages.
- Agent replies.
- Tool call requests.
- Tool results.
- System-level constraints.
- References to necessary multimodal content.
But not all historical messages need to be kept in the model context permanently.
As the conversation keeps growing, context-window management mechanisms are needed to control the size of the input.
2. Task State
Task state records what stage the agent has currently reached.
For example:
{
"task_id": "task_1001",
"goal": "Analyze the project architecture and generate a report",
"status": "running",
"current_step": "dependency_analysis",
"completed_steps": [
"scan_project",
"identify_modules"
],
"pending_steps": [
"dependency_analysis",
"generate_report"
]
}
Task state differs clearly from conversation context:
- Conversation context records what happened.
- Task state records what state things are currently in.
A model can infer task progress from conversation history, but this approach is not reliable enough.
In production, task state should be stored in a structured form rather than relying on the model to re-infer it from a large volume of chat logs.
3. Execution Plan
For complex tasks, an agent usually needs to break the goal down into multiple subtasks.
For example:
{
"plan": [
{
"step": 1,
"name": "Scan project",
"status": "completed"
},
{
"step": 2,
"name": "Analyze module dependencies",
"status": "running"
},
{
"step": 3,
"name": "Generate architecture report",
"status": "pending"
}
]
}
The execution plan needs to be updated dynamically as the task proceeds.
When a subtask fails, the agent may need to:
- Retry the current step.
- Adjust the remaining plan.
- Add compensating steps.
- Roll back previous operations.
- Mark the task as failed or as waiting for human intervention.
So the execution plan is in effect a structured state within short-term memory.
4. Tool Execution History
During execution, an agent calls various external tools.
For example:
- Querying a database.
- Calling a search API.
- Reading files.
- Executing code.
- Calling business APIs.
A tool execution record should contain at least:
{
"tool_call_id": "call_1001",
"tool_name": "search_database",
"arguments": {
"keyword": "order"
},
"status": "success",
"result_summary": "Found 128 order records"
}
The value of storing tool execution records is that it:
- Avoids repeated calls to tools that have already executed successfully.
- Supports recovery after a task is interrupted.
- Enables tracing the causes of tool call failures.
- Lets the agent continue working based on actual execution results.
- Supports auditing and troubleshooting.
For tools with side effects—such as payments, order placement, and sending email—you cannot rely solely on the execution records in memory to decide whether a retry is safe; you also need idempotency keys, business-state queries, and transaction mechanisms.
5. Intermediate Results
Complex tasks may generate a large amount of intermediate data.
For example:
- File parsing results.
- Database query results.
- Code analysis results.
- Web search results.
- Interim reports generated by the model.
Not all of this content is necessarily suitable for inclusion in the model context.
A more reasonable approach is to store the complete results in an external system, while short-term memory retains only references, summaries, and key fields.
{
"result_id": "result_1001",
"type": "code_analysis",
"storage_uri": "storage://task_1001/analysis.json",
"summary": "Identified 12 core modules",
"created_at": "2026-11-20T10:00:00Z"
}
This preserves the complete data while preventing the context from being occupied by large amounts of low-value content.
IV. Core Management Mechanisms of Short-Term Memory
The most important issue for short-term memory is not storage, but how to retain the most valuable information within a limited context window.
1. Sliding Window
Sliding window is the most direct context management method.
Keep the most recent N messages; historical messages beyond that range are removed from the current context.
For example:
Original messages:
M1 → M2 → M3 → M4 → M5 → M6 → M7
Window size is 4:
M4 → M5 → M6 → M7
Mermaid flowchart:
flowchart TD
A["Receive new message"] --> B["Append to message list"]
B --> C{"Exceeds window limit?"}
C -->|"No"| D["Keep all messages"]
C -->|"Yes"| E["Remove earliest message"]
E --> F["Keep the latest N messages"]
D --> G["Build model context"]
F --> G
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:#F7EAE7,stroke:#C98D7C,color:#754438
style F fill:#EAF3EC,stroke:#83A995,color:#315D49
style G fill:#DCEFE5,stroke:#4F8B70,color:#24563F
Advantages:
- Simple to implement.
- Low processing cost.
- Retains recent interaction content.
- Does not require additional summarization model calls.
Disadvantages:
- May lose important early constraints.
- May lose the initial task objective.
- Cannot distinguish important messages from ordinary messages.
- Not reliable enough for long-running tasks.
Sliding window is suitable for short conversations and is not suitable as a complete memory strategy for complex Agents directly.
2. Conversation Summarization
Conversation summarization uses a model to compress historical messages into a structured summary.
For example, the original conversation contains a lot of details:
User: Help me analyze the order system.
Agent: I'll first inspect the project structure.
User: Focus on the payment and order modules.
Agent: Found that the order module depends on the payment module.
User: Also analyze the failure retry mechanism.
Agent: The payment module uses a message queue for asynchronous processing.
It can be compressed into:
Task goal: Analyze the order system architecture.
User focus areas:
1. Order module.
2. Payment module.
3. Failure retry mechanism.
Findings:
1. The order module depends on the payment module.
2. The payment module performs asynchronous processing through a message queue.
Pending:
Analyze the retry and compensation mechanism after payment failure.
A summary is not simply shortening the text; it extracts information useful for subsequent tasks.
A reliable summary should retain as much as possible:
- The original task objective.
- Constraints explicitly stated by the user.
- Confirmed facts.
- Completed work.
- Unfinished items.
- Key decisions and their rationale.
- Unresolved issues.
After the summary is generated, it is best to use a structured format to avoid important information being missed during natural language compression.
{
"goal": "Analyze the order system architecture",
"constraints": [
"Focus on the payment module",
"Pay attention to the failure retry mechanism"
],
"facts": [
"The order module depends on the payment module",
"The payment module uses a message queue"
],
"completed": [
"Project structure analysis"
],
"pending": [
"Payment failure retry analysis"
]
}
3. Hierarchical Context
A complex Agent can divide the context into multiple layers.
| Layer | Stored Content | Handling |
|---|---|---|
| Core context | System constraints, task objectives | Retained long-term until task completion |
| Current context | Recent conversation, current operation | Updated frequently |
| Summary context | Key content of historical conversation | Periodically compressed |
| External results | Large tool outputs, file contents | Read on demand |
When constructing model input, dynamically combine information from different layers according to the current task.
Mermaid flowchart:
flowchart TD
A["Short-term memory storage"] --> B["Core context"]
A --> C["Current conversation context"]
A --> D["Historical summary"]
A --> E["External result references"]
B --> F["Context assembler"]
C --> F
D --> F
E --> G["Load external data on demand"]
G --> F
F --> H["Token budget check"]
H --> I["Generate model input"]
style A fill:#DCEFE5,stroke:#4F8B70,color:#24563F
style B fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
style C fill:#EAF3EC,stroke:#83A995,color:#315D49
style D fill:#EAE8FA,stroke:#A49BD5,color:#51477D
style E fill:#F5F8F4,stroke:#B5CBBB,color:#40594A
style F fill:#DCEFE5,stroke:#4F8B70,color:#24563F
style G fill:#F5F8F4,stroke:#B5CBBB,color:#40594A
style H fill:#F7F4E8,stroke:#C7B77A,color:#675A2D
style I fill:#DCEFE5,stroke:#4F8B70,color:#24563F
4. Checkpoint and Task Recovery
For long-running Agents, saving only conversation summaries is not enough.
You also need to save task execution snapshots, i.e., Checkpoints.
A Checkpoint usually includes:
- Current task status.
- Current execution step.
- Completed steps.
- Pending steps.
- Tool call records.
- References to key intermediate results.
- Current context summary.
- Retry count and error information.
When the process crashes or a task is interrupted, execution can be resumed from a Checkpoint.
flowchart TD
A["Agent executes task"] --> B["Complete an execution step"]
B --> C["Update task status"]
C --> D["Persist Checkpoint"]
D --> E{"Is the task complete?"}
E -->|"Yes"| F["Save final result"]
E -->|"No"| G["Continue to next step"]
G --> B
H["Process exception or service restart"] --> I["Read latest Checkpoint"]
I --> J["Restore task status"]
J --> K["Verify external side effects"]
K --> L["Continue execution from a safe point"]
style A fill:#DCEFE5,stroke:#4F8B70,color:#24563F
style B fill:#EAF3EC,stroke:#83A995,color:#315D49
style C fill:#EAF3EC,stroke:#83A995,color:#315D49
style D fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
style E fill:#F7F4E8,stroke:#C7B77A,color:#675A2D
style F fill:#DCEFE5,stroke:#4F8B70,color:#24563F
style G fill:#F5F8F4,stroke:#B5CBBB,color:#40594A
style H fill:#F7EAE7,stroke:#C98D7C,color:#754438
style I fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
style J fill:#EAF3EC,stroke:#83A995,color:#315D49
style K fill:#F7F4E8,stroke:#C7B77A,color:#675A2D
style L fill:#DCEFE5,stroke:#4F8B70,color:#24563F
There is a very important engineering issue here:
Checkpoint recovery does not mean directly re-executing from the last saved position.
Suppose the Agent has already called the payment API, but the process crashes before the Checkpoint is saved. After recovery, if the payment API is called again directly, it may cause a duplicate payment.
Therefore, the recovery mechanism must combine:
- Idempotency keys.
- External system status queries.
- Tool invocation logs.
- Transaction or compensation mechanisms.
- Commit status of execution steps.
V. Internal Structure of Long-Term Memory
Long-term memory cannot simply store all historical conversations permanently.
A large amount of raw conversation contains redundant, temporary, erroneous, and low-value information. Storing it directly would cause retrieval noise to increase continuously.
A more reasonable approach is to organize long-term memory into different types.
1. Factual Memory
Stores facts that have been confirmed and have lasting value.
For example:
{
"type": "fact",
"subject": "project",
"predicate": "language",
"object": "Go",
"confidence": 0.98
}
Factual memory is suitable for storing:
- Project technology stack.
- Business rules.
- System architecture information.
- Confirmed configurations.
- Background information explicitly provided by the user.
Factual memory should distinguish between sources and credibility.
Information inferred by the model cannot be stored directly as confirmed fact.
2. User Preference Memory
Stores the user's relatively stable preferences.
For example:
{
"type": "preference",
"subject": "user",
"key": "code_style",
"value": "prefer_explicit_error_handling",
"confidence": 0.95
}
Typical content:
- Preferred output formats.
- Programming style.
- Technology selection preferences.
- Documentation organization methods.
- Requirements for answer depth.
The value of preference memory lies in reducing repetitive communication and enabling the Agent to better adapt to the user in subsequent tasks.
However, preferences may change, so a one-time temporary request should not be permanently fixed.
3. Semantic Memory
Semantic memory stores knowledge that has been summarized and organized.
For example:
- The overall architecture of a project.
- Core concepts in a business domain.
- Module relationships within a system.
- Design principles of a technical solution.
Unlike raw conversations, it focuses on storing knowledge itself rather than the complete interaction process through which the knowledge was produced.
Semantic memory can usually be organized through methods such as document segmentation, entity extraction, knowledge graphs, and vectorization.
4. Episodic Memory
Episodic memory stores specific events and task experiences that occurred in the past.
For example:
{
"type": "episode",
"task": "数据库迁移",
"context": "将订单数据从 MySQL 迁移到 PostgreSQL",
"action": "采用分批迁移策略",
"outcome": "迁移成功",
"lesson": "迁移前需要统一时区和字段精度"
}
Episodic memory focuses on:
- What the task was at the time.
- What environment it was in.
- What the Agent did.
- What the final result was.
- What lessons were learned.
It can help the Agent refer to historical experience when encountering similar tasks.
5. Procedural Memory
Procedural memory stores the methods and processes for executing tasks.
For example:
- How to deploy a service.
- How to troubleshoot a certain type of failure.
- How to perform data migration.
- How to call a set of business APIs.
- How to generate reports according to organizational standards.
Its difference from episodic memory is:
- Episodic memory records a single task experience.
- Procedural memory distills reusable methods.
For example, episodic memory stores the process of a particular database migration, while procedural memory stores validated operational standards for database migration.
6. Comparison of Long-Term Memory Types
| Type | Content Stored | Typical Use |
|---|---|---|
| Factual Memory | Confirmed facts | Accurately answer background questions |
| Preference Memory | Stable user preferences | Personalized interaction |
| Semantic Memory | Summarized knowledge | Knowledge reuse |
| Episodic Memory | Historical events and experiences | Refer to past experience |
| Procedural Memory | Methods, processes, and skills | Reuse execution strategies |
These types are not mutually exclusive.
The same historical interaction may simultaneously produce factual memory, preference memory, and episodic memory.
VI. Storage Architecture of Long-Term Memory
Long-term memory usually requires combining multiple storage methods rather than relying on a single database.
1. Structured Storage
Structured storage is suitable for storing data with clearly defined fields that requires precise querying.
For example:
- User preferences.
- User configurations.
- Business rules.
- Task metadata.
- Memory status and version information.
Common technologies:
- PostgreSQL.
- MySQL.
- MongoDB.
- Redis.
For example, use a relational database to store user preferences:
CREATE TABLE agent_memories (
id BIGSERIAL PRIMARY KEY,
user_id VARCHAR(128) NOT NULL,
memory_type VARCHAR(32) NOT NULL,
memory_key VARCHAR(255) NOT NULL,
memory_value JSONB NOT NULL,
confidence FLOAT DEFAULT 1.0,
source VARCHAR(64),
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
expires_at TIMESTAMP,
status VARCHAR(32) DEFAULT 'active'
);
This approach can support:
- Querying by user.
- Filtering by type.
- Exact matching by key.
- Filtering by status.
- Determining whether something has expired based on time.
2. Vector storage
Vector storage is suitable for semantic similarity retrieval.
For example, the user previously said:
I prefer to use Go to develop backend services.
The current user asks:
Help me design a high-concurrency API service.
The two texts do not contain exactly the same keywords, but they are semantically related.
By converting text into vectors with an Embedding model, relevant memories can be recalled based on semantic similarity.
Typical vector databases:
- Milvus.
- Qdrant.
- Weaviate.
- Chroma.
- PostgreSQL + pgvector.
The basic process of vector retrieval:
flowchart TD
A["Historical interactions or knowledge"] --> B["Memory extraction"]
B --> C["Text cleaning and summarization"]
C --> D["Embedding model"]
D --> E["Generate vector"]
E --> F["Write to vector database"]
G["Current task"] --> H["Construct retrieval query"]
H --> I["Generate query vector"]
I --> J["Vector similarity search"]
F --> J
J --> K["Return candidate memories"]
style A fill:#EAF3EC,stroke:#83A995,color:#315D49
style B fill:#EAF3EC,stroke:#83A995,color:#315D49
style C fill:#F5F8F4,stroke:#B5CBBB,color:#40594A
style D fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
style E fill:#EAF3EC,stroke:#83A995,color:#315D49
style F fill:#DCEFE5,stroke:#4F8B70,color:#24563F
style G fill:#EAF3EC,stroke:#83A995,color:#315D49
style H fill:#F5F8F4,stroke:#B5CBBB,color:#40594A
style I fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
style J fill:#EAE8FA,stroke:#A49BD5,color:#51477D
style K fill:#DCEFE5,stroke:#4F8B70,color:#24563F
It is important to note that a vector database stores vectors and their associated metadata, not the entirety of a memory system.
Production environments usually also need to store information such as the original content, memory ID, user ID, source, time, and status.
3. Full-text search
Vector retrieval is good at semantic matching, but not necessarily at exact keyword queries.
For example:
- Error code
E1024. - Database table name
order_payment. - Service name
payment-service. - Specific version number
v2.3.1.
This type of information is usually better suited to full-text search or exact matching.
Common technologies:
- Elasticsearch.
- OpenSearch.
- PostgreSQL Full Text Search.
4. Hybrid Search
In practice, Agent memory retrieval usually combines:
- Vector retrieval: recalls semantically related content.
- Full-text search: recalls content matching keywords.
- Structured queries: filter by user, time, type, and status.
- Reranking: recalculates the relevance of candidate memories.
A typical hybrid retrieval architecture:
flowchart TD
A["Current Task"] --> B["Query Understanding"]
B --> C["Vector Retrieval"]
B --> D["Keyword Retrieval"]
B --> E["Structured Filtering"]
C --> F["Candidate Memory Set"]
D --> F
E --> F
F --> G["Deduplication and Merging"]
G --> H["Rerank"]
H --> I["Relevance Threshold Filtering"]
I --> J["Build Memory Context"]
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:#F7F4E8,stroke:#C7B77A,color:#675A2D
style F fill:#F5F8F4,stroke:#B5CBBB,color:#40594A
style G fill:#EAF3EC,stroke:#83A995,color:#315D49
style H fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
style I fill:#F7F4E8,stroke:#C7B77A,color:#675A2D
style J fill:#DCEFE5,stroke:#4F8B70,color:#24563F
Hybrid retrieval can improve recall coverage, but it also increases system complexity and query cost.
7. Long-Term Memory Write Mechanism
An Agent should not write every conversation into long-term memory.
For example:
User: Please check the weather in Beijing for me today.
This is usually just a one-off request, and there is no need to keep it as long-term memory.
Whereas:
User: When generating backend code from now on, prefer Go.
This may be a stable preference worth saving.
Writing to long-term memory typically involves the following steps.
1. Extracting Memory Candidates
Extract candidate information from conversations, tool results, and task execution records.
Extractable information includes:
- Explicitly stated preferences.
- Confirmed facts.
- Long-term goals.
- Important decisions.
- Task experience.
- Recurring behavioral patterns.
2. Importance Assessment
Not all candidate information is worth keeping long-term.
You can weigh the following:
| Dimension | What to Evaluate |
|---|---|
| Importance | Whether it is valuable for future tasks |
| Stability | Whether it is likely to hold over the long term |
| Reusability | Whether it can be applied to other tasks |
| Credibility | Whether the information has a reliable source |
| Timeliness | Whether it expires easily |
| Sensitivity | Whether it involves information that should not be stored |
Pay particular attention: high importance does not mean information can be stored unconditionally. You must also comply with user authorization, privacy requirements, and data governance rules.
3. Memory Summarization and Standardization
Convert raw text into structured memory.
For example:
Raw conversation:
User: Our backend mainly uses Go and the database is PostgreSQL;
when designing services from now on, try to follow this tech stack.
Memory after summarization:
{
"type": "preference",
"category": "technology_stack",
"content": {
"backend": "Go",
"database": "PostgreSQL"
},
"confidence": 0.98
}
Once structured, it becomes easier to query, deduplicate, and update.
4. Deduplication and Conflict Detection
Suppose the system has already saved:
User preference: the backend uses Go.
A new conversation extracts again:
User preference: the backend uses Go.
In this case, there is no need to create a duplicate memory entry.
If the new information is:
User preference: new projects should prefer Rust.
This does not necessarily mean the old memory is wrong.
Because the two pieces of information may apply to different projects.
Therefore, conflict detection cannot compare text alone; it must also consider:
- Memory scope.
- Effective time.
- Applicable projects.
- Information source.
- Whether it is explicitly stated as a preference change.
5. Persistent Storage
After validation is complete, write to the appropriate storage based on the memory type.
For example:
- User preferences are written to a structured database.
- Semantic knowledge is written to document stores and vector databases.
- Historical task experiences are written to the episodic memory store.
- Reusable processes are written to the procedural memory store.
6. Long-Term Memory Write Flow
flowchart TD
A["New interaction or task result generated"] --> B["Extract candidate memories"]
B --> C["Determine whether it has long-term value"]
C -->|"No"| D["Keep only in short-term context"]
C -->|"Yes"| E["Sensitive information and authorization check"]
E --> F{"Is storage allowed?"}
F -->|"No"| G["Reject writing or anonymize"]
F -->|"Yes"| H["Summarize and structure"]
H --> I["Assess importance and credibility"]
I --> J["Deduplication and conflict detection"]
J --> K["Select storage type"]
K --> L["Persist memory"]
L --> M["Record source and version"]
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:#F7EAE7,stroke:#C98D7C,color:#754438
style F fill:#F7F4E8,stroke:#C7B77A,color:#675A2D
style G fill:#F7EAE7,stroke:#C98D7C,color:#754438
style H fill:#EAF3EC,stroke:#83A995,color:#315D49
style I fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
style J fill:#EAE8FA,stroke:#A49BD5,color:#51477D
style K fill:#F5F8F4,stroke:#B5CBBB,color:#40594A
style L fill:#DCEFE5,stroke:#4F8B70,color:#24563F
style M fill:#DCEFE5,stroke:#4F8B70,color:#24563F
VIII. Retrieval and Use of Long-Term Memory
Memory writing is only the first step. What truly determines the value of memory is whether the Agent can recall the right information at the right time.
1. Retrieval Triggers
Not every model call requires retrieving long-term memory.
For example:
- When the user asks an independent math question, retrieving user preferences may not be necessary.
- When the user asks to continue the previous project analysis, the relevant task experiences need to be retrieved.
- When the user asks to generate code, the project's technology stack and code style preferences can be retrieved.
Therefore, whether to trigger memory retrieval can be determined based on the task type and query intent.
2. Query Rewriting
The user's current question may be relatively short, and using it directly for vector retrieval may not yield ideal results.
For example:
User: Continue in the same way as before.
The system needs to combine the current session and task state to identify what "the same way as before" specifically refers to.
Query rewriting can supplement:
- The current task topic.
- Relevant entities.
- The project name.
- The user's intent.
- The type of memory that needs to be recalled.
3. Candidate Recall
Recall a set of candidate results from long-term memory.
Common strategies:
- Top-K vector retrieval.
- BM25 keyword retrieval.
- Filter by user ID.
- Filter by project ID.
- Filter by time range.
- Filter by memory type.
4. Re-ranking
Vector similarity is not the same as final relevance.
For example, when the user asks about the payment service architecture, historical memory may contain multiple items that include the keyword "payment":
- Technical architecture of the payment service.
- A log of a payment failure.
- The user's preference for the payment page color.
- Historical reports for the payment business.