What it is used for

The paper evaluates this design in two domains that are hurt most by a limited context window. The first domain is document analysis: MemGPT is able to analyze large documents that far exceed the underlying model's context window, because the documents are stored outside the context and retrieved piece by piece when needed. The second domain is multi-session chat: MemGPT is used to create conversational agents that remember, reflect, and evolve dynamically through long-term interactions with their users.

In everyday terms, those two domains mean things like the following. A companion agent or personal assistant can continue next week's conversation from its memory of this week's conversation, including facts and preferences the user mentioned earlier. A question answering system can answer questions over a document collection that is too large to read all at once, by searching and examining its documents one by one.

How to use it

MemGPT divides its memory into two kinds. The first kind is main context, meaning all the text currently inside the context window that the model can see at that moment. The second kind is external context, meaning text stored outside the context window; data in external context is not visible to the model and must be explicitly moved into main context before it can be used.

What makes this design different is who manages that movement. The model itself decides, through function calls, meaning written commands the model generates to carry out a specific action: storing something important into external context, retrieving something needed, or evicting something that no longer needs to occupy the window. Because the model edits its own memory, the paper calls this mechanism self-editing memory.

The system also warns before memory fills up. When the amount of text approaches the window limit, the queue manager inserts a memory pressure warning message into the conversation, so the model has a chance to store important things in a safe place before older messages are evicted from the window. MemGPT's work is driven by events, meaning inputs such as user messages, system messages, or scheduled events, and its control flow is managed with interrupts, following how an operating system handles a running program.

Real examples

The following examples are not invented for this article. Both are evaluation tasks documented in the MemGPT paper itself, complete with their result numbers.

The first example is deep memory retrieval, a consistency task for conversational agents. The paper expands the Multi-Session Chat dataset, a record of five conversation sessions between two personas, then adds a sixth session containing a single question that can only be answered from knowledge in the earlier sessions. A model working on its own without MemGPT only sees a shrinking summary of the old conversation, while MemGPT can search its entire conversation history. The results on this task: GPT-4 alone reached 32.1 percent accuracy, while GPT-4 with MemGPT reached 92.5 percent. The same pattern appears for the other models in the same paper table: GPT-3.5 Turbo rose from 38.7 percent to 66.9 percent, and GPT-4 Turbo rose from 35.3 percent to 93.4 percent.

The second example is nested key-value retrieval, a data analysis task that requires collating information from multiple sources. The paper assembles 140 key and value pairs in the form of random identifiers, equivalent to roughly 8 thousand tokens, and some of those values are themselves other keys, so the final answer requires a chained lookup of up to four nesting levels. Ordinary models collapse on this task: GPT-3.5 falls to 0 percent accuracy already at one nesting level, and GPT-4 and GPT-4 Turbo reach 0 percent at three levels. MemGPT with GPT-4 is unaffected by the number of nesting levels, because it checks the stored key and value pairs through repeated function queries until the key chain is complete.

Practitioner level

MemGPT's main context is made of three consecutive sections. The first section is the system instructions, which are read-only and contain information on the MemGPT control flow, how to use each memory level, and how to use its functions. The second section is working context, a fixed-size read and write block of text that can only be changed through function calls; in conversational use, this block stores key facts, preferences, and important information about the user and about the persona the agent is adopting. The third section is the FIFO queue, a rolling history containing agent and user messages, system messages, and function call inputs and outputs; the first position of this queue holds a recursive summary, a continually updated summary of messages that have been evicted from the queue.

External context is also divided in two. Recall storage stores every message that has ever passed through, indefinitely, and those messages can be searched and reinserted into the queue. Archival storage is a read and write database holding text objects of arbitrary length; in the paper's document analysis experiments, the entire document collection, converted into meaning vectors, is loaded into this archival storage, and its search runs as vector search based on cosine similarity.

The queue manager runs clear limit rules. When the token count exceeds the warning token count, which in the paper's example is 70 percent of the context window, a memory pressure warning is inserted into the queue so the model stores important things into working context or archival storage. When the token count exceeds the flush token count, meaning 100 percent of the context window, the queue is partially flushed to free up space: in the paper's example, 50 percent of the context window is evicted, and a new recursive summary is generated from the old summary plus the evicted messages. Evicted messages are not lost; they remain stored in recall storage and can be read again through function calls.

In the multi-document question answering experiments, this arrangement means the number of documents that can be examined is no longer limited by what fits in the window. The paper uses 50 sampled questions from the NaturalQuestions-Open collection and a late 2018 Wikipedia dump, with the same retriever for all parties. An ordinary model can only use the documents handed to it that one time, while MemGPT calls the retriever repeatedly through archival storage and pages through the search results page by page. On the conversation opener task, meaning composing an engaging opening message from knowledge of prior sessions, MemGPT's openers were judged similar to human-written openers and occasionally exceeded them, and the paper notes that storing information in working context is key to that result.

Expert level

At the advanced level, MemGPT reads as a literal application of the operating system idea to language models. Main context plays the role of main memory or RAM, external context plays the role of disk storage, and virtual context management is the technique that provides the appearance of a far larger context through data movement between tiers, just as virtual memory provides the illusion of larger physical memory through paging. Interrupts manage the control flow between the system and the user, and events are the general triggers for the model's inference runs, including scheduled events that let MemGPT run unprompted without user intervention.

The self-editing memory mechanism runs as a feedback loop. In each cycle, the language model processor receives the main context concatenated into a single string and generates an output; that output is parsed by MemGPT, the function is executed if its arguments are valid, and the results, including runtime errors such as trying to add to a main context that is already at maximum capacity, are fed back to the processor as material for the next cycle. Its retrieval mechanisms are cognizant of token constraints and implement pagination to prevent retrieval calls from overflowing the context window. The documented names in the paper are kept as they are at this level: main context, external context, working context, FIFO queue, recall storage, archival storage, memory pressure, recursive summary, and deep memory retrieval for the deep memory retrieval task.

Its authors openly released the code and data for their experiments, as stated in the paper's abstract, and also released their experiment materials for further research, including the expanded multi-session chat dataset, the nested key-value retrieval dataset, and a dataset of embeddings for 20 million Wikipedia articles. In lineage terms, MemGPT is the origin: this 2023 paper and system are what Letta later continued as its successor, and readers looking for the current tooling from this line are directed to the Letta article, not to this one.

Limits and risks

Every result number in this article comes from the paper by the system's own inventors. That standing is stated plainly: the experiments in this paper are an evaluation by the party that designed the system, with tasks and datasets partly assembled by themselves, so its numbers are the inventors' report, not an independent outside measurement.

MemGPT's performance depends heavily on the underlying model's function calling ability. The paper itself notes that MemGPT with GPT-3.5 has significantly degraded performance on document question answering due to its limited function calling capabilities, and performs best using GPT-4. The same system on top of a weaker model is not the same system in its results.

The paper also records a limit of its own behavior: on document question answering, MemGPT will often stop paging through retriever results before exhausting the retriever database, so the document containing the answer may never reach main context. Memory managed by the model itself also means the model decides on its own what is important to store and what may leave the window; a wrong decision at that point carries into later answers, because what was not stored correctly cannot be retrieved correctly.

Finally, the subject of this article is a 2023 research system and paper. Its living line continues in its successor, so readers who need tooling that is maintained today do not stop at this article; this article documents the origin and the design on which that line is based.