Agent Memory System Architecture: From Long-Term Memory to Experiential Memory

    This article explains how to design a reliable memory system for AI agents, moving beyond simply stuffing chat history into the context window. It covers a layered architecture built around long-term memory for facts and background, and experiential memory for reusable strategies and lessons learned. Key topics include write and extraction strategies, hybrid retrieval, versioning and forgetting, trajectory aggregation, retaining failure samples, and using Agentic RL for continuous improvement. It also outlines engineering practices such as layered storage, context budgeting, tenant isolation, observability, and evaluation loops, along with three metrics for判断 whether memory is actually being used. The goal is to turn memory into a foundation for agent evolution rather than a growing cost burden.

    1. The Problem: Why Agents Forget and Fail to Learn

    As AI agents move from demos to production, a recurring bottleneck is memory continuity. Many agents perform well within a single conversation but run into three typical problems across sessions and tasks:

    • Forgetting: constraints a user set last week get asked again this week.
    • Repeating mistakes: the same failing path is tried over and over.
    • No accumulation: an effective approach discovered yesterday is not reused automatically today.

    The industry is converging on a shared view: reliable agent memory is not about stuffing all historical messages into the context window. It requires a layered design, typically split into two layers—long-term memory and experiential memory. The former captures facts and background; the latter captures practices and lessons. Working together, these two layers let an agent both remember context and improve later decisions based on its own action history. This is also the prerequisite for Agentic RL (reinforcement learning for agents) to be practical.

    It is worth stressing that a memory system is not merely a storage component. It is a complete subsystem covering writes, governance, retrieval, updates, forgetting, and evaluation. Building only a vector store does not solve memory quality.

    2. Long-Term Memory: Turning Facts into Retrievable Assets

    The goal of long-term memory is to let an agent quickly restore the necessary context in any new session. It typically contains four types of content:

    1. Raw conversation snippets: keep key turns rather than the full chat log.
    2. Structured facts: project names, environment parameters, API contracts, time windows, and so on.
    3. Scene blocks: package a stretch of task context into a reusable unit, such as “the troubleshooting scene for a certain class of errors.”
    4. User profiles: stable traits such as preferences, taboos, common tech stacks, and communication style.

    2.1 Write Strategy: Extract First, Then Store

    Do not dump raw text directly into storage. A more robust approach is to extract information first, converting the conversation into “fact triples + summary + tags,” then writing them separately into structured storage and a retrieval index. Illustrative code:

    1. def ingest(messages):
    2. facts = extract_facts(messages) # structured facts
    3. scene = summarize_scene(messages) # scene block summary
    4. tags = classify_intent(messages) # task/domain tags
    5. store.write_facts(facts) # structured storage
    6. index.upsert(scene, tags) # retrieval index
    7. profile.merge(extract_profile(messages)) # incremental user profile update

    2.2 Retrieval: Hybrid Search Beats a Single Vector Store

    In production, pure vector retrieval often returns results that are “semantically close but factually wrong.” A more reliable combination is: keyword search to guarantee exact hits (error codes, resource identifiers), vector search to add semantic generalization, structured filters (time, tenant, task type) to narrow the scope, and finally reranking.

    2.3 Updates and Forgetting: Memory Needs Versions

    Facts expire. Attach an effective time, source, and confidence score to each memory entry. On conflict, overwrite the old value with the newer, higher-confidence one rather than simply appending. Sensitive information needs masking and expiration cleanup. This is both a compliance requirement and a significant boost to retrieval signal-to-noise ratio.

    3. Experiential Memory: Turning Practices into Reusable Strategies

    Experiential memory answers a different kind of question: which practices work, which tend to backfire, and what to try first next time. Its smallest unit is not a fact but a trajectory of “situation—action—result.”

    A practical structure looks like this:

    1. {
    2. "context": "dependency download timeout during build stage",
    3. "action": "switch mirror source and add retries",
    4. "result": "success",
    5. "score": 0.82,
    6. "tags": ["build", "network"],
    7. "ts": "2026-08-26T18:00:00Z"
    8. }

    3.1 From Trajectories to Strategies

    A single trajectory has limited value; aggregation is what makes it meaningful. A common approach is to cluster by tags, then compute the success rate and average cost of each action under the same situation to form a “preferred action list.” When a new task matches a similar situation, the agent tries high-success-rate actions first and falls back to a default plan on failure.

    3.2 Failure Samples Must Be Kept

    Recording only successful paths makes the memory system fragile. The value of failure samples lies in providing negative constraints that prevent repeated pitfalls. In practice, give failure trajectories a longer retention period and inject them at retrieval time as “cautions.”

    4. Agentic RL: Giving Memory the Ability to Evolve

    Once experiential memory reaches a certain scale, you can introduce reinforcement-learning-style optimization: use retrieved experience as context, execution results as feedback signals, cumulative return as the measure of policy quality, and adjust retrieval ranking and action priority accordingly. You do not need to train a large model from the start. A more pragmatic path is to begin with rule-based scoring and bandit-style methods for online adjustment, then gradually transition to a parameterized policy.

    To judge whether the memory system is truly “evolving,” look at three metrics: whether the average number of steps for similar tasks drops, whether the repeated failure rate drops, and whether the number of times users restate the same constraint drops. If none of these improve, memory is only being stored, not being used.

    5. Engineering Practices and Common Pitfalls

    • Layered storage: use relational or document storage for structured facts, a retrieval index for long text and scene blocks, and push cold data down to object storage to control costs.
    • Context budget: cap the memory injected into context and truncate by relevance so it does not crowd out reasoning space.
    • Isolation and permissions: in multi-tenant scenarios, enforce strong isolation by tenant and user dimensions, filtering at retrieval time rather than trimming after results are returned.
    • Observability: log the memory entries recalled each time and their hit sources, making it easier to review “why the agent did this.”
    • Evaluation loop: build an offline evaluation set covering three case types—factual Q&A, strategy selection, and conflict updates.

    When implementing on the cloud, you can combine general-purpose capability categories: a container platform to host the agent runtime, a message queue to asynchronously decouple memory writes, logging and monitoring/alerting services for trajectory tracking and anomaly detection, object storage for cold memory, and a caching service to reduce high-frequency recall latency. Baidu AI Cloud offers corresponding product categories for these general capabilities, and you can select based on scale and compliance requirements.

    6. Conclusion

    The competitiveness of agent memory lies not in capacity but in governance and reuse efficiency. Long-term memory solves “knowing the background,” experiential memory solves “knowing how to do it,” and Agentic RL solves “getting better over time.” Do the five things—write, retrieve, update, forget, and evaluate—solidly, and the memory system becomes a true foundation for agent evolution rather than an ever-growing cost burden.