← All AI Agents & Tool Use modules

Module 4 — Memory: Context & Recall

Reasoning across steps · hands-on · about 30 minutes.

In Module 3 the agent kept its whole Thought–Action–Observation trace in view to finish the task. But that trace lives in the model's context window — the fixed-size chunk of recent text the model can actually see at once. It is not unlimited. Push enough into it and the oldest material falls off the back, gone. An agent that runs for many steps, or holds a long conversation, will forget things it once knew unless you give it a second kind of memory.

Two kinds of memory

The skill is knowing what to commit to long-term memory. Anything the agent must not forget — a user's name, a constraint, a result it will need ten steps later — should be saved, not left to drift out of the context window.

Fill the context window and watch it forget

The agent below has a small context window — it can hold only the last few turns. Add turns and watch the oldest ones fall off the back. Then try the same turns with a long-term store switched on: the agent saves key facts, so even after a fact leaves the window, it can still recall it.

This activity needs JavaScript. The lesson below still covers everything.

Ask the agent to recall

After loading some turns above, ask the agent a question that depends on an old fact. With only the context window, the fact may already be gone. With the store on, the agent recalls it. This is exactly why production assistants pair a context window with an external memory.

This activity needs JavaScript.

Two memories in code — read only, nothing to install
# 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

A real "memory" is fancier — embeddings let the agent recall by meaning rather than an exact key (the vectors from Course 2 and Course 5) — but the idea is identical: a store that outlives the context window.

AI anchor — why your assistant forgets, and how it remembers Every limit you have felt with an AI chat traces back to the context window: paste a huge document and it loses the start; chat long enough and it forgets what you said at the beginning. The fix shipping in real products is exactly this module — a long-term memory (often a vector database) the assistant writes important facts to and searches when it needs them. "Memory" features, "projects," retrieval-augmented generation: all of them are a store that survives beyond the window.

Check your understanding

A few questions about context and recall. You will get a score.

This activity needs JavaScript.

Why this matters next An agent that can act, remember, and recall still needs to handle goals too big for a single step. Module 5 gives it the discipline to plan first — break a goal into an ordered list of subtasks before charging in.
One-sentence summary: an agent has two memories — a fixed-size context window that holds only recent turns and silently forgets the oldest, and a long-term store it can deliberately save facts to and recall by key, so important information survives after it leaves the window.

Next: Planning Before Acting →