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. 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). Context windows look like they should solve memory. They do not. Three failure modes recur in production: Overflow. Multi-turn conversations, long documents, or tool-call-heavy trajectories cross the window. Everything past the cutoff is gone. Dilution. Even within the window, stuffing irrelevant context dilutes attention over what matters. Frontier models still degrade on long inputs. 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…
Agent Memory — Virtual Context and Memory Paging: Context windows are finite. Conversations, documents, and tool traces are not. The fix is OS virtual memory…
This free lesson is part of the AI Engineering from Scratch curriculum. Read the full explanation, run the lesson code, and verify the result in the interactive reader or from the repository source.
Browse the complete course catalog or open this lesson on GitHub.