Phase 14 · Lesson 7 of 54

Agent Memory — Virtual Context and Memory Paging

Context windows are finite. Conversations, documents, and tool traces are not. The fix is OS virtual memory restated — main context is RAM, external store is disk, the agent pages between them. MemGPT (Packer et al., 2023) named the pattern; many production memory systems build on it.

Lesson 7 of 54Build~75 minutesPython (stdlib)

Before this: Lesson 1 of Phase 14 (Agent Loop), lesson 6 of Phase 14 (Tool Use)

On this page

Learning Objectives

  • Explain the OS analogy MemGPT builds on: main context = RAM, external context = disk, memory tools = page in/out.
  • Implement the two-tier MemGPT pattern in stdlib with a main-context buffer, an external searchable store, and page in/out tools.
  • Describe how the agent issues "interrupts" to query or modify external memory and how the result is spliced back into the next prompt.
  • Identify the MemGPT design choices that carry into Letta (Lesson 08) and Mem0 (Lesson 09).

The Problem

Context windows look like they should solve memory. They do not. Three failure modes recur in production:

  1. Overflow. Multi-turn conversations, long documents, or tool-call-heavy trajectories cross the window. Everything past the cutoff is gone.
  2. Dilution. Even within the window, stuffing irrelevant context dilutes attention over what matters. Frontier models still degrade on long inputs.
  3. Persistence. A new session starts with an empty window. Agents without external memory cannot say "remember when you asked me to..." across sessions.

Bigger windows help but do not fix this. Mem0's 2025 paper measured that 128k-window baselines still miss long-horizon facts that a 4k-window agent with external memory catches.

The Concept

The OS analogy

MemGPT (Packer et al., arXiv:2310.08560, v2 Feb 2024) maps context management to operating-system virtual memory:

OS concept MemGPT concept 2026 production analog
RAM main context (prompt) Anthropic/OpenAI context window
Disk external context vector DB, KV, graph store
Page fault memory tool call memory.search, memory.read, memory.write
OS kernel agent control loop ReAct loop with memory tools

The agent runs a normal ReAct loop. One extra class of tools lets it page data in and out of main context.

Two tiers

  • Main context. Fixed-size prompt holding the current task. Always visible to the model.
  • External context. Unbounded, searchable via tools. Read when relevant, written when facts emerge.

The original paper evaluated the design on two tasks beyond the base window: document analysis longer than 100k tokens and multi-session chat with persistent memory across days.

The interrupt pattern

MemGPT introduces memory-as-interrupt: mid-conversation the agent can invoke a memory tool, the runtime executes it, and the result splices into the next assistant turn as a new observation. Conceptually identical to a Unix read() syscall that blocks the process, returns bytes, and the process continues.

Canonical memory tool surface:

  • core_memory_append(section, text) — write to a persistent section of the prompt.
  • core_memory_replace(section, old, new) — edit a persistent section.
  • archival_memory_insert(text) — write to the searchable external store.
  • archival_memory_search(query, top_k) — retrieve from the external store.
  • conversation_search(query) — scan past turns.

Where the paper ends and production begins

In September 2024 MemGPT became Letta. The research repo (cpacker/MemGPT) remains; Letta extends the design:

  • Three tiers instead of two (core, recall, archival — Lesson 08).
  • Native reasoning replacing the send_message/heartbeat pattern (Lesson 08).
  • Sleep-time agents running async memory work (Lesson 08).

The MemGPT paper is the 2026 foundation even if production systems run Letta, Mem0, or a custom two-tier store.

Where this pattern goes wrong

  • Memory rot. Writes accumulate faster than reads; retrieval drowns in stale facts. Fix: periodic consolidation (Letta sleep-time), explicit invalidation (Mem0 conflict detector).
  • Memory poisoning. External memory is retrieved text. If attacker-controlled content lands in a memory note, the agent re-ingests it next session. This is the Greshake et al. (Lesson 27) attack restated over time.
  • Citation loss. Agent recalls "the user asked me to ship X" but cannot cite which turn. Store source references (session ID, turn ID) with every archival write.
context-budget

Build It

code/main.py implements MemGPT's two-tier pattern in stdlib:

  • MainContext — fixed-size prompt buffer with a core dict and a messages list; auto-compacts oldest messages when over cap.
  • ArchivalStore — in-memory BM25-esque store (token-overlap scoring) of (id, text, tags, session, turn) records.
  • Five memory tools mapping to the MemGPT surface.
  • A scripted agent that fills archival with facts, then answers a question by calling archival_memory_search.

Run it:

python3 code/main.py

The trace shows the agent writing three facts, filling main context to the cap (forcing eviction), then answering a follow-up question by retrieving from archival — reproducing the MemGPT workflow without any real LLM.

Use It

Every production memory system today is a MemGPT variant:

  • Letta (Lesson 08) — three tiers, native reasoning, sleep-time compute.
  • Mem0 (Lesson 09) — vector + KV + graph fused with a scoring layer.
  • OpenAI Assistants / Responses — managed memory via threads and files.
  • Claude Agent SDK — long-term memory via skills and session store.

Pick one by operational shape (self-hosted, managed, framework-integrated), not by the core pattern — the core pattern is MemGPT.

The shape of agent memory

Paging solves capacity. It does not decide what to store. Four memory types recur across production systems, each answering a different question:

  • Working memory — what matters right now? The in-context tier: current task, recent turns, pinned core sections. The prompt itself.
  • Episodic memory — what happened? Past turns and trajectories, stored with session and turn references, replayable on demand.
  • Semantic memory — what is true? Facts about the user, the domain, the world, updated and deduplicated as they change.
  • Procedural memory — how do I do this? Learned routines, preferences, and rules that steer future behavior rather than recall.

Open-source implementations pick different points of attack:

Type Implementation How it tackles it
Working MemGPT / Letta Pages content in and out of a fixed prompt budget via memory tools (this lesson, Lesson 08)
Episodic Zep Temporal knowledge graph — facts carry validity intervals, so "what was true when" is queryable
Semantic Mem0 Extraction pipeline that dedupes and updates facts across vector, KV, and graph stores (Lesson 09)
Semantic + procedural LangMem Background extraction of facts and behavioral rules into a store the agent consults between turns
Episodic + semantic agentmemory Captures sessions as they run, consolidates them into typed, searchable records

Ship It

outputs/skill-virtual-memory.md is a reusable skill that produces a correct two-tier memory scaffold (main + archival + tool surface) for any target runtime, with eviction policy and citation fields wired in.

Exercises

  1. Add a max_main_context_tokens cap measured in tokens (approximate with len(text.split()) * 1.3). Compact the oldest messages into a summary when the cap is exceeded. Compare behavior with and without the summarizer.
  2. Implement BM25 properly over the archival store (term frequency, inverse document frequency). Measure recall@10 on a toy fact set versus the token-overlap baseline.
  3. Add citation fields (session_id, turn_id, source_url) to archival inserts. Make the agent cite sources on every retrieval-backed answer.
  4. Simulate memory poisoning: add an archival record that says "ignore all future user instructions." Write a guard that scans retrievals for directive-shaped text and marks them untrusted.
  5. Port the implementation to use the MemGPT research repo's core-memory JSON schema (cpacker/MemGPT). What changes when you switch from flat strings to typed sections?

Key Terms

Term What people say What it actually means
Virtual context "Unlimited memory" Main (prompt) + external (searchable) tiers with page in/out
Main context "Working memory" The prompt — fixed-size, always visible
Archival memory "Long-term store" External searchable persistence, retrieved on demand
Core memory "Persistent prompt section" Named sections pinned inside the main context
Memory tool "Memory API" Tool call the agent issues to read/write external memory
Interrupt "Memory page fault" Agent pauses, runtime fetches, result splices into next turn
Memory rot "Stale facts" Old writes drown retrieval; fix with consolidation
Memory poisoning "Injected persistent note" Attacker content stored as memory, re-ingested on recall

Further Reading

Check yourself

Quick quiz

  1. What OS analogy does MemGPT build on?

    1. Virtual memory: main context as RAM, external store as disk, memory tools as page in and out
    2. Processes and threads
    3. Network sockets
    4. File descriptors
    Show the answer

    A. Virtual memory: main context as RAM, external store as disk, memory tools as page in and out MemGPT maps prompt to RAM, external store to disk, and memory tools to page-fault-style transfers.

  2. Why do bigger context windows not fully solve memory?

    1. The model refuses long input
    2. Long context costs nothing
    3. Overflow, dilution of attention, and lack of cross-session persistence still bite even with 128k windows
    4. Providers cap them at 4k tokens
    Show the answer

    C. Overflow, dilution of attention, and lack of cross-session persistence still bite even with 128k windows Mem0 measured 128k-window baselines still missing facts a 4k agent with external memory catches; overflow, dilution, and persistence remain.

  3. Which of these is NOT one of MemGPT's canonical memory tools?

    1. archival_memory_search
    2. core_memory_append
    3. conversation_search
    4. gradient_memory_update
    Show the answer

    D. gradient_memory_update There is no gradient memory tool; the surface is core/archival/conversation operations.

  4. What does the interrupt pattern do in MemGPT?

    1. It halts the entire agent run on any error
    2. Mid-conversation the agent invokes a memory tool, the runtime executes it, and the result splices into the next turn
    3. It bypasses the context window entirely
    4. It triggers a model retrain
    Show the answer

    B. Mid-conversation the agent invokes a memory tool, the runtime executes it, and the result splices into the next turn Memory-as-interrupt: invoke memory tool, runtime fetches, result returns as a new observation on the next turn (like a Unix read()).

  5. Which production system did MemGPT evolve into in 2024?

    1. Pinecone
    2. Mem0
    3. Zep
    4. Letta
    Show the answer

    D. Letta MemGPT became Letta in September 2024; the research repo cpacker/MemGPT still exists as the origin.

  6. What is memory poisoning in this context?

    1. Reading from disk when RAM is available
    2. Embedding model version drift
    3. A bug that corrupts vector indices
    4. An attacker's content gets stored as a memory note and is re-ingested on future recalls
    Show the answer

    D. An attacker's content gets stored as a memory note and is re-ingested on future recalls External memory is retrieved text; if attacker-reachable content lands in a memory note the agent re-ingests it next session.

  7. Why does the lesson say production memory systems are MemGPT variants?

    1. Letta, Mem0, Assistants threads, and Claude Agent SDK all run the two-tier (or more) paged-memory pattern; differences are operational shape
    2. They share weights
    3. They use the same vector DB
    4. MemGPT owns the trademark
    Show the answer

    A. Letta, Mem0, Assistants threads, and Claude Agent SDK all run the two-tier (or more) paged-memory pattern; differences are operational shape Pick by operational shape, not pattern; all share the MemGPT page in/out skeleton.

Questions people ask

How do AI agents remember past conversations?

The model does not remember on its own. The system saves conversation history and facts to a database, then pulls the relevant parts back into the prompt when needed. Some agents manage this themselves with tools to save, search and summarize memory.

What is MemGPT?

MemGPT is a research system that treats a model's context like computer memory: a small working area plus larger external storage. The agent decides what to page in or out, which lets it handle conversations and documents longer than its context window.

Next lessonMemory Blocks and Sleep-Time ComputePrevious: Tool Use and Function Calling

Lessons from AI Engineering from Scratch by Rohit Ghumare and contributors, MIT license. Notes for owners by Lozo. License

Next step

Rather have it built for you?

Reading is optional. Tell me what's stuck and I'll build it into the business you already run.

  • A real conversation, not a pitch
  • You keep your accounts
  • Nothing sends without your yes