The Missing Memory Hierarchy: Demand Paging for LLM Context Windows
Organizations: University of British Columbia Canada · Georgia Institute of Technology USA
Abstract
The context window of a large language model is not memory. It is L1 cache: a small, fast, expensive resource that the field treats as the entire memory system. There is no L2, no virtual memory, no paging. Every tool definition, every system prompt, and every stale tool result occupies context for the lifetime of the session. The result is measurable: across 857 production sessions and 4.45 million effective input tokens, 21.8% is structural waste. We present Pichay, a demand paging system for LLM context windows. Implemented as a transparent proxy between client and inference API, Pichay interposes on the message stream to evict stale content, detect page faults when the model re-requests evicted material, and pin working-set pages identified by fault history. In offline replay across 1.4 million simulated evictions, the fault rate is 0.0254%. In live production deployment over 681turns, the system reduces context consumption by up to 93% (5,038KB to 339KB); under extreme sustained pressure, the system remains operational but exhibits the expected thrashing pathology, with repeated fault-in of evicted content. The key observation is that the problems the field faces, such as context limits, attention degradation, cost scaling, lost state across sessions, are virtual memory problems wearing different clothes. The solutions exist: working set theory (Denning, 1968), demand paging, fault-driven replacement policies, and memory hierarchies with multiple eviction-managed levels. We describe the architecture of a full memory hierarchy for LLM systems (L1 through persistent storage), report on the first three levels deployed in production use (L1 eviction, L2 fault-driven pinning, L3 model-initiated conversation compaction), and identify cross-session memory as the remaining frontier.
Figures & tables
| OS Concept | Context Window Equivalent |
|---|---|
| Physical memory | Context window (200K tokens) |
| Virtual memory | Persistent state (disk, databases) |
| Page table | Retrieval handles (UUIDs, anchors) |
| Page fault | Model re-requests evicted content |
| MMU | Proxy layer between client and API |
| Working set | Currently relevant context subset |
| Level | Contents | Eviction | Fault |
|---|---|---|---|
| L1 | Generation window | — | — |
| L2 | Working set (pinned) | Pressure-based | Re-read |
| L3 | Session history | Age-based | Summary expansion |
| L4 | Cross-session memory | LRU / relevance | Retrieval query |
| Storage | Full corpus | None | Search + ingest |
| Category | Bytes | % | Mechanism |
|---|---|---|---|
| Dead tool output | 6,468,360 | 26.5 | Stale results never re-referenced |
| Tool definition stubs | 4,924,950 | 20.2 | Schemas for unused tools |
| Static re-send | 2,680,794 | 11.0 | Unchanged system prompt content |
| Skill triplication | 700,582 | 2.9 | Same skill listed |
| Total addressable | 14,774,686 | 60.5 |
| Total simulated evictions | 1,393,000 |
|---|---|
| Page faults detected | 354 |
| Fault rate | 0.0254% |
| Content evicted | 8.49 GB |
| Metric | Baseline | Trimmed | Compact+Trim |
|---|---|---|---|
| API calls | 3 | 4 | 3 |
| Effective input tokens | 114,222 | 88,421 | 71,816 |
| Cache read tokens | 79,712 | 38,639 | 32,228 |
| Task completed correctly | Yes | Yes | Yes |
| Token reduction | — | 22.6% | 37.1% |
| Intervention | Tokens Saved | % of Input |
|---|---|---|
| Tool stub trimming | 487.5M | 11.0% |
| Skill deduplication | 95.8M | 2.2% |
| Static re-send | 387.0M | 8.7% |
| Total addressable | 970.4M | 21.8% |
| Context free before compaction | 7% |
| Context free after compaction | 43% |
| Total evictions | 15 |
| Garbage collected | 11 |
| Read evictions (pageable) | 4 |
| Page faults | 1 |
| Fault rate (Read only) | 25% |
| Turns | 681 |
|---|---|
| Evictions (total) | 680 |
| Garbage collected | 74 |
| Read evictions (pageable) | 606 |
| Page faults | 659 |
| Fault rate (total evictions) | 97% (659/680) |
| Peak compression | 5,038KB 339KB |
| Metric | Baseline | Treatment |
|---|---|---|
| Judges preferring | 15 (28%) | 20 (37%) |
| Ties | 19 (35%) | |
| Mean correctness | 3.89 | 3.74 |
| Mean completeness | 3.59 | 3.59 |
| Mean coherence | 3.74 | 3.69 |
| Max score | 0.15 | |
LLM Context Management
Large language model context management studies how to select, compress, retrieve, and organize information within bounded model inputs.
See all 248 papers