Memory · Free preview

Forget When Full

Evict the least-recently-used

Agent memory is bounded, so when it fills you must evict — and evicting the least-recently-used key keeps the items you actually keep touching, unlike dropping a random or oldest-inserted one.

Forget When Full

Your agent's working memory is not infinite. It's a fixed-size buffer — a few slots of recent facts, the last handful of tool results, whatever fits in the context budget you set in the last lesson. So the real question isn't what to remember; it's what to throw away when the slots run out. Get that wrong and the agent forgets the thing it's about to need while still holding junk it touched once an hour ago.

The cheap, surprisingly good answer is LRU — least-recently-used. When memory is full and something new arrives, evict the item that has gone untouched the longest. Not a random victim (you might nuke the fact you need next). Not the oldest-inserted item (that punishes things you keep coming back to). Recency of use is the proxy: if you haven't touched it in a while, you probably won't miss it.

The trick is that reading counts as using. Keep memory ordered most-recently-used-first, and every access — write or read — moves that key to the front. Walk the trace ["set a","set b","set c","get a","set d"] over a capacity of 3: after the three sets, memory is [c, b, a] (c freshest). Then get a touches a, sliding it to the front: [a, c, b]. Now set d arrives and memory is full — so you evict the tail, b, the one nobody has touched since the start, and unshift d: [d, a, c]. That one read of a is exactly what saved it from being evicted.

Below, the recency-refresh is already wired — use pulls an existing key out and unshifts it to the front. The missing piece is the eviction itself: when mem is already full and a new key comes in, drop the least-recently-used key (the one at the end of the array) and record it before unshifting. Make it print the survivors MRU-first, then the key you let go.

A bounded memory is only as smart as its eviction rule — keep what you keep touching, forget what you stopped touching.

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

// Memory is bounded: this LRU holds at most CAP keys.
// 'mem' is ordered MOST-recently-used first (index 0 = freshest).
const CAP = 3;
const mem = [];
const evicted = [];

// "use" a key: move it to the front (most recent). If it's new, add it.
// On a "set" when memory is already FULL, you must EVICT the least-recently-used
// key — the one sitting at the END of the array — before adding the new key.
function use(key) {
  // Drop any existing copy so we can re-insert it at the front (refresh recency).
  const at = mem.indexOf(key);
  if (at !== -1) mem.splice(at, 1);

  // TODO: if mem is FULL (length === CAP) and 'key' is new, evict the
  // least-recently-used key (the last element) and push it onto 'evicted'.
  // Right now nothing is evicted, so mem grows past CAP.

  mem.unshift(key); // put the just-used key at the front
}

const trace = ["set a", "set b", "set c", "get a", "set

🔒 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