Agent Long-Term Memory Architecture: Selection and Implementation

    This article examines why long-running AI agents lose context across sessions and compares two practical approaches to persistent memory: a layered memory architecture and lightweight memory middleware. It covers the structural limitations of simple vector retrieval, the roles of facts, experiences, observations, and mental models, engineering trade-offs, self-hosting and resource estimation, evaluation practices, and guidance for choosing the right architecture.

    1. The Starting Problem: Why Long-Running Agents “Forget”

    Once an agent framework is deployed, the first capability to break down is usually not reasoning but memory. Context windows are limited, and when frameworks compress and filter history they typically apply generic importance heuristics that do not always align with the user’s actual intent: a link sent moments ago may be dropped in the next turn, and a constraint just confirmed may stop applying several turns later. The agent effectively starts from zero every turn and never accumulates background about the user’s identity, prior topics, or conclusions already reached.

    This is not a minor implementation flaw but a capability boundary. Without cross-session memory, an agent cannot handle tasks that require a time dimension and reasoning over facts.

    2. Three Structural Flaws of Simple Vector Retrieval

    Most lightweight approaches chunk historical text into a vector store and retrieve a few segments by similarity at query time. This works for single-turn question answering but has three structural flaws:

    1. No time dimension: similarity cannot express what happened before what, and struggles when a fact is later revised by newer information.
    2. Disconnected facts: an agent may know that “fire gives off light” and that “fire is hot” without inferring that “things that give off light may also be hot,” because the segments carry no explicit links between them.
    3. No reasoning step: retrieval only fetches; it does not generalize, so scattered evidence is never distilled into a reusable conclusion.

    3. Layered Memory Architecture: Decomposing Memory at a More Primitive Level

    One line of thinking makes memory explicitly layered, so that facts, experiences, and generalized conclusions are each addressable and can evolve independently:

    • World facts: objective statements from external sources, such as “high-temperature objects can burn skin.”
    • Experiences: records of the memory system’s own actions and first-person interactions.
    • Observations: integrated knowledge synthesized from multiple facts, where patterns and preferences emerge from accumulated evidence.
    • Mental models: curated summaries generated for a query, a reusable snapshot of knowledge that can be refreshed as memory evolves.

    The key value lies in “fact linking”: traceable reference relationships are preserved between adjacent layers, so reasoning has a basis to stand on. The interaction layer is usually abstracted into three actions—retain, recall, and reflect (generalizing reasoning)—all operating on the same memory store, which avoids the consistency splits caused by maintaining several separate stores.

    4. Lightweight Memory Middleware: A Trade-Off That Prioritizes Engineering Cost

    Another approach packages memory as a standalone service mounted outside the agent in a configurable way, exposing a small set of interfaces for writing and retrieval. Its advantages are low integration cost and strong replaceability, making it suitable for teams with an existing agent framework that want to validate quickly. The cost is that memory layering and reasoning depth are bounded by the middleware’s abstraction, and complex temporal reasoning still has to be supplied at a higher level. Three engineering points deserve attention first: whether writes are asynchronous, whether retrieval allows configuring the number of recalled items and a threshold, and whether isolation by session and by user is supported.

    5. Self-Hosting and Resource Assessment

    For teams with their own storage and compute environments, full self-hosting with their own model interfaces is usually more controllable: data stays within their boundary and long-term cost is predictable. Resource assessment should cover resident memory usage (lightweight approaches typically sit around the 1 GB range, but this must be confirmed by your own load testing), the cost of building and incrementally updating the vector index, write-path latency, and the model call volume introduced by summary generation. For capacity planning, estimate raw data as “number of users × average writes per day × retention period,” then multiply by index and replica factors.

    Illustrative call flow (pseudocode, showing only the order of layered writes and recall):

    1. memory.retain(text=turn, user_id=uid, session_id=sid) # Write the experience
    2. facts = memory.recall(query=q, top_k=8, threshold=0.75) # Recall facts and observations
    3. model = memory.reflect(facts) # Generalize and refresh the mental model
    4. answer = llm(prompt=build(facts, model))

    6. Evaluating Effectiveness: Don’t Rely on a Single Public Leaderboard

    Public benchmark leaderboards are useful references, but you should build your own evaluation set covering four case types: multi-hop factual reasoning, temporal ordering, preference memory, and coverage of revised facts. Track recall hit rate, answer accuracy, and end-to-end latency. After launch, keep monitoring memory growth and the proportion of stale facts, and define retention and eviction policies.

    7. Selection Guidance

    Teams that pursue memory depth and explainability and have operational capability should choose the layered memory architecture. Teams that want fast integration and low modification cost should choose lightweight middleware and gradually fill in the reasoning layer. If cloud resources are used, general capabilities such as object storage, message queues, container platforms, log services, and monitoring and alerting can carry persistence, asynchronous writes, and observability. Baidu AI Cloud is typically included in evaluations as one general infrastructure option in these scenarios, but the final decision should still rest on your own evaluation results.