Virtual Context Management

The agent as its own memory manager: page facts in and out like an OS.

The agent treats its finite context window like RAM and an external store like disk, and pages information between the tiers under its own control — deciding what stays resident, what is evicted to long-term storage, and what is recalled on demand — so it can operate over histories far larger than the window.

In practiceA legal-research agent managing a 200-page contract pages out early clause summaries to an external store and pages them back in only when a downstream question references that section.

Level
04 · Production
Subdomain
Memory Architecture
Also known as
MemGPT, OS-Style Memory Paging, Tiered Context Management, Self-Editing Memory, Virtual Context, Context-Aware Agent
Trade-off
Effectively unbounded memory under a fixed window is gained against extra inference calls to manage the paging and the risk of evicting something the agent later needs.
Native in
Letta, Mem0, Zep, LlamaIndex
The mechanism

How it works.

The context window is divided into fixed parts, and a queue manager watches how full it is. The model itself moves information out and back in, through function calls.

1 · Evict, when the window fills

  1. Warning thresholdFor example at 70 % of the window
  2. The model savesIt writes what it will need to a store
  3. Flush thresholdFor example at 100 %
  4. Oldest messages leaveA running summary takes their place

2 · Recall, on demand

  1. Something old is neededIt is no longer in the window
  2. The model searchesA function call against a store
  3. Results in pagesSo the answer cannot overflow the window
  4. Back in the windowThe model continues with it
Working context
A fixed-size block of text in the window. The model edits it through function calls: facts about the user and the task.
Recall storage
Every message, kept indefinitely. Evicted messages go here and can be searched.
Archival storage
A read and write store for texts of any length: documents, long notes.
Walk it through

A real run, step by step.

TaskA companion agent in a conversation that runs for months, far beyond one context window.
Illustrative values. The thresholds are the examples the MemGPT paper gives.
MomentWindowWhat happens
Turn 180 · ordinary chat68 %Nothing. Everything is still in view.
Turn 181 · the user mentions a birthday71 %The warning is inserted. The model writes “daughter Mia, birthday 4 March” to the working context.
Turn 240100 %Flush. The oldest messages leave the window, the summary is updated, the originals go to recall storage.
Turn 300 · “Which venue did we talk about in spring?”55 %The model searches recall storage, reads two pages of results and answers.
What this shows: the birthday survives because the model saved it when warned; the venue survives only in recall storage, and the model has to think of searching there. Both moves cost an extra model call.
In code

The pattern in code.

Plain Pythonpython
def step(window, event):
    window.queue.append(event)
    if window.tokens > 0.7 * LIMIT and not window.warned:
        window.queue.append(system("memory pressure: save what you will need"))
        window.warned = True
    if window.tokens > LIMIT:
        evicted = window.queue.pop_oldest(tokens=0.5 * LIMIT)
        recall_storage.add(evicted)  # out of view, not lost
        window.summary = summarize(window.summary, evicted)
        window.warned = False
    return model(window, tools=[edit_working_context, write_archive,
                                search_archive, search_recall])
Fit

Use it, or not.

When to reach for it

  • Conversations or tasks outgrow the context window.
  • Salient facts must survive across very long horizons.
  • Which information is salient changes over the run.

When it backfires

  • The task fits comfortably in-context.
  • A plain truncation or summary buffer suffices.
  • The extra LLM calls to manage paging are not justified.
Pitfalls

Two ways this pattern will hurt you.

The agent evicts something it later needs

Self-editing memory means the agent decides what to drop. Under token pressure it can page out a fact that becomes critical several steps later, then confabulate a replacement.

Countermeasure

Pin invariant facts (task goal, constraints, identity) to a non-evictable region and only let the pager touch the discretionary tier.

Re-summarizing on every page-out drifts from the source

Each eviction compresses already-compressed content. After many cycles the paged-back summary is a summary of a summary, and details mutate or invert relative to what was originally said.

Countermeasure

Page the verbatim content out to the store and summarize only the resident working view; recall the original on demand rather than re-compressing repeatedly.

Framework support

Where Virtual Context Management is native.

LettaMemGPT lineage; OS-style tiered memory.Native
Mem0ServiceManaged memory layer: fact extraction, summarization, eviction.Native
ZepServiceLong-term memory store with progressive summarization.Native
LangChainLegacy buffer / summary / entity memory classes (deprecated).Adaptable
LlamaIndexMemory class: short-term buffer flushed into long-term memory blocks.Native
LangGraphAs custom state plus summarization.Adaptable
Sources

Where this pattern comes from.

  1. MemGPT: Towards LLMs as Operating SystemsIn the source list

Search

Search patterns, frameworks, and pages.