Memory · Free preview

When to Recall

Read memory only when needed

An agent should consult memory only when it actually holds the fact — when the key is missing, the honest move is to say so, not to read back the undefined a naive lookup hands you.

When to recall

You built up a memory store across earlier turns — names, cities, the durable state the agent wrote down. Now a question arrives for a key you never saved, and the naive lookup betrays you: store["age"] doesn't throw, it cheerfully returns undefined. Print that and your agent announces age = undefined — or worse, some model dressed it up as a real number. You've just manufactured a fact out of an empty slot. That is hallucination at its most mechanical, and it's a JavaScript footgun as much as a model one.

The fix is a recall policy: a guard that runs before you answer the question "do I actually hold this key?" The intuition is that a missing key is not a value — it's a different kind of event entirely, and it deserves a different response. So you split the decision in two. If the key is present, recall it and state the real value. If it's absent, don't read the store at all; admit you don't have it.

Take store = { name: "Jakub", city: "Lisbon" } and three questions: name, city, age. The first two are present, so the agent answers name = Jakub and city = Lisbon. But age was never written, so "age" in store is false and the honest line is "I do not have age saved." — no invented number, no undefined leaking into the answer. Notice this is the same instinct as refusing on a no-match in retrieval: when the source is empty, say so.

This is the trust boundary of the whole memory track. An agent that fits its window, keeps a scratchpad, and compacts its history is still worthless if it confidently recites facts it never stored. Users forgive "I don't know"; they don't forgive a fabricated answer that sends them to the wrong city.

Below, the store holds name and city but the questions also ask for age. Make answer check question in store first: recall the real value when the key exists, and return "I do not have age saved." when it doesn't. Done means the age line reads exactly that apology, with no undefined anywhere in the output.

Knowing when to read memory matters as much as what's in it. A missing key is not a value of undefined — it's a signal to say "I don't know."

In the full academy, you write and run this — live, graded:

// A tiny memory store the agent has written across earlier turns.
const store = { name: "Jakub", city: "Lisbon" };

// Incoming questions. Each one asks for the value of a key.
const questions = ["name", "city", "age"];

// 🧠 Recall policy: decide what to say for each question.
// Return the line the agent should print.
function answer(question) {
  // TODO: only recall from the store when you actually have the key.
  // Right now this ALWAYS reads the store — even for keys you never saved.
  return question + " = " + store[question];
}

for (const q of questions) {
  console.log(answer(q));
}

🔒 Live code execution, real agent runs, mastery tracking and verifiable credentials unlock with the full academy.

This is 1 of 50 lessons.

The full academy: write real code, watch real agents run, and earn verifiable credentials — across 8 tracks, in a 3D campus.

Unlock the full academy — $100 →

14-day refund · 🔒 Stripe-secured checkout · lifetime access

More free lessons: An LLM Is a Function  ·  The Agent Loop  ·  Define a Tool  ·  Give an Agent a Tool  ·  Durable State

← The Agent Marketplace