Skip to content

The Memory Hierarchy

The context window's finite size is not a temporary engineering limitation that will disappear as models scale. It is a new instance of a very old problem, one that operating systems solved decades ago.

Why storage is always a pyramid

Fast storage is expensive. Cheap storage is slow. This is not a historical accident; it is a fundamental constraint rooted in physics and economics, and it has held across every technology generation from magnetic cores to DRAM to SSDs to NVMe.

The numbers make the tradeoff vivid. CPU registers hold data in under a nanosecond — but there are only a handful of them, measured in kilobytes; one extra byte and the register file is full. L1 cache stretches to a few megabytes at two or three nanoseconds. RAM sits at a hundred nanoseconds but buys you tens of gigabytes. A disk takes milliseconds, but terabytes are suddenly affordable. The pattern has held across every storage technology from magnetic cores to NVMe: each layer down is an order of magnitude slower and an order of magnitude larger. That ratio has never broken.

Computer architects learned to exploit a statistical property of programs to make this hierarchy work: locality. Programs tend to access the same data repeatedly in short windows of time (temporal locality), and they tend to access data that is physically near data they just accessed (spatial locality). If you keep the recently-used, frequently-used data in the fast layer and push everything else to the slow layer, average access time approaches the fast tier's speed.

Virtual memory is the elegant abstraction built on top of this observation. The operating system presents each process with a continuous, large address space — a fiction. Behind the scenes, it maps the most actively used portions of that address space to physical RAM and stores the rest on disk. When a process reaches for a page that is not in RAM, the hardware fires a page fault: execution pauses, the OS retrieves the needed page from disk, evicts the least-recently-used page to make room, and resumes execution. The process never sees any of this. The mechanism is invisible — transparent in the systems sense.

The agent memory pyramid

The same structure maps directly onto agent systems:

OS Memory Layer Agent System Equivalent Who manages it
CPU cache (KV cache) KV cache — inference acceleration Inference infrastructure, automatic
RAM Context window Harness — the core management object
Disk Vector database / external storage Harness — explicit management
Network storage External APIs / knowledge bases Agent — on-demand retrieval

The core problem is identical: simulate unlimited slow storage access using a finite fast store. The context window is RAM — limited in size, but directly accessible during inference. Vector databases are the disk — large, but every retrieval is an explicit operation with measurable latency.

Virtual memory in agent systems

UC Berkeley's MemGPT (2023) made this mapping explicit as a system design. Main context is RAM: whatever the LLM can access directly during inference. Archival memory is disk: an external store for history, documents, and prior reasoning that does not fit in context. When the agent needs something from archival memory, it generates a function call to retrieve it — the agent's version of a page fault. When context approaches its limit, summaries are compressed and written back to external storage — the agent's version of page eviction.

Structurally, this is the same design pattern: finite fast storage simulating access to a larger slow store. The underlying mechanism is recognizably analogous.

But there is one dimension where the analogy inverts, and it is worth being precise about it. An OS page fault is completely transparent to the running process. The hardware detects the missing page, the OS handles the swap, and execution resumes — the process never knew anything happened. MemGPT's "page fault" is the opposite: it is an active function call that the LLM itself generates, after the LLM decides that it needs to retrieve something and chooses what to retrieve. The LLM is not a passive recipient of memory management. It is the system making the decisions. Resource management transparency is inverted, not just translated.

This inversion is not a design flaw — it is a structural consequence of building on a language model. The LLM is the thing best positioned to judge "what information do I need right now?" No external system can make that semantic judgment on its behalf. The design adapts the mechanism while preserving the underlying purpose.

Anthropic's Compaction API takes the MemGPT approach and descends it into production infrastructure. When context nears its limit, the API server automatically summarizes and compresses the conversation, produces a compact block, and continues from the compressed state. Application code does not need to implement the paging logic manually. This follows a familiar OS trajectory: a research mechanism that worked gets embedded in infrastructure so that every application benefits without reimplementing it.

The breakpoint: wrong context is worse than missing context

OS virtual memory has one deeply reliable property: it only affects latency, never correctness. The data on disk is an exact copy of what was in RAM. Retrieving a page introduces a delay — sometimes a significant one — but the retrieved data is bit-for-bit accurate. A process running after a page fault behaves identically to one that never triggered it. Correctness is preserved.

Agent memory retrieval breaks this guarantee.

If a vector database retrieval returns context that is semantically related but factually wrong, the effect is not just a slowdown. The wrong context actively degrades reasoning quality. Chroma's 2025 research across eighteen frontier models — including GPT-4.1, Claude 4, Gemini 2.5, and Qwen3 — measured this directly: distractor interference, where a retrieved passage is topically relevant but incorrect, harms model performance more than providing no context at all. The model cannot easily reject a plausible-sounding but wrong piece of context; it gets incorporated into the reasoning.

An OS memory manager only has to track whether a page is present. An agent memory manager has a second dimension: whether the retrieved content is accurate and appropriate. This is a new engineering requirement with no direct OS equivalent.

Longer windows do not eliminate management

A natural response is to wait. If context windows keep growing — and they are, from four thousand tokens to one million — won't management become unnecessary?

RAM capacity has grown from megabytes to hundreds of gigabytes over the history of computing. Virtual memory did not disappear. It became more sophisticated: larger page tables, NUMA-aware allocation, prefetching algorithms, huge pages to reduce TLB pressure. The management layer did not shrink because the resource grew; it expanded to manage the new scale.

The same dynamic applies here. Chroma's data shows performance degradation as input length increases, consistently across models. The cause is not an insufficient window size. It is that attention is scarce: a fixed computational budget gets spread over more tokens, diluting the signal from any individual piece of information. A longer window holds more, but it also dilutes more. The signal-to-noise problem does not go away; it scales with the resource.

Management complexity grows with system complexity, not in inverse proportion to raw capacity.


Memory management answers "what does the LLM get to work with." A different question is who gets to use the LLM at all, and when. Multiple agents sharing a single inference backend need arbitration — and that is the job OS engineers have always called scheduling.


Further reading

  • Packer, C. et al. (2023). MemGPT: Towards LLMs as Operating Systems. arXiv:2310.08560.
  • Chroma. (2025). Context Rot: How Long Contexts Degrade LLM Performance. research.trychroma.com.
  • Anthropic. (2026). Context Management. docs.anthropic.com. (Compaction API)