Memory
How AI apps give a model memory by saving text and loading it into later calls, and how to decide what to save, update and delete.
On this page
A language model does not remember anything between calls, and using it does not change its weights. When an AI app appears to remember you, the app has saved some text and put it back into the context of a later call. Memory is therefore a design problem for the app: deciding what to save, how to keep it correct, and which saved text to load for each request.
Without that design, every new chat starts from nothing and the user has to repeat their preferences each time. With a careless design, the app saves too much, keeps facts that are no longer true, and fills the context with notes that have nothing to do with the question.
How memory works
Every memory system, however it is built, makes the same two decisions on every turn. On the write side it decides what is worth saving and whether the new fact replaces something already saved. On the read side it decides which saved notes belong in the context for the current call. Both decisions are made by the application or by a model that the application calls, never by the main model's weights.
Memory is context that was chosen earlier. It has the same limits as any other context: it can be irrelevant, out of date or wrong, and it uses space in the context window every time it is loaded.
The tracer below uses a small note store with one note per topic. A new fact about a topic replaces the old fact, and each call loads only the topics it needs.
write: notes[topic] = fact (replaces any older fact)read: load notes for the topics this message needscontext = notes loaded + message- topic0dietfact0vegetarian
- session
- 1
1In session 1 the user says they are vegetarian. This is a lasting preference, so it is saved under the topic
diet. - topic0diet1cityfact0vegetarian1Leeds
- session
- 1
2The user also says they live in Leeds, which is saved under
city. They also say they are tired today, which is not saved because it will not be true tomorrow. - topic0diet1cityfact0vegetarian1Leeds
- session
- 2
- loaded
- diet
3In session 2, a new chat, the user asks for quick dinner ideas. The request needs
dietbut notcity, so only the diet note is loaded, and every suggestion is vegetarian. - topic0diet1cityfact0vegetarian1Bristol
- session
- 3
4In session 3 the user says they moved to Bristol. This is the same topic with a newer fact, so Bristol replaces Leeds instead of being added as a second city.
- topic0diet1cityfact0vegetarian1Bristol
- session
- 3
- loaded
- city
5The user then asks for a climbing gym nearby. The
citynote is loaded, so the model searches in Bristol.
The fourth step is the important one. A store that only appends would hold both "lives in Leeds" and "moved to Bristol" in session 3, and the model would sometimes pick the wrong one.
class Memory {
constructor() {
this.notes = new Map(); // topic -> { fact, saved }
}
remember(topic, fact, saved) {
this.notes.set(topic, { fact, saved }); // newer replaces older
}
forget(topic) {
this.notes.delete(topic);
}
recall(topics) {
return topics
.filter((t) => this.notes.has(t))
.map((t) => `- ${t}: ${this.notes.get(t).fact} (saved ${this.notes.get(t).saved})`)
.join("\n");
}
}
const memory = new Memory();
memory.remember("diet", "vegetarian", "March");
memory.remember("city", "Leeds", "March");
memory.remember("city", "Bristol", "June");
console.log(memory.recall(["city"])); // - city: Bristol (saved June)class Memory:
def __init__(self):
self.notes = {} # topic -> (fact, saved)
def remember(self, topic, fact, saved):
self.notes[topic] = (fact, saved) # newer replaces older
def forget(self, topic):
self.notes.pop(topic, None)
def recall(self, topics):
return "\n".join(
f"- {t}: {self.notes[t][0]} (saved {self.notes[t][1]})"
for t in topics if t in self.notes
)
memory = Memory()
memory.remember("diet", "vegetarian", "March")
memory.remember("city", "Leeds", "March")
memory.remember("city", "Bristol", "June")
print(memory.recall(["city"])) # - city: Bristol (saved June)Real systems are less tidy than this. Usually a model reads the conversation and proposes the notes, so it can save the wrong thing, and the notes are loaded by similarity search instead of by exact topic. The three properties still apply: a new fact replaces the old one, only relevant notes are read, and each note carries the date it was saved.
Kinds of memory
Apps combine three kinds of memory, and each one fits a different job.
The conversation itself
The simplest memory is the conversation. The app sends the earlier messages again with every call, so the model can refer to something said ten messages ago. It is the right tool for anything that matters only inside one chat. It fails in two ways. It is lost when the chat ends, and a long conversation eventually exceeds the context window, at which point the app has to drop or compress the oldest messages.
A running summary
A running summary solves the length problem. When the conversation gets long, the app asks a model to summarise the older turns and then sends the summary in place of those turns. The context stays small and the main thread of the chat survives. The cost is that a summary is lossy: whatever it leaves out is gone from the context, including an exact number, a file name or a constraint that turns out to matter later. Summarise in a way that keeps decisions, open questions and exact values, and keep the original turns in storage so that a bad summary can be redone.
Long-term notes
Long-term notes are short facts saved across chats, such as a dietary restriction, a preferred answer length or the project the user is working on. They are what most products mean by "memory". With a small number of notes, the app can load all of them on every call. With a large number, it searches them with the same techniques as retrieval over documents and loads only the best matches. Notes work well for stable facts and preferences. They work badly for details of a single task and for anything that changes often, because a note is only as current as the last time someone updated it.
Deciding when to save
Long-term notes can be written in two ways. In the explicit way, the user asks for it ("remember that I am vegetarian") or approves a note the app proposes, so the user always knows what was saved. In the automatic way, a model reads each conversation and decides by itself what to save. Automatic saving is convenient because the user does nothing, but it produces more wrong and unexpected notes, so it needs the visibility and deletion controls described below. Many apps combine the two by saving automatically and showing the user a list they can edit.
What goes wrong with memory
Saving too much
An app that stores every passing remark fills the memory with moods, one-off tasks and jokes that the model later takes literally. Each loaded note costs tokens and can steer the answer in the wrong direction. Save lasting facts, stated preferences and decisions, and leave out anything that will not be true next week.
Appending instead of updating
A store that only adds notes ends up holding contradictions, such as two different employers or two cities. Key each note by topic so that a newer fact replaces the older one, or have the model that writes notes compare each candidate with the existing notes before it saves anything.
Stale notes
Even a store that updates correctly holds facts that were true when they were saved and are not true now, because nobody told the app. The fix is to save the date with every note, to tell the model in the prompt that notes may be out of date, and to give it a rule for when to check. The first example below has neither the date nor the rule.
In the second example the note is dated and the system prompt says what to do when a relevant note is old.
The extra question costs the user a few seconds and avoids a confident answer about the wrong city.
Memory the user cannot see
Notes written by a model can be wrong, and they can contain personal or sensitive information. If the user cannot see, correct and delete what is stored about them, a wrong note keeps affecting answers with no way to remove it, and the app is holding personal data without the user knowing. Show the notes, let the user edit or delete each one, and avoid saving secrets such as passwords. Privacy and Security covers this in more detail. Saved notes are also text that the model reads as part of its instructions, so a note copied from a web page or an email that contains an instruction can affect later chats. Do not save text from untrusted sources as a note without checking it.
What this changes in practice
When you use an assistant that has memory, treat it as a short list of notes that you can read and edit. Check the list now and then, delete anything wrong or outdated, and state lasting preferences directly ("always keep answers short") so that they are worth saving.
When you build memory into an app, start with the smallest version that works. Save only lasting facts, key notes by topic, store the date, load only the notes that matter for the current request, and give users a screen to inspect and delete notes. Add summaries and similarity search only when the conversation length or the number of notes makes the simple version stop working.
A user tells a general-purpose assistant the following things in one chat. For each one, decide whether it should be a long-term note, be kept for this chat only, or never be stored, and give a reason.
- "I work night shifts as a nurse."
- "Ugh, I'm so tired today."
- "Please always keep answers short, I read them on my phone."
- "My sister is visiting this weekend."
- "Here's my online banking password so you can check my balance: …"
Show a sample answer
- Save it as a long-term note. It is a lasting fact that changes good answers, for example about meal timing or sleep. The user should be able to see it and remove it.
- Keep it for this chat only. It is true today and probably false tomorrow, and a saved mood makes later replies strangely focused on tiredness.
- Save it as a long-term note. It is a clear, lasting preference about every answer, which is exactly what long-term memory is for.
- Keep it for this chat only, or save it with an expiry date. It is useful this week and wrong next week.
- Never store it. Secrets do not belong in memory, and ideally not in the conversation either. A good assistant tells the user not to share them.
The rule behind these answers is to save what is lasting, useful and safe. When it is unclear, the assistant can ask "Do you want me to remember that?"
An assistant with a memory feature recalls your favourite programming language in a brand-new chat. How does it do that?
A memory store holds "works at a bakery" from January and "works at a bookshop" from May. What is the best design fix?
What is the main cost of replacing old chat turns with a running summary?
What to practise next
Notes and documents are prepared in advance. Tools and MCP shows how a model fetches fresh information while it is answering, and Context Rot covers what happens to answer quality when a chat grows too long.