Skip to content

MemGPT: Towards LLMs as Operating Systems

Skip →Context-Folding & AgentFold
main track #6corefundamentalpaperrubric 10/122023 · ~35 min

Charles Packer, Sarah Wooders, Kevin Lin, … Joseph E. Gonzalez

Status

TL;DR. MemGPT treats the context window like an operating system treats RAM. The model sees a small main context: fixed instructions, a short editable working context, and a FIFO queue of recent messages. Everything else lives in external context (recall and archival storage). The model moves information between the two with function calls, and a queue manager warns it and evicts old messages when the window fills.

Why it matters now. MemGPT is the reference design for “the LLM manages its own memory”. Later systems (A-MEM, Mem0, ReSum, context folding, Librarian Agents) all choose a different answer to the same question: what stays in the window, what is moved out, and who decides. For an IR researcher, the key point is that MemGPT’s external context is an index plus a search tool. The method stands or falls with how well the model queries that index.

MemGPT: a fixed context window plus external storage that the model pages through with function calls Top left: the main context is the LLM's real window. It holds read-only system instructions, a small working context the model edits through function calls, and a FIFO queue of recent messages whose first entry is a recursive summary of evicted messages. A queue manager warns the model at a memory-pressure threshold (for example 70 percent of the window) and, at the flush threshold, evicts about half the window to recall storage and rewrites the recursive summary. Top right: a function executor runs the calls the model emits; a heartbeat flag asks for another inference step so calls can be chained. Bottom: external context lies outside the window: recall storage keeps every message and is searched page by page; archival storage is a general read/write store with vector search. Virtual context = a fixed window + storage paged in by function calls Main context (the LLM's real, fixed window) System instructions read-only Working context via fn calls FIFO queue [0] = summary then messages Queue manager (paper's example thresholds) warning (~70%) → system message: memory pressure flush (100%) → evict ~50%, rewrite recursive summary Function executor LLM output = function calls heartbeat=true → infer again no heartbeat → yield to user Events trigger inference user message, system alert, timed event External context (outside the window; reachable only through function calls) Recall storage: every message seen searched one page of results at a time Archival storage: read/write store e.g. documents; vector search evict results paged in insert / search
Original sketch. Only main context is inside the window; everything else is reached through function calls. Thresholds are the paper's examples.

Problem. A fixed-size window cannot hold a months-long conversation or a large document collection. Simply truncating loses old facts, and one summary written in advance loses whatever the summarizer did not think was important.

Mechanism (paper §2).

  • Main context = system instructions (read-only) + working context (a small read/write block the model can change only through function calls) + a FIFO queue of messages, function results and system alerts. The first queue entry is a recursive summary of messages already evicted.
  • External context = recall storage (a database of every message seen) + archival storage (a general read/write store, for example documents, with vector search). Search results are paginated, so one call returns one page.
  • Queue manager. When prompt tokens pass a warning threshold (the paper’s example is 70% of the window), it inserts a “memory pressure” system message so the model can save important facts. At the flush threshold (example: 100%) it evicts a share of the queue (example: 50% of the window) and writes a new recursive summary from the old summary and the evicted messages:
sk+1=Summ(sk,  mevicted(k)),mevicted(k)→recall storage.s_{k+1} = \mathrm{Summ}\big(s_k,\; m_{\text{evicted}}^{(k)}\big), \qquad m_{\text{evicted}}^{(k)} \rightarrow \text{recall storage}.

Evicted messages are not deleted. They move to recall storage, where they can still be searched.

  • Control flow. Events (a user message, a system alert, a timed event) trigger inference. The model’s output is parsed as function calls. If a call includes request_heartbeat=true, the model runs again right away, so it can chain calls (search, read a page, search again). Without the flag, control returns to the user.

Worked trace (illustrative, not from the paper). In session 6 the user asks: “What was the name of the hiking trail I mentioned in our second chat?”

  1. The user message arrives and triggers inference. The trail is not in the working context, and the recursive summary only says “talked about hiking”.
  2. The model calls conversation_search("hiking trail") with a heartbeat. Page 1 of hits enters the FIFO queue.
  3. The hits are about a different hike, so the model searches again with “second chat trail name” and a heartbeat, and page 2 contains the message.
  4. The model writes the trail name into the working context (a function call) so it will not need to search again, then replies without a heartbeat and yields.

Steps 2–3 are just query reformulation over an index of raw messages. The OS framing adds the control loop: when to search, how far to page, and what to keep in the window.

Before (fixed context)MemGPT
What is in the windowThe most recent tokens, or one summaryInstructions + working context + queue headed by a recursive summary
Old informationTruncated or summarized onceMoved to recall storage and still searchable
Who decidesFixed ruleThe model, through function calls, with warnings from the queue manager
What did not changeThe model’s real window size; nothing here enlarges the attention span
EvidenceDMR (Table 2) and nested-KV results below

All numbers are from the paper (arXiv v2, February 2024); the context sizes in its Table 1 are dated January 2024.

  • Deep memory retrieval (DMR) on Multi-Session Chat (Table 2). The question is about earlier sessions. Baselines receive a lossy summary of sessions 1–5. Accuracy / ROUGE-L recall:
    • GPT-3.5 Turbo: 38.7% / 0.394 → with MemGPT 66.9% / 0.629
    • GPT-4: 32.1% / 0.296 → with MemGPT 92.5% / 0.814
    • GPT-4 Turbo: 35.3% / 0.359 → with MemGPT 93.4% / 0.827
  • Conversation openers (Table 3): openers are scored by similarity to the gold persona labels (SIM-1, SIM-3) and to a human-written opener (SIM-H). On SIM-1, MemGPT with GPT-4 scores 0.868 against 0.800 for the human opener. This measures persona use, not factual recall.
  • Document QA (NQ-Open, 50 questions, ada-002 retriever over a late-2018 Wikipedia dump). Fixed-context baselines are limited by how many retrieved documents fit in the window. MemGPT can page through more results, but the authors note that MemGPT “will often stop paging before exhausting the retriever database”. Results are reported in a figure, so I don’t quote exact numbers here.
  • Nested key-value retrieval (140 UUID pairs, about 8k tokens, nesting levels 0–4). Plain GPT-3.5 drops to 0% at one nesting level, and GPT-4 and GPT-4 Turbo reach 0% by level 3. MemGPT with GPT-4 is unaffected by nesting depth; MemGPT with GPT-4 Turbo or GPT-3.5 degrades from level 2.
  • The baselines are weak for an IR reader. DMR compares paging over raw messages against a single summary. There is no single-shot retrieval baseline over the same message store with the same reader and token budget, so the evidence does not separate “keeping raw evidence” from “the OS control loop”. The Raw-RAG paper supplies that kind of baseline for later memory systems.
  • Early stopping. The document-QA result shows that the controller often stops paging before it has seen everything. This is a stopping-rule problem, close to deciding retrieval depth in IR.
  • Summary drift. The recursive summary is rewritten on every flush. A fact can be lost from the summary even though it is still in recall storage, and it is then found only if the model thinks to search.
  • Dependence on function calling. GPT-3.5 results are much weaker, and the paper attributes this to function-calling quality. The method is only as good as its controller.
  • Open question. Can the paging policy (when to search, what to store in the working context, when to stop) be trained with RL rather than prompted? ReSum, Context-Folding and Librarian Agents train related policies.

Questions to think through

These questions have no single right answer. Write or say your answer before you open the hints. Mark a question once you could defend your answer in a reading group.

  1. Derive / compute

    Take a MemGPT agent on an 8,192-token model. System instructions use 1,200 tokens and the working context 800, so the FIFO queue gets the rest. Use the paper's example thresholds: a memory-pressure warning at 70% of the window and a flush at 100% that evicts 50% of the window. Messages average 150 tokens and the recursive summary stays near 300 tokens. How many messages fit before the warning and before the first flush, how many are evicted per flush, and how many flushes happen over a 600-message relationship? If each rewrite of the recursive summary independently drops a given fact with probability 2%, what is the chance a fact from message 1 survives in the summary alone, and why does that not equal the chance MemGPT can still answer a question about it?

    What a strong answer covers
    • Warning at 0.7 × 8,192 ≈ 5,734 tokens; minus 2,000 fixed and about 300 for the summary leaves about 3,434 tokens, roughly 22 messages before the warning. The flush at 8,192 tokens comes at about 5,892 / 150 ≈ 39 messages.
    • Each flush frees about 4,096 tokens, roughly 27 messages, so after the first flush the queue refills every ~27 messages: about (600 − 39) / 27 + 1 ≈ 21–22 flushes.
    • A message-1 fact goes through about 21 summary rewrites, so its survival in the summary alone is about 0.98^21 ≈ 0.65.
    • The evicted message itself stays in recall storage, so the fact is recoverable if the model decides to search and the search finds it. The real failure modes are not searching, querying with the wrong words, or stopping paging too early.
    • The working context gives a second, explicit place to keep durable facts, written by function calls rather than by the summarizer.
  2. Mechanism

    On the deep-memory-retrieval (DMR) task, the GPT-4 baseline that reads a summary of earlier sessions answers 32.1% correctly while GPT-4 with MemGPT reaches 92.5%. With GPT-3.5 Turbo the gain is smaller (38.7% to 66.9%). Walk through, step by step, what MemGPT does when the user asks about a detail from session 2, and explain which part of the mechanism produces the gain and why the weaker model benefits less.

    What a strong answer covers
    • The baseline gets one lossy, query-independent summary; if the detail was dropped when the summary was written, no amount of reasoning can recover it.
    • MemGPT sees the question as an event, emits a conversation_search call over recall storage with request_heartbeat=true, reads the first page of hits in its queue, and either pages further, refines the query, or answers.
    • The gain comes from query-time access to raw messages: the lookup is conditioned on the question, so nothing has to be predicted as important in advance.
    • Every step depends on the LLM producing correct function calls and judging when it has enough; the paper attributes weaker GPT-3.5 results to weaker function calling.
    • So MemGPT turns memory into a tool-use problem, and the quality of the controller (query writing, stopping decision) bounds the quality of the memory.
  3. Critique

    The DMR baselines get a lossy summary of past sessions, while MemGPT can search the raw messages. Critique what the 32.1% versus 92.5% comparison actually shows, from an IR evaluation perspective, and design the experiment that would separate "OS-style memory management" from "having a retriever over raw history".

    What a strong answer covers
    • The comparison changes two things at once: the information available (raw messages versus a summary) and the access policy (agentic paging versus a fixed prompt).
    • The missing baseline is non-agentic retrieval over the same recall storage: embed or BM25-index the messages, retrieve top-k for the question, and give them to the same reader with the same token budget.
    • Also add a full-context baseline where the history fits, to show what is lost by compression at all.
    • Report accuracy against tokens and LLM calls, since paging costs extra inference steps.
    • If single-shot retrieval matches MemGPT, the win is about keeping raw evidence, not about the OS abstraction. The Raw-RAG paper in this section makes exactly this kind of argument for later memory systems.
  4. What if…

    In nested key-value retrieval (140 UUID pairs, about 8k tokens, nesting 0–4), plain GPT-3.5 drops to 0% at one nesting level and plain GPT-4 and GPT-4 Turbo reach 0% by level 3. MemGPT with GPT-4 is unaffected by nesting, while MemGPT with GPT-4 Turbo or GPT-3.5 degrades from level 2. Explain both patterns and propose the ablations that would show which MemGPT component matters for multi-hop lookups.

    What a strong answer covers
    • Each nesting level is one more dependent lookup: the value of one key is the next key. In one forward pass the model must chain these hops inside attention, which fails quickly.
    • MemGPT turns each hop into an explicit search call whose result enters the queue, so a hop is a fresh retrieval rather than an implicit attention chain.
    • Degradation with weaker controllers suggests the bottleneck moves to the controller: issuing the follow-up search, carrying the intermediate key forward, and not stopping early.
    • Ablations: remove heartbeat chaining (one call per user turn), limit page size, remove the working context as a scratchpad for intermediate keys, and swap the controller model at fixed tools.
    • Log the number of calls per question and where trajectories stop, to separate retrieval failures from reasoning failures.
  5. Design

    You want a MemGPT-style memory for a deep-research agent that searches BrowseComp-Plus over many turns. Decide what goes into the working context, what into recall and archival storage, what the eviction policy should be, and how you would evaluate whether paging helps compared with simply truncating old tool outputs.

    What a strong answer covers
    • Working context: the question, the current hypothesis about the answer, verified constraints and candidate entities with document ids; small, edited by explicit calls.
    • Recall storage: the full trajectory, including every query and result list. Archival storage: fetched documents or passages, indexed for search.
    • Eviction: evict tool outputs first (they are long and re-fetchable), keep the agent's own reasoning summaries, and keep document ids so evidence can be re-read rather than re-summarized.
    • Evaluation: same model and tool budget, compare truncation, recursive summary only, and full paging; report accuracy, evidence recall against the BrowseComp-Plus qrels, tokens and number of calls.
    • Failure analysis: count questions where the needed document was fetched earlier but not re-found, which measures paging failure directly.

Done with this page?

Status