Module 4 — Memory: Context & Recall
In Module 3 the agent retained its complete Thought–Action–Observation trace to complete the task. That trace resides in the model's context window — the finite span of recent text accessible to the model at any single inference call. The context window is bounded: once it is full, the oldest entries are evicted. An agent that executes many iterations, or maintains an extended conversation, will lose access to earlier information unless a secondary memory mechanism is introduced.
Two kinds of memory
- Short-term (the context window) — the recent turns the agent can see right now. Fast and automatic, but capped: when it is full, the oldest turn is dropped to make room. This is the working memory from Module 3.
- Long-term (a memory store) — a place the agent can deliberately save a fact to and recall it later by key, even after that fact has scrolled out of the context window. In real systems this is often a database or a vector store.
The essential design decision is determining which information warrants commitment to long-term memory. Anything the agent must not lose — a user's identity, a binding constraint, an intermediate result required at a later iteration — should be written to the store rather than left to be evicted from the context window.
Demonstration: context-window eviction
The agent below has a small context window — it can retain only the most recent turns. Add turns and observe the oldest entries being evicted. Then repeat with the long-term store enabled: the agent commits key facts to the store, and these remain accessible after they have been evicted from the context window.
This activity needs JavaScript. The lesson below still covers everything.
Querying the agent's recall
After loading several turns above, query the agent on information that depends on an earlier fact. With only the context window, the fact may have already been evicted. With the store enabled, the agent retrieves it from long-term memory. This pairing of a context window with an external memory store is the standard architecture of production assistants.
This activity needs JavaScript.
# short-term: a capped list — the context window def remember(turn): context.append(turn) while len(context) > WINDOW: # full? drop the oldest context.pop(0) # long-term: an explicit save / recall the agent controls store["user_name"] = "Mienie" # save a fact by key name = store.get("user_name") # recall it later, after it left the window
Production memory systems are more elaborate — vector embeddings enable the agent to retrieve by semantic similarity rather than by exact key (the vector representations introduced in Course 2 and Course 5) — but the underlying principle is identical: a persistent store that outlives the context window.
Recall by meaning, not by exact key
The store above needs the exact key. Production memory retrieves by meaning: every saved memory and the incoming query are turned into vectors, and the closest ones are pulled back — even when they share no words. Pick a question and watch cosine similarity rank the memories and retrieve the top matches. This is the retrieval step of retrieval-augmented generation.
This activity needs JavaScript. The lesson below still covers everything.
Check your understanding
Answer a short set of questions on context and recall.
This activity needs JavaScript.