Lesson 54.1 · 1 of 5 · 4 min read · Reviewed 24 Nov 2026
Short-term vs long-term
By the end, you can
- Tell short-term from long-term memory in agents.
- Explain why models need memory added around them.
People talk about AI “memory” as if the model remembers. It doesn’t. Each API call starts blank. Memory is something you build around the model.
+-----------------------------+
long-term store | facts, past episodes, how-tos|
(outside model) +--------------+--------------+
| load what's relevant
v
short-term +-----------------------------+
(context window) | system prompt, this chat, | --> model --> reply
| tool results, loaded memory |
+-----------------------------+
| save what matters
v
back to the store
Short-term memory
Everything in the current context: the conversation so far, tool results, notes. It’s fast and the model sees all of it, but it’s limited in size and disappears when the session ends (chapter 55).
Long-term memory
Anything stored outside the model and loaded back later: a user profile in a database, past conversation summaries, documents in a vector storeVector database: A database built to store embeddings and find the nearest ones to a query quickly., a notes file (chapter 56).
The types map
Borrowing terms from psychology, long-term memoryLong-term memory: Information kept outside the context window, in files or databases, and brought back when relevant. is often split three ways:
| Type | Holds | Everyday example | Lesson |
|---|---|---|---|
| Episodic | What happened | “Last week we debugged the login bug together” | 54.2 |
| Semantic | Facts | “The user prefers metric units” | 54.3 |
| Procedural | How to do things | “To deploy, run tests, then tag a release” | 54.4 |
These are useful labels for deciding what to store and how, not strict categories.
Key points
- A model has no memory between calls: everything it 'remembers' is in the context you send.
- Short-term (working) memory is the current context window: this conversation, these tool results.
- Long-term memory lives outside the model in stores you build, and is loaded back into context when needed.
Add memory around a stateless model
Show that a model only knows what is in its context, then add a small long-term store so a second session remembers the user.
You will have: A script with a toy stateless model, a memory store and two sessions, printing what the model knew in each.
Steps
- Run the starter: the toy model answers only from the context list it is given.
- Session 1: the user gives their name. Save it to the MEMORY dictionary under the user id.
- Session 2 starts with an empty context. Load the user's memories into the context before calling the model.
- Print both session replies and the context sent in session 2.
Done when
- Session 2 greets the user by name.
- Without loading memory, session 2 does not know the name.
- The memory store is keyed by user id.
Open in the Playground No API key needed.
Starter code (Python)
MEMORY = {}
def model(context):
# A toy stateless model: it only knows what is in the context list.
for line in context:
if line.startswith("memory: name="):
return "Welcome back, " + line.split("=", 1)[1] + "."
if line.startswith("user: my name is "):
return "Nice to meet you, " + line[len("user: my name is "):] + "."
return "Hello! What is your name?"
def save(user_id, key, value):
# TODO: store the value in MEMORY under this user
pass
def load(user_id):
# TODO: return context lines such as "memory: name=Priya" for this user
return []
# Session 1
ctx1 = ["user: my name is Priya"]
print("Session 1 reply:", model(ctx1))
save("u-17", "name", "Priya")
# Session 2: a brand new context
ctx2 = load("u-17") + ["user: hi again"]
print("Session 2 context:", ctx2)
print("Session 2 reply:", model(ctx2))
Show a solution
MEMORY = {}
def model(context):
for line in context:
if line.startswith("memory: name="):
return "Welcome back, " + line.split("=", 1)[1] + "."
if line.startswith("user: my name is "):
return "Nice to meet you, " + line[len("user: my name is "):] + "."
return "Hello! What is your name?"
def save(user_id, key, value):
MEMORY.setdefault(user_id, {})[key] = value
def load(user_id):
return ["memory: " + k + "=" + v for k, v in MEMORY.get(user_id, {}).items()]
ctx1 = ["user: my name is Priya"]
print("Session 1 reply:", model(ctx1))
save("u-17", "name", "Priya")
print("Without memory:", model(["user: hi again"]))
ctx2 = load("u-17") + ["user: hi again"]
print("Session 2 context:", ctx2)
print("Session 2 reply:", model(ctx2))