# MemGPT: Towards LLMs as Operating Systems

MemGPT pages context between a fixed model window and external storage

Charles Packer, Sarah Wooders, Kevin Lin, Vivian Fang, Shishir G. Patil, Ion Stoica and 1 more · arXiv · 2023-10-12

- **Problem** LLMs can only read what fits in their context window, so long chats and documents cannot be seen at once. Enlarging the window is costly because self-attention cost grows quadratically with length.
- **Method** MemGPT pages data between the window and external storage using function calls.
- **Results** With GPT-4, MemGPT reached 92.5% accuracy on deep memory retrieval, against 32.1% for GPT-4 without it. ROUGE-L recall rose from 0.296 to 0.814 on the same task.

## Why fixed context windows limit LLM use

A transformer-based LLM can only read what fits inside its prompt, so a long chat history or a document longer than the window cannot be seen at once. Enlarging the window is expensive, because self-attention cost grows quadratically with length in both time and memory, which is why the authors treat new long-context architectures as a research problem of their own.

Scaling alone also has limits: long-context models tend to use material near the start or end of the window more reliably than material in the middle.

The paper instead keeps the model's window fixed and manages what sits inside it, borrowing the idea of virtual memory, in which an application sees more memory than is physically present because data is paged between RAM and disk. Function calling is the enabling ingredient: a model that can read and write external stores and edit its own context can move information in and out of the window on its own.

## A two-tier memory around the prompt

MemGPT separates main context, which is the LLM prompt tokens and the only material the processor reads during inference, from external context, which holds everything outside the window. External data has to be explicitly moved into main context before the model can use it, and the model performs those moves through MemGPT function calls without user involvement.

Main context is made of three contiguous parts: read-only system instructions describing the control flow and the function set, a fixed-size working context that the model edits through functions, and the FIFO queue of recent messages and function results. Working context is where the agent keeps durable facts, preferences and persona details, which is why it is central to the conversational results later on.

The first entry of the FIFO queue is a recursive summary of messages already evicted, so older history persists in compressed form inside the window.

External context is split into recall storage, the MemGPT message database that keeps every incoming message and generated output, and archival storage, a read and write store of arbitrary length text objects.

## How evicted messages stay reachable

The queue manager appends each incoming message to the FIFO queue, assembles the prompt, triggers inference and writes both the input and the generated output into recall storage. Retrieved recall messages are appended to the back of the queue, which lets older exchanges re-enter the window when needed.

Eviction works through two thresholds. When prompt tokens pass the warning count, for example 70% of the window, a memory pressure system message tells the model that eviction is coming, so it can save important items to working context or archival storage. When prompt tokens reach the flush count, for example 100% of the window, the queue manager removes a block of messages, for example 50% of the window, and folds them into the recursive summary.

Flushed messages leave the prompt but remain in recall storage and can be read back through function calls.

## From completion tokens to memory operations

The completion tokens are parsed as function calls, checked for correct arguments, and executed, and the outcome including any runtime error is fed back to the model on the next cycle. The system prompt holds both a natural language description of the memory hierarchy and a function schema, so the model learns which operation to use for which tier.

Token warnings are placed in the prompt to guide these decisions, and retrieval is paginated so that one search cannot fill the window.

Inputs to the loop are events: user messages, system alerts such as a finished upload, and timed triggers that run without a user, and each is converted into plain text before entering main context. A function can be called with a request heartbeat=true flag, which runs another inference immediately after the call so several calls can be chained for one query.

Without the flag the model yields until the next event, such as a user message or a scheduled interrupt.

## Context windows of the models compared

The experiments use the gpt-4-1106-preview endpoint as GPT-4 Turbo, gpt-4-0613 as GPT-4 and gpt-3.5-turbo-1106 as GPT-3.5 Turbo, and run MemGPT on top of each of them to show how the base model affects the result.

## Document QA with paged retrieval

The task is a retriever-reader setup built on NaturalQuestions-Open questions, where the reader is asked to answer from retrieved Wikipedia documents. The corpus is a Wikipedia dump from late 2018, and 50 sampled questions are scored by an LLM judge that checks whether the answer is derived from the retrieved text.

The fixed-context baselines cannot go beyond the retriever, because they only see what their window holds, so if the retriever misses the gold article they never see it. MemGPT can call the retriever several times through archival storage and page through results, so the effective number of documents is not bounded by what fits in the window.

Figure 5 plots accuracy against documents retrieved from 0 to 200. The authors report MemGPT as unaffected by the growing context, while truncation lets fixed-context models hold more documents but lowers accuracy as the chance of dropping the gold snippet rises.

The embedding retriever is the main bottleneck, since the gold document often lies beyond the first dozen retrieved results, or even further. MemGPT with GPT-3.5 is markedly weaker on this task, which the authors attribute to its limited function calling, and MemGPT performs best with GPT-4.

## Nested KV retrieval as a multi-hop test

The original Key-Value retrieval task uses 128-bit UUID keys and values, and the nested variant lets a value itself be a key, which forces a multi-hop lookup. Each setup fixes 140 UUID pairs, roughly 8k tokens, and varies the nesting level from 0 to 4 across 30 ordering configurations.

GPT-3.5 reaches 0 percent accuracy already at 1 nesting level, mostly by returning the original value. GPT-4 and GPT-4 Turbo fall to 0 percent by 3 nesting levels. MemGPT with GPT-4 is reported as unaffected by nesting depth, because it repeatedly queries the key-value pairs held in main context.

MemGPT with GPT-4 is the only configuration that stays consistent beyond 2 nesting levels, and MemGPT with GPT-4 Turbo scores below MemGPT with GPT-4. MemGPT with GPT-4 Turbo and with GPT-3.5 also beat their own baselines, but they begin to drop at 2 nesting levels, which the authors link to too few lookups.

## Deep memory retrieval and consistency

The Multi-Session Chat dataset has five sessions per chat, and the authors add a sixth session containing one question and answer between the same two personas. Deep memory retrieval questions were written by a separate LLM so that they can only be answered from the earlier chat log and not from persona summaries.

The baselines see a lossy summary of the five prior conversations, while MemGPT can access the full history through paginated searches that pull results into main context. With GPT-3.5 Turbo, accuracy rises from 38.7% to 66.9% with MemGPT, and ROUGE-L recall rises from 0.394 to 0.629. With GPT-4, accuracy rises from 32.1% to 92.5%, and ROUGE-L recall rises from 0.296 to 0.814.

With GPT-4 Turbo, accuracy rises from 35.3% to 93.4%, and ROUGE-L recall rises from 0.359 to 0.827.

Generated answers tend to be longer than the short gold answers, which is why the authors use ROUGE-L recall rather than precision. The gap is measured on one question set built from Multi-Session Chat personas, so it describes consistency on that set and does not establish performance on other dialogue data.

## Conversation openers and engagement

The opener is scored by SIM-1 and SIM-3 against the gold persona labels, and by SIM-H against the human-written opener from the next session. On SIM-1, the GPT-4 opener scores 0.868, above the 0.800 of the human opener, and GPT-3.5 Turbo scores 0.830 and GPT-4 Turbo scores 0.857. On SIM-H the human opener is the reference at 1.000, and the MemGPT openers score between 0.767 and 0.817, so the engagement gain is shown on the persona-based scores only.

The authors observe that MemGPT openers are more verbose and cover more persona details than the human openers, and they consider the working context central to this effect.

## Limits of the evidence

The document QA results use 50 sampled questions and one 2018 Wikipedia dump, so their precision is limited by that sample. The conversational results rest on Multi-Session Chat persona conversations and on a question set built from them.

MemGPT often stops paging through retriever results before the retriever database is exhausted, so it does not always use the full ranking. MemGPT with GPT-3.5 is markedly weaker on document QA and is attributed to limited function calling. MemGPT with GPT-4 Turbo and with GPT-3.5 begin to drop at 2 nesting levels, and nested KV was tested only up to 4 levels.

## Where MemGPT sits among related approaches

Sparse attention, low-rank approximations and neural memory extend what a model can process, and MemGPT treats such longer main memory as a foundation rather than a replacement. Retrieval-augmented models bring external passages into the prompt, and FLARE lets the model decide when and what to retrieve while it generates.

Agent work such as generative agents with memory, web search with pagination and interleaved reasoning and acting is close in spirit, and the paper's own focus is long-term memory of user inputs.

## Paper info

- **Authors**: Charles Packer, Sarah Wooders, Kevin Lin, Vivian Fang, Shishir G. Patil, Ion Stoica, Joseph E. Gonzalez
- **Venue**: arXiv · Preprint
- **First published**: 2023-10-12
- **Added**: 2026-11-12
- **Updated**: 2024-2-12
- **Version**: v2
- **arXiv**: 2310.08560
- **Category**: cs.AI
- **Original paper terms**: [CC BY 4.0](https://creativecommons.org/licenses/by/4.0/)
- **What we show**: Paper info and commentary

[Original paper ↗](https://arxiv.org/abs/2310.08560v2) · [PDF ↗](https://arxiv.org/pdf/2310.08560v2)

AI commentary is a separate explanation from the original paper and may contain errors. The original paper and figures follow each source's terms of use and any rights noted separately.

## Quote from the paper

Charles Packer, Sarah Wooders, Kevin Lin, Vivian Fang, Shishir G. Patil, Ion Stoica, Joseph E. Gonzalez (2023). MemGPT: Towards LLMs as Operating Systems. arXiv:2310.08560v2. https://arxiv.org/abs/2310.08560v2

**BibTeX**

```
@misc{packer2023memgpt,
  title = {{MemGPT: Towards LLMs as Operating Systems}},
  author = {Charles Packer and Sarah Wooders and Kevin Lin and Vivian Fang and Shishir G. Patil and Ion Stoica and Joseph E. Gonzalez},
  year = {2023},
  eprint = {2310.08560},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2310.08560v2}
}
```

## Other paper commentary

- [A Survey on Multimodal Large Language Models](https://refivu.com/en/papers/3b7ae094-f8ab-4e29-a2fd-d41b1efe8cc0-a-survey-on-multimodal-large-language-models.md)
- [ChatGLM: A Family of Large Language Models from GLM-130B to GLM-4 All Tools](https://refivu.com/en/papers/9637d904-3e6d-4047-bd71-6ae5b68f90d8-chatglm-a-family-of-large-language-models-from.md)
- [BloombergGPT: A Large Language Model for Finance](https://refivu.com/en/papers/2dae70a1-3c88-432c-b934-14abfe8cc431-bloomberggpt-a-large-language-model-for-finance.md)
- [Not what you've signed up for: Compromising Real-World LLM-Integrated Applications with Indirect Prompt Injection](https://refivu.com/en/papers/8c7ef054-dbb9-4dbc-aa5f-7f764d2b0d6b-not-what-you-ve-signed-up-for-compromising.md)
- [Making Pre-trained Language Models Better Few-shot Learners](https://refivu.com/en/papers/58cbc07a-35f5-4401-ada0-4d09082241d1-making-pre-trained-language-models-better-few-shot.md)
- [Evaluating Object Hallucination in Large Vision-Language Models](https://refivu.com/en/papers/994f787c-7b50-4945-8065-0d6a2ea146e6-evaluating-object-hallucination-in-large-vision-language.md)
- [Enhanced-alignment Measure for Binary Foreground Map Evaluation](https://refivu.com/en/papers/668b22a3-321a-4e3f-9465-2c7d6fb8ea23-enhanced-alignment-measure-for-binary-foreground-map.md)
- [ProxylessNAS: Direct Neural Architecture Search on Target Task and Hardware](https://refivu.com/en/papers/40299bf6-79d4-4e50-829c-39acb400238f-proxylessnas-direct-neural-architecture-search-on-target.md)
- [How to Fine-Tune BERT for Text Classification?](https://refivu.com/en/papers/b42bc4b9-8309-43db-89e9-41cd1d0ec800-how-to-fine-tune-bert-for-text-classification.md)
- [Beyond Correlation Filters: Learning Continuous Convolution Operators for Visual Tracking](https://refivu.com/en/papers/cc3e5d8d-4cc2-494c-bc24-c9b0fd010736-beyond-correlation-filters-learning-continuous-convolution.md)
- [Representation Engineering: A Top-Down Approach to AI Transparency](https://refivu.com/en/papers/f6b6fa1e-01cf-4bad-a9dc-8b83f1107927-representation-engineering-a-top-down-approach-to-ai.md)
- [ZOO: Zeroth Order Optimization based Black-box Attacks to Deep Neural Networks without Training Substitute Models](https://refivu.com/en/papers/5b5ce3e2-3dd3-4814-b5fc-ab7233717cda-zoo-zeroth-order-optimization-based-black-box-attacks.md)

## Terms in this paper

- [Accuracy](https://refivu.com/en/terms/1995c0cd6282-accuracy)
- [self-attention](https://refivu.com/en/terms/426589b81077-self-attention)
- [GPT-4](https://refivu.com/en/terms/1e2d0d2cdc44-gpt-4)
- [Large Language Models](https://refivu.com/en/terms/f2404476b183-large-language-models)
- [Wikipedia](https://refivu.com/en/terms/2569175e3d90-wikipedia)
- [LLM judge](https://refivu.com/en/terms/324fc46281d9-llm-judge)
- [ROUGE-L](https://refivu.com/en/terms/b00e147f8ed4-rouge-l)
- [NaturalQuestions-Open](https://refivu.com/en/terms/76be8d7cdf31-naturalquestions-open)

---

AI-written text can contain mistakes. [Refivu](https://refivu.com/en/papers/bee01795-9fbb-4af1-af4c-8b4bff391e4b-memgpt-towards-llms-as-operating-systems)
