Papa Programmer
Lesson 09 of 20

Memory between requests

7 min read

Optional

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-termLong-term
Where it livesIn the context windowIn a file or a database
How long it lastsUntil the task endsBetween tasks
What it holdsSteps taken, results seen, the planFacts worth keeping, such as a preference
Who adds it to the promptThe loop, on every stepThe program, when it looks up related notes
Short-term and long-term memory

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.

python
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.

Memory is saved textThe program stores notes and puts them back in the prompt. The model keeps nothing.
Short-term and long-termHistory lives in the window for one task. Notes live outside and last across tasks.
Store little, let users editSave stable, useful facts, never secrets, and allow correction and deletion.

Your reading position is saved

0%

0 of 20 complete

Contents

Learning tools