GUIDE

Architecting Agent Memory: Working, Episodic, and Long-Term Systems

By Chris DavisPublished 2026-08-20

Overview

Foundation models operate as stateless functions. Every invocation begins with an empty execution state unless historical context is explicitly injected into the prompt window. While simple conversational applications can pass a sliding window of recent messages, autonomous agents executing multi-step tasks across days or weeks require structured, persistent memory architectures.

Without dedicated memory infrastructure, agents exhibit critical operational failure modes:

  • Forgetting user preferences and previously verified facts across sessions.
  • Repeating expensive or failed tool executions in cyclic loops.
  • Overfilling active context windows with redundant conversational transcripts.
  • Failing to resolve contradictions when user instructions evolve over time.

To solve these challenges, context engineering structures agent memory into a multi-tier hierarchy modeled after cognitive memory systems and virtual memory management in operating systems.

flowchart TD
    App["AUTONOMOUS AGENT RUNTIME"]
    
    App --> Working["WORKING MEMORY<br>(Active Context Window)<br>- Core persona & rules<br>- Scratchpad & state block<br>- Recent message queue<br>- Current tool outputs"]
    App --> LongTerm["LONG-TERM MEMORY<br>(Persistent Storage)"]
    
    LongTerm --> Episodic["EPISODIC MEMORY<br>- Chronological event logs<br>- Session interaction traces<br>- Tool execution receipts"]
    LongTerm --> Semantic["SEMANTIC MEMORY<br>- Extracted user facts<br>- Entity-relationship graph<br>- User preference store"]
    
    Episodic -->|Paging & Retrieval Flow| Working
    Semantic -->|Paging & Retrieval Flow| Working

The Four Memory Tiers

Memory Tier Cognitive Function Storage Medium Read/Write Latency Retrieval Mechanism
Working Memory Active reasoning & execution scratchpad Active prompt tokens Zero (In-context) Direct attention over prompt window
Episodic Memory Chronological record of historical events Time-series database / Log store Fast (10–50ms) Timestamp filtering & session query
Semantic Memory Deduplicated factual knowledge & relationships Knowledge graph / Vector store Medium (20–100ms) Entity lookup & vector similarity
Procedural Memory Execution routines, tool schemas, and skills Code repository / Prompt library Static Function registration & schema injection

Architectural Patterns for Agent Memory

Production systems utilize two primary architectural paradigms for managing long-term state:

1. The Virtual Memory Model (OS-Style Paging)

Pioneered by architectures like MemGPT and Letta, this approach treats the LLM context window as physical RAM and external storage as hard disk memory.

flowchart TD
    subgraph RAM["LLM PROMPT WINDOW (RAM)"]
        Core["CORE MEMORY (Always in prompt)<br>Persona: Assistant specializing in financial data analysis.<br>Human: User is CTO at FinTech Corp. Prefers concise Python scripts."]
        Buffer["WORKING MESSAGE BUFFER<br>Last N user/agent dialogue turns"]
    end
    
    RAM -->|Tool Calls<br>core_memory_append, archival_search| Disk
    
    subgraph Disk["EXTERNAL STORAGE TIERS (DISK)"]
        Recall["RECALL MEMORY<br>Complete historical conversation log (searchable by text & timestamp)"]
        Archival["ARCHIVAL MEMORY<br>Document store & vector index (paged into context via explicit tools)"]
    end

In this model, the agent explicitly modifies its own core memory blocks using tool calls (e.g. core_memory_replace, core_memory_append). When external facts are required, the agent issues explicit retrieval tool calls (archival_memory_search) to page data into its working memory.

2. The Asynchronous Extraction Model (Background Graph Synthesis)

Used by platforms like Zep, Mem0, and Cognee, this approach decouples memory extraction from the active conversation turn.

flowchart TD
    User["User Dialogue Turn"] --> Response["Active Response Generation"]
    
    User -->|Asynchronous Event| Pipeline["Background Ingestion Pipeline"]
    
    Pipeline --> NER["1. Named Entity Recognition (Extract entities: Person, Org, Project)"]
    Pipeline --> Fact["2. Fact Extraction (Extract atomic propositions)"]
    Pipeline --> Conflict["3. Conflict Resolution (Evaluate if new fact invalidates existing fact)"]
    Pipeline --> Update["4. Graph Update (Upsert nodes and temporal edges in Knowledge Graph)"]

During subsequent interactions, a pre-execution middleware hook queries the memory graph for all entities mentioned in the incoming user prompt, injecting relevant factual triples into the system context before generation starts.


Memory Consolidation and Conflict Resolution

A core failure mode in agent memory is contradictory information. If a user states in Session 1: “I live in Seattle”, and in Session 4: “I just moved to Austin”, a naive vector search retrieves both passages, confusing the model.

Consolidation Workflow

flowchart TD
    Input["Incoming Message: I moved to Austin today."] --> Extract["Extract Atomic Proposition<br>Proposition: { subject: User, predicate: lives_in, object: Austin, timestamp: T2 }"]
    Extract --> Query["Query Existing Graph<br>Existing: { subject: User, predicate: lives_in, object: Seattle, timestamp: T1 }"]
    Query --> Check["Temporal Conflict Check"]
    Check --> Functional["If predicate is functional (single-value):<br>1. Mark T1 edge as superseded: { valid_until: T2 }<br>2. Insert T2 edge: { valid_from: T2, valid_until: null }"]
    Check --> Multi["If predicate is multi-value (e.g. likes_language):<br>1. Append new relation without invalidating previous relations"]

Mathematical Modeling of Memory Decay

Human memory models (such as the Ebbinghaus forgetting curve) are used to prioritize recent and frequently reinforced facts over stale historical interactions.

The retrievable score $S(f)$ of a memory fact $f$ is calculated as a composite function of semantic relevance, recency, and access frequency:

$$S(f) = \alpha \cdot \text{Sim}(q, f) + \beta \cdot e^{-\lambda (t - t_0)} + \gamma \cdot \log(1 + C)$$

Where:

  • $\text{Sim}(q, f)$ is the cosine similarity between the current query embedding $q$ and the memory embedding $f$.
  • $e^{-\lambda (t - t_0)}$ is the exponential decay function based on elapsed time $(t - t_0)$ and decay rate parameter $\lambda$.
  • $C$ is the access count (number of times this fact has been retrieved and verified).
  • $\alpha, \beta, \gamma$ are weighting hyperparameters balancing relevance, recency, and reinforcement.

Multi-Tenant Security & State Isolation

In enterprise environments, agent memory stores must guarantee strict tenant isolation:

  1. User and Session Scoping: Every episodic log and semantic memory node must include cryptographic tenant and user IDs (tenant_id, user_id, role_id).
  2. Metadata Filtering at the Index Level: Retrieval queries must enforce mandatory pre-filtering on tenant IDs before vector similarity or graph traversal occurs, preventing cross-account data leaks.
  3. Data Erasure Compliance (GDPR/CCPA): Memory platforms must support targeted deletion of all episodic and semantic memory triples associated with a specific user identifier upon request.