Memory between requests
7 min read
The model has no memory
When a conversation feels continuous, that is a trick of the program. On every call, the program sends the earlier messages again. The model reads them as if for the first time. Close the session, and everything is gone unless the program saved it.
Agent memoryText that an agent's program saves and puts back into the prompt. Short-term memory is the history of the current task. Long-term memory is notes stored outside the prompt that last between tasks. is therefore a design choice about what to save, where to keep it and when to bring it back.
Two kinds of memory
Agents use two kinds, and they work differently.
- Short-term memory is the history of the current task. It lives in the Context windowThe maximum number of tokens a model can process in its context. Depending on the model, the limit can include the answer as well as the input. and disappears when the task ends. It holds the steps taken, the results seen and the plan.
- Long-term memory is stored outside the prompt, in a file or a database. It survives between tasks. It holds facts worth keeping, such as "the user prefers metric units" or "the staging server is down on Fridays".
| Short-term | Long-term | |
|---|---|---|
| Where it lives | In the context window | In a file or a database |
| How long it lasts | Until the task ends | Between tasks |
| What it holds | Steps taken, results seen, the plan | Facts worth keeping, such as a preference |
| Who adds it to the prompt | The loop, on every step | The program, when it looks up related notes |
The two work together. At the start of a task, the program looks up related long-term notes and adds them to the prompt. During the task, short-term memory grows. At the end, the agent saves anything new that is worth keeping.
Remember, recall, forget
The sketch below keeps long-term notes in a dictionary. Three functions cover the full cycle.
notes = {}
def remember(key, value):
notes[key] = value
def recall(words):
return {key: value for key, value in notes.items() if any(word in key for word in words)}
def forget(key):
notes.pop(key, None)
remember("user_units", "metric")
remember("user_timezone", "Europe/Berlin")
print(recall(["units"]))
forget("user_units")
print(recall(["units"]))The first print shows the units note. After forget, the second print shows an empty dictionary. A real system would use a database and a smarter search, but the three actions stay the same.
What to store
Storing everything is a mistake, because old notes crowd out useful ones and some of them go stale. Store a fact when all three of these hold.
- It will matter again. A preference or a decision, yes. A one-off search result, no.
- It is stable. "The user lives in Berlin" lasts. "The build is running" does not.
- It is safe to keep. Do not store passwords, payment details or private data that the user did not agree to save.
Also give notes a way out. Let the user see what the agent remembers, correct it and delete it. A memory that nobody can fix will eventually hold an error that the agent repeats with confidence.
Where does the memory live?
An assistant seems to remember what you said yesterday.
A model keeps nothing between calls. Continuity comes from the program saving notes and resending them.
Should the agent save it?
A user says, "I prefer metric units" and then "the build is running right now".
Store facts that will matter again and stay true. A one-off status goes stale in minutes.
What should the user be able to do?
An agent has stored a wrong fact about a user.
A memory nobody can fix will keep repeating its errors. Give users control over saved notes.
Your reading position is saved
0 of 20 complete
Lesson complete
Memory between requests
Up next