Overcoming AI Agent "Amnesia": Practices and Reflections on Building a Custom Memory System
This article explores the common "amnesia" and intelligence degradation issues that plague AI Agents during cross-session interactions. By analyzing the underlying technical causes—such as confusing volatile context windows with persistent memory—the article introduces a custom external memory system. It details a three-tier memory architecture, structured event modeling, retrieval and injection mechanisms, and periodic state snapshots to enable continuous, traceable, and reliable agent operations.
I. The Starting Point: A Baffling Conversation
After using various autonomous AI Agents for several months, I have gained an intuitive understanding of their strengths and weaknesses. While popular agent frameworks today emphasize “self-evolution” or automated task execution, a common shortcoming remains unresolved: the lack of free, user-friendly, out-of-the-box persistent memory capabilities.
This shortfall is not just a minor UX flaw; it directly leads to task breakage. Here is a real scenario I experienced.
In late April, I asked an AI assistant to work on a project: automatically scraping Product Manager job postings from a recruitment platform and upgrading a previous job analysis report into a “Job Radar” product. I detailed the requirements, and it spent several hours writing code, debugging errors, running the entire data pipeline, and finally confirming the task was complete. Satisfied, I went to sleep.
The next morning, I returned to my computer and said, “Continue. Show me the specific execution results of yesterday’s project.”
Its response was, “Sorry, I don’t remember what you are talking about.”
I pressed further: “We were working on this project all day yesterday. Look at the context. Isn’t this exactly what we were doing just over a dozen hours ago? I just went to sleep and came back, and you don’t remember anything?”
That feeling was hard to describe—an AI that spent hours completing a complex task completely forgot what it had done the next day. In its cognition, the “completion” of the task equated to a reset to zero.
II. Beyond Amnesia: The Hidden “Intelligence Degradation”
If cross-session amnesia is an explicit problem, another situation is even more frustrating: the agent is clearly running but quietly “dumbing down” during execution.
Here are two examples I observed in actual use.
Case 1: Executed, but unaware of its own execution.
I asked the assistant to restart a certain gateway service. Following its preset safety rules, it replied: “According to the iron rule, gateway restarts require your confirmation and on-site monitoring. The command to be executed is ready. Confirm execution?” I confirmed, and it executed smoothly.
The next morning, I asked: “Did the restart complete last night?”
It answered: “Which restart are you talking about? I haven’t executed any gateway restart operations.”
The fact is, it did execute it the night before, and I was present for the entire confirmation process. It performed the action but had no perception of its own execution history. This type of issue is particularly dangerous when it comes to infrastructure operations—operators cannot trace what the agent did, and auditing and post-mortems completely fail.
Case 2: Drift in state perception.
In the same conversation, I asked about the version status of a service component, and it replied that everything was normal. However, upon actual inspection, two of its components were three minor versions apart. It spent a long time analyzing before finally “discovering” the version inconsistency—and I couldn’t be sure when it first observed this issue, nor did it explain which features were affected.
This exposes a deeper flaw: the agent lacks a continuous record of its own state history, causing its “self-awareness” to be fragmented and instantaneous. It can only make judgments based on the world it sees at this exact moment, unable to answer “how did I get here.”
III. The Technical Essence: Why the Amnesia?
Abstracting these phenomena, the root causes can be attributed to three layers:
- Session context ≠ memory. Most agents’ “memory” is just the current session’s context window. Once the session ends or the context becomes too long, the content is truncated, and everything prior ceases to exist for the model. Context is volatile; memory should be persistent.
- Execution logs are not consolidated into structured events. When an agent executes commands, produces code, or completes tasks, it generates massive logs. However, these logs are often scattered across terminal outputs or temporary files, never summarized into a structured event stream of “what time, based on what instruction, what was done, and what was the result.”
- Lack of retrieval and injection mechanisms. Even if history is saved, it requires retrieving relevant history based on the user’s current intent at the start of each new session and injecting it into the context appropriately. Without this step, the saved history is just dead data.
IV. My Solution: An External Memory System for Agents
Since existing frameworks didn’t provide a satisfactory answer, I decided to build a plug-and-play external memory module myself. The overall design approach is as follows:
4.1 Architecture: Three-Tier Memory Model
┌─────────────────────────────────┐│ Short-term Memory: Current session context │├─────────────────────────────────┤│ Working Memory: Summaries of recent N tasks │├─────────────────────────────────┤│ Long-term Memory: Structured event library (Persistent) │└─────────────────────────────────┘
- Short-term memory is the model’s native context, without extra processing.
- Working memory generates a summary at the end of each session turn, retaining the recent task context.
- Long-term memory writes all key events into structured storage.
4.2 Structured Event Modeling
Each memory entry is not a blob of raw text, but an event record with metadata:
{"event_id": "evt_20260430_142","timestamp": "2026-04-30T23:12:00+08:00","type": "task_complete","intent": "Complete job information scraping pipeline","actions": ["Write scraper script", "Fix 3 field parsing bugs", "Run data ingestion"],"artifacts": ["Pipeline script path", "First-day dataset path"],"result": "success","requires_followup": true}
The key field is requires_followup—it marks that “this task has a follow-up,” which exactly solves the scenario of “coming back after a night’s sleep to continue the work.”
4.3 Retrieval and Injection
When a new session starts, the external module performs intent matching based on the user’s message, retrieves the most relevant historical records from the event library, compresses them into a “background briefing,” and injects it into the system prompt. This way, before the agent even speaks, it “remembers” what it did yesterday and at what step it left off.
4.4 Self-State Snapshots
To address the “dumbing down” issue, I introduced an additional periodic state snapshot mechanism: it periodically collects the versions, running status, and recent operation logs of key components, writing them into the event library. This allows the agent to answer “when did I discover this, and what was the state at the time,” turning instantaneous observations into a traceable state timeline.
V. Outcomes and Reflections
With this external memory module added, both of the aforementioned issues were substantially improved: when continuing a task across days, the agent can accurately pick up yesterday’s progress; when performing infrastructure operations, it can fully recount its execution history, restoring auditing capabilities.
Here are a few takeaways:
- Memory is not optional; it is the infrastructure of an agent. An agent without persistent memory is essentially a system where “everything is being done for the first time.” The higher the task complexity and continuity, the more fatal this flaw becomes.
- Execution history must be structured. Raw logs have no cognitive value. Only when summarized into events with intent, results, and timestamps can they be effectively utilized by the model.
- Retrieval and injection are more important than bulk storage. The value of a memory system lies not in how much it stores, but in injecting the right historical fragments at the right time.
- Traceability is the safety baseline. For agents that operate in real environments, “knowing what you have done” is just as important as “being able to do it.”
For developers building or using agents, my advice is: do not wait until your agent says “Sorry, I don’t remember” to think about memory. Treat it as a first-class citizen in your architecture design. Plan your event modeling, persistence, and retrieval/injection strategies early, so your agent can truly evolve from “capable of working” to “working continuously and reliably.”
