A method for deep optimization of memory of an AI agent
By constructing a hierarchical storage architecture based on Markdown files and a dynamic relevance ranking algorithm, combined with vector semantic search and BM25 retrieval, the problems of high storage cost and low retrieval efficiency of AI Agent are solved, achieving lightweight storage and adaptive memory management, and improving the task completion rate and interaction continuity of AI Agent.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- BEIJING YIXUN ZHENGTONG NETWORK COMM TECH CO LTD
- Filing Date
- 2026-04-16
- Publication Date
- 2026-07-10
AI Technical Summary
Existing AI agents suffer from problems such as high storage costs, low retrieval efficiency, poor adaptability, and inability to proactively capture and recall key memories.
It adopts a hierarchical storage architecture based on Markdown files, combines a hybrid strategy of vector semantic search and BM25 keyword retrieval, and achieves lightweight storage and adaptive retrieval through dynamic relevance ranking (DRS) algorithm and online learning to optimize memory management.
It reduced system deployment and operation costs, improved the accuracy and adaptability of search results, and enhanced the task completion rate and interaction continuity of the AI Agent.
Smart Images

Figure CN122364283A_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the field of artificial intelligence technology, and in particular to a method for deep optimization of memory in AI agents. Background Technology
[0002] With the development of large language models and intelligent agent technology, AI agents are no longer just stateless tools, but are evolving into intelligent partners capable of remembering the past. Memory ability is key for intelligent agents to achieve personalization, continuous interaction, and task planning. In existing technologies, the memory management of AI agents mainly suffers from the following problems:
[0003] (1) Memory storage often uses a unified database or a dedicated vector storage system, which increases the system deployment and maintenance costs and is not conducive to lightweight applications.
[0004] (2) Existing memory retrieval mostly uses simple vector similarity matching, which lacks comprehensive consideration of multiple dimensions such as semantic similarity, temporal proximateness, importance, access frequency, and task alignment, resulting in retrieval results that do not match the current context.
[0005] (3) The weights of existing technologies are fixed by humans and cannot be dynamically optimized according to actual usage scenarios, resulting in a decline in search results as the task changes. Furthermore, the fixed weights cannot be adapted to different application scenarios, resulting in low search efficiency and failing to meet the needs of real-time interaction of AI Agents.
[0006] (4) The capture and retrieval of memories rely on manual triggering and lack an automated mechanism, which makes it impossible for the AI Agent to actively extract key memories in the dialogue and to accurately recall relevant memories in subsequent dialogues, affecting the continuity and intelligence of the interaction.
[0007] Therefore, how to solve the problems of high memory storage and operating costs, lack of adaptability, and low task completion rate in existing technologies is a technical problem that needs to be solved. Summary of the Invention
[0008] Based on this, the present invention provides a method for deep optimization of AI agent memory. The present invention uses a lightweight file system storage architecture to integrate a dynamic relevance ranking (DRS) algorithm with five dimensions of semantic similarity, temporal proximateness, importance, access frequency and task alignment, adaptive weight learning and online optimization of retrieval results to achieve adaptive intelligent agent memory management, reduce system operating costs and improve task completion rate.
[0009] One embodiment of the present invention provides a method for deep optimization of memory in an AI agent. The method includes: constructing a hierarchical memory storage architecture based on Markdown files to store long-term memory and short-term memory divided by date; adopting a hybrid retrieval strategy that integrates vector semantic search and BM25 keyword retrieval, and combining MMR deduplication and time-series decay strategies to obtain a candidate memory set; calculating a comprehensive score for the candidate memory set using a dynamic relevance ranking (DRS) algorithm that integrates five dimensions: semantic similarity, temporal proximateness, importance, access frequency, and task alignment; outputting the final retrieval results in descending order of the score; and adaptively updating the weight coefficients of each dimension based on retrieval feedback through an online learning algorithm; and realizing automatic triggering of memory capture and recall functions.
[0010] Furthermore, the aforementioned hierarchical memory storage architecture based on Markdown files stores long-term memory and short-term memory divided by date. Specifically, this includes: storing the AI Agent's memory data as Markdown files in the file system, creating an independent directory for each user session, with the directory containing long-term memory files and short-term memory log folders; the long-term memory files are used to store user preferences, habits, decision reasons, and project background; the short-term memory log folders are used to store daily conversation logs, temporary contexts, and event records divided by date; when the AI Agent starts a single-user session, it only loads the aforementioned long-term memory files and the short-term memory files for the current date and the previous day.
[0011] Furthermore, the above-mentioned hybrid retrieval strategy, which integrates vector semantic search and BM25 keyword retrieval, and combines MMR deduplication and time-series decay strategies to obtain a candidate memory set, specifically includes: dividing the aforementioned memory file into text blocks and converting them into vectors, storing them in a vector database; calculating the similarity between the query vector and the memory vector during a query to obtain a vector semantic search score, and then calculating the keyword matching score between the query statement and the memory content using the BM25 algorithm to obtain a BM25 keyword search score; and finally, fusing the vector semantic search score and the BM25 keyword search score to obtain a comprehensive retrieval score. The formula for calculating the comprehensive retrieval score, FinalScore, is as follows: Where VectorScore is the vector semantic search score, and BM25Score is the BM25 keyword search score; the comprehensive retrieval score is deduplicated using MMR, and then sorted in descending order by time-decay score to obtain the candidate memory set. The formula for calculating the time-decay score is as follows: Where RawScore is the overall retrieval score, λ is the decay coefficient, and ageInDays is the number of days since the last memory entry was created.
[0012] Furthermore, for the candidate memory set, a comprehensive score is calculated using the Dynamic Relevance Ranking (DRS) algorithm, which integrates five dimensions: semantic similarity, temporal recency, importance, access frequency, and task alignment. The final retrieval results are then sorted in descending order of the scores. Based on retrieval feedback, the weight coefficients of each dimension are adaptively updated using an online learning algorithm. Specifically, for the candidate memory set, the Dynamic Relevance Ranking (DRS) algorithm is used to achieve deep optimization retrieval of memories. A comprehensive score is constructed by integrating semantic similarity, temporal recency, importance, access frequency, and task alignment. A comprehensive relevance score is calculated for the candidate memories and sorted in descending order of the scores. The Top-K relevant memories are returned as the final retrieval results. Based on the actual citation status or task completion reward of the top K memories in the retrieval results, the weight coefficients of the five dimensions are periodically updated using an online gradient descent algorithm or the Bandit algorithm. The formula for calculating the comprehensive relevance score DRS_score is: Where α, β, γ, δ, and ε are the weight coefficients of each dimension, and α+β+γ+δ+ε=1, sim is the semantic similarity, recency is the temporal recency, importance is the importance, frequency is the access frequency, and task_alignment is the task alignment.
[0013] Furthermore, the above-mentioned automatic memory capture and retrieval function includes: automatically capturing and retrieving the above-mentioned memories; automatically extracting key information to generate attributed memory entries and adding them to the memory bank after the dialogue ends; and automatically retrieving relevant memories and injecting them into the conversation context based on user queries before each dialogue.
[0014] This invention provides a method for deeply optimizing the memory of an AI agent. First, a hierarchical storage architecture based on Markdown files is constructed to replace traditional unified databases or dedicated vectors, significantly reducing deployment and maintenance costs. Furthermore, the lightweight memory loading strategy reduces token consumption, lowering system operating costs. During the retrieval phase, a hybrid retrieval strategy combining vector semantic search and BM25 keyword matching is employed, along with maximum boundary relevance (MMR) algorithm for deduplication and a temporal decay strategy. In addition, the method introduces a dynamic relevance ranking (DRS) algorithm, which comprehensively calculates scores across five dimensions: semantic similarity, temporal proximity, importance, access frequency, and task alignment. An online learning algorithm adaptively updates the weight coefficients of each dimension, thereby achieving efficient memory capture and accurate recall. Attached Figure Description
[0015] To more clearly illustrate the technical solutions in the embodiments of the present invention, the accompanying drawings used in the description of the embodiments will be briefly introduced below. Obviously, the accompanying drawings described below are only some embodiments of the present invention. For those skilled in the art, other drawings can be obtained based on these drawings without creative effort.
[0016] Figure 1 A flowchart illustrating a method for deep memory optimization in AI agents, as proposed in an embodiment of the present invention, is shown.
[0017] Figure 2 A schematic diagram of the hybrid retrieval process proposed in an embodiment of the present invention is shown;
[0018] Figure 3 A schematic diagram of the dynamic relevance ranking (DRS) retrieval optimization process proposed in an embodiment of the present invention is shown;
[0019] Figure 4 A schematic diagram of the automatic memory capture and retrieval process proposed in an embodiment of the present invention is shown. Detailed Implementation
[0020] The technical solutions of the embodiments of the present invention will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only some embodiments of the present invention, and not all embodiments. Based on the embodiments of the present invention, all other embodiments obtained by those skilled in the art without creative effort are within the scope of protection of the present invention.
[0021] This invention provides a method for deep optimization of memory in AI agents, such as... Figure 1 As shown, it includes the following steps:
[0022] S1 constructs a hierarchical memory storage architecture based on Markdown files, storing long-term memory and short-term memory divided by date.
[0023] Specifically, in a preferred embodiment of the present invention,
[0024] The AI Agent's memory data is stored in the file system as Markdown files. A unique independent directory is created for each user session. Each user directory contains a fixed long-term memory file (MEMORY.md) and a short-term memory log folder. The long-term memory file (MEMORY.md) stores long-term valid information such as user preferences, habits, decision-making rationale, and project background. The short-term memory log folder stores daily short-term memory files named in the YYYY-MM-DD date format, used to store the daily conversation log, temporary context, and event records. When starting a session, the AI Agent only loads the long-term memory file (MEMORY.md) and the short-term memory files for the current date and the previous day to avoid loading too much historical context and reduce token consumption. For group chat scenarios, to avoid information confusion, the AI Agent does not load any long-term memory files when starting a session, only the short-term memory files for the current day. For example, for the user "Zhang San", the MEMORY.md file in his independent directory stores long-term information such as "Zhang San prefers hand-drip light roast coffee, needs to submit a weekly work report every Monday, and is responsible for the product design of the XX project"; the 2026-03-30.md file in the memory subdirectory stores short-term information for the day such as "discussed the requirement change of the XX project with Zhang San on March 30, and Zhang San inquired about flight information for his business trip to Shanghai".
[0025] S2 employs a hybrid retrieval strategy that combines vector semantic search with BM25 keyword retrieval, and combines MMR deduplication and time-series decay strategies to obtain a candidate memory set.
[0026] Specifically, in a preferred embodiment of the present invention, such as Figure 2 As shown, the specific implementation steps are as follows:
[0027] S201 splits all Markdown memory files in the user's directory into text blocks, calls the embedding API to convert each text block into a vector, and stores the vector data in a vector database;
[0028] S202, when a user initiates a query, a lightweight embedding model (such as Sentence-BERT) is called to convert the user's query statement into a vector, and the cosine similarity between the query vector and the memory vector in the vector database is calculated to obtain the vector semantic search score VectorScore;
[0029] S203, and at the same time, the BM25 algorithm is used to match keywords between the query statement and the memory content to calculate the BM25 keyword search score BM25Score;
[0030] S204, according to the formula By combining the two scores, the final score of the hybrid retrieval model is obtained.
[0031] S205, perform MMR deduplication on the comprehensive search score results to remove highly similar memory results and improve the diversity of search results; then follow the formula Calculate the temporal decay score, where RawScore is the overall retrieval score, λ is the decay coefficient, and ageInDays is the number of days since the memory entry was created. Finally, sort the candidate memory sets in descending order according to the temporal decay score.
[0032] In this embodiment, the preferred text block size is 400 Tokens, and λ is 0.0231 (30-day half-life). That is, λ is set to a value that reduces the overall retrieval score of the memory entries from 30 days ago to 50% (today's memory score is 100%, 7 days ago it is 84%, and 90 days ago it is 12.5%).
[0033] For example: Zhang San initiates a query "What kind of coffee do I like?". S202 calculates VectorScore=0.89 (corresponding to the text block of coffee preferences in MEMORY.md), S203 calculates BM25Score=0.85, and S204 merges to get FinalScore=0.7×0.89+0.3×0.85=0.88. After MMR deduplication, coffee memories in 2026-02-08.md that are highly similar to 2026-02-10.md are removed. Then, the memory decays over a 30-day half-life. The memory ranked first in the final candidate memory set is "Your coffee preference is hand-drip, light roast, and you prefer Yirgacheffe beans; on February 10, 2026, you purchased hand-drip light roast Yirgacheffe coffee and reported a refreshing taste."
[0034] S3 calculates a comprehensive score for the candidate memory set by integrating five dimensions: semantic similarity, temporal proximateness, importance, access frequency, and task alignment. The final retrieval results are then sorted in descending order of the scores, and the weight coefficients of each dimension are adaptively updated based on the retrieval feedback through an online learning algorithm.
[0035] Specifically, in a preferred embodiment of the present invention, such as Figure 3 As shown, the specific implementation steps are as follows:
[0036] S301, calculate the score for each candidate memory across five dimensions, then apply the formula... Calculate the overall relevance score, where α, β, γ, δ, and ε are the weight coefficients of each dimension, and satisfy α + β + γ + δ + ε = 1; the calculation rules for each dimension score are as follows:
[0037] Semantic similarity (sim): Cosine similarity between the query vector and the memory vector, with a value range of [0,1].
[0038] Recency: According to the formula Calculation: τ is the time constant (1 day in this embodiment), t_now is the current time, and t_m is the memory entry generation time, with a value range of [0,1].
[0039] Importance: The initial value is set by the AI Agent. It is set to 0.8 when the user explicitly says "remember this" and 0.3 for general observation information. The value range is [0,1].
[0040] Access frequency: If the memory records at least two accesses, avg_gap_hours is the average interval in hours; otherwise, it is 0.1, with a value range of [0,1].
[0041] Task alignment: 1 is the value when the task label of the memory entry exactly matches the current query task, and 0 is the value when they do not match.
[0042] In this embodiment, a preferred initial weight set is α=0.4, β=0.3, γ=0.1, δ=0.1, ε=0.1, which can be dynamically adjusted according to the performance feedback of the AI Agent.
[0043] S302, calculate the comprehensive relevance score of all candidate memories, sort them in descending order of score, and return the Top-K relevant memories with the highest scores as the final search results;
[0044] S303, in order to adapt to different scenarios, uses an online gradient descent algorithm or the Bandit algorithm to periodically update the weight coefficients α, β, γ, δ, and ε based on the actual citation status of the top K memories in the retrieval results or the task completion reward.
[0045] Online gradient descent: After each retrieval, if the AI Agent performs an action and receives an environmental reward R, the weights are adjusted based on the actual usage of the first K memories (such as clicks and references); Bandit algorithm update: Set a set of candidate weight vectors, periodically evaluate the quality of retrieval results (such as user feedback and task completion rate), and dynamically select the optimal weight combination; After the weights are updated, only the metadata of the memory entries is updated, without rebuilding the vector index, thus ensuring retrieval efficiency.
[0046] For example, if Zhang San queries "What is my schedule for tomorrow?", the candidate memory "Going on a business trip to Shanghai on March 31st" has a sim=0.92, recency=1.0, importance=0.8, frequency=0.1, and task_alignment=1. Calculated using the initial weights, its comprehensive score is DRS_score=0.4×0.92+0.3×1.0+0.1×0.8+0.1×0.1+0.1×1.0=0.848, making it a Top-1 candidate memory, and is returned to the user. If this memory helps the AI Agent answer the question accurately and receives an environmental reward R=1, the system fine-tunes the weights using the Exp3 algorithm, increasing the weights of temporal recency and task alignment. When updating weights using online gradient descent, the loss function can be defined as... Where R is the task completion reward (0 or 1), and score_top is the DRS score of the adopted memory. The update rule for weight α is as follows: η is the learning rate (set to 0.01). If a memory with a high "recency" score is adopted and the task is successful (R=1) in a certain retrieval, then the loss L=0, and the weight remains unchanged; if it is not adopted (R=0), the weight is adjusted according to the score. This allows the memory system to automatically evolve as the usage scenario changes.
[0047] S4 enables automatic triggering of memory capture and recall functions.
[0048] Specifically, in a preferred embodiment of the present invention, such as Figure 4 As shown, the specific implementation steps are as follows:
[0049] S401 After the AI Agent finishes its conversation with the user, it uses natural language processing technology to intelligently parse the conversation content and extract key information such as tasks, preferences, and events; it generates standardized memory entries with timestamps, importance scores, and task tags; it converts these memory entries into vectors and adds them to the vector database, while simultaneously updating the vector index and memory metadata (such as access records and importance scores) in the memory database.
[0050] S402, before the user initiates a new conversation, the AI Agent parses the user's query and generates core search keywords; based on the Dynamic Relevance Ranking (DRS) algorithm of this invention, it retrieves the Top-K memories related to the query and encapsulates the search results into a preset format. <relevant-memory>Relevant memory tags are injected into the AI Agent's conversation context, and the AI Agent provides users with personalized interaction and task planning services based on this memory.
[0051] For example, after Zhang San's conversation with the AI Agent ends, the system parses the key event "going on a business trip to Shanghai on March 31st," generates a memory entry with a timestamp of 2026-03-30, an importance score of 0.8, and the task tag "travel," and adds it to the vector database. When Zhang San subsequently queries "What do I need to prepare for a business trip to Shanghai?", the AI Agent parses search keywords such as "business trip to Shanghai," "preparation," and "itinerary," retrieves the aforementioned memory entry, and encapsulates it into... <relevant-memory> - [events] Went to Shanghai on a business trip on March 31st< / relevant-memory> Inject context and recommend things to prepare for Zhang San's business trip.
[0052] Compared to existing AI Agent memory management systems that use a unified database and dedicated vector storage system, which suffer from inaccurate retrieval results and poor adaptability due to single vector similarity matching, this invention offers the following advantages: The hierarchical storage architecture based on Markdown files not only reduces storage costs, but its strategy of loading only the short-term memory of the last two days synergizes with the subsequent time-decay retrieval strategy. The proactive truncation of the storage layer reduces the amount of data during retrieval, allowing the time-decay algorithm to focus more on truly important recent information, while significantly reducing the computational cost and token consumption for each retrieval, achieving a balance between lightweight deployment and retrieval accuracy. By integrating a dynamic relevance ranking (DRS) algorithm based on five dimensions—semantic similarity, temporal proximateness, importance, access frequency, and task alignment—the retrieval results are highly relevant to the current context. It supports scenario-based optimization of initial weights and can adapt to different application scenarios such as dialogue interaction and task planning through adaptive weight learning and online optimization based on retrieval feedback. Simultaneously, the AI Agent can proactively capture key dialogue information and accurately recall relevant memories. Therefore, this invention effectively improves the AI Agent's adaptability and task completion rate, while reducing system storage and operating costs.
[0053] Finally, it should be noted that the above embodiments are only used to illustrate the technical solutions of the present invention and not to limit it. Although the present invention has been described in detail with reference to the above embodiments, those skilled in the art should understand that modifications or equivalent substitutions can still be made to the specific implementation of the present invention. Any modifications or equivalent substitutions that do not depart from the spirit and scope of the present invention should be covered within the scope of protection of the claims of the present invention.
Claims
1. A method for deep optimization of memory in AI agents, characterized in that, The method includes: Construct a hierarchical memory storage architecture based on Markdown files, storing long-term memory and short-term memory divided by date; A hybrid retrieval strategy combining vector semantic search and BM25 keyword retrieval is adopted, and candidate memory sets are obtained by combining MMR deduplication and time-series decay strategies. For the candidate memory set, a comprehensive score is calculated by the Dynamic Relevance Ranking (DRS) algorithm, which integrates five dimensions: semantic similarity, temporal proximateness, importance, access frequency, and task alignment. The final retrieval results are output in descending order of the scores, and the weight coefficients of each dimension are adaptively updated based on the retrieval feedback through an online learning algorithm. Enables automatic triggering of memory capture and recall functions.
2. The method according to claim 1, characterized in that, The construction of a hierarchical memory storage architecture based on Markdown files, which stores long-term memory and short-term memory divided by date, specifically includes: The AI Agent's memory data is stored in the file system as Markdown files, and an independent directory is created for each user session. The directory contains long-term memory files and short-term memory log folders. The long-term memory file is used to store user preferences, habits, decision-making rationale, and project background; The short-term memory log folder is used to store daily conversation logs, temporary contexts, and event records, all separated by date. When the AI Agent initiates a single-user session, it loads only the long-term memory file and the short-term memory file for the current date and the previous day.
3. The method according to claim 1, characterized in that, The hybrid retrieval strategy, which integrates vector semantic search and BM25 keyword retrieval, and combines MMR deduplication and time-series decay strategies to obtain the candidate memory set, specifically includes: The memory file is segmented into text blocks and converted into vectors, which are then stored in a vector database. During a query, the similarity between the query vector and the memory vector is calculated to obtain a vector semantic search score. Then, the keyword matching score between the query statement and the memory content is calculated using the BM25 algorithm to obtain the BM25 keyword search score. The vector semantic search score and the BM25 keyword search score are combined to obtain a comprehensive retrieval score. The formula for calculating the comprehensive retrieval score (FinalScore) is as follows: ; VectorScore is the vector semantic search score, and BM25Score is the BM25 keyword search score. The comprehensive retrieval score is deduplicated using MMR (Mean Replication) and then sorted in descending order by temporal decay score to obtain the candidate memory set. The formula for calculating the temporal decay score is as follows: ; Where RawScore is the overall retrieval score, λ is the decay coefficient, and ageInDays is the number of days since the last memory entry was created.
4. The method according to claim 1, characterized in that, The candidate memory set is evaluated using a Dynamic Relevance Ranking (DRS) algorithm that integrates five dimensions: semantic similarity, temporal proximateness, importance, access frequency, and task alignment. The final retrieval results are then sorted in descending order of score. Furthermore, the weight coefficients of each dimension are adaptively updated using an online learning algorithm based on retrieval feedback. Specifically, this includes: For the candidate memory set, a deep optimization retrieval of memories is achieved through the Dynamic Relevance Ranking (DRS) algorithm. A comprehensive score is constructed by integrating semantic similarity, temporal proximity, importance, access frequency, and task alignment. The comprehensive relevance score is calculated for each candidate memory and sorted in descending order of score. The Top-K relevant memories are returned as the final retrieval results. Based on the actual citation status or task completion reward of the top K memories in the retrieval results, the weight coefficients of the five dimensions are periodically updated using an online gradient descent algorithm or the Bandit algorithm. The formula for calculating the comprehensive relevance score DRS_score is: ; Where α, β, γ, δ, and ε are the weight coefficients of each dimension, and α+β+γ+δ+ε=1, sim is the semantic similarity, recency is the temporal recency, importance is the importance, frequency is the access frequency, and task_alignment is the task alignment.
5. The method according to claim 1, characterized in that, The automatic triggering of memory capture and retrieval functions specifically includes: The system enables automatic capture and retrieval of memories. After a conversation ends, key information is automatically extracted to generate memory entries with attributes and added to the memory bank. Before each conversation, relevant memories are automatically retrieved based on user queries and injected into the conversation context.