Yashotantra Academy logo
level 1
Curious · 0 XP
streak
0d
perfect
0
badges
0

100 XP to Apprentice

Memory · easy

5. Memory: scratchpad, summary, and long-term store

Three distinct memory tiers, each with a different eviction policy.

1 min read3 questionsup to 50 XPtopic 5/14

Read in another language

AI translates the explanation and quiz text while preserving code and answer scoring.

Agents need memory beyond one context window:

| Tier | Holds | Lifetime | Backed by | | --- | --- | --- | --- | | Scratchpad | current message list | one run | in-process array | | Episodic summary | compressed history of the run | run or session | model-generated summary | | Long-term | durable facts and preferences | forever | database + vector search |

Compress the scratchpad when it approaches a token threshold instead of truncating blindly:

ts
async function compress(messages: Msg[], limit = 12_000) {
  if (countTokens(messages) < limit) return messages;
  const [system, ...rest] = messages;
  const head = rest.slice(0, -6);                    // keep the last 6 turns verbatim
  const summary = await chatOnce(
    "Summarise the facts, decisions and open questions below in under 200 words.",
    head,
  );
  return [system, { role: "assistant" as const, content: "Context so far: " + summary }, ...rest.slice(-6)];
}

Long-term memory should store facts, not transcripts: `user prefers metric units` retrieves far better than a 40-turn conversation. Write facts through an explicit `remember` tool so the agent's memory is auditable and deletable — a requirement for GDPR-style deletion requests.

Exercise: add `remember(fact)` and `recall(query)` tools backed by a table with a vector column, then verify a preference stated in session one is applied in session two.

How to study this topic

  1. 1. Read the explanation once end to end, skipping the code.
  2. 2. Re-read the code snippets and say out loud what each line does.
  3. 3. Answer the quiz without scrolling back — that is the real check.
  4. 4. Read every explanation, even for the answers you got right.

Scoring

  • · Up to 30 XP from quiz accuracy.
  • · +15 XP for a perfect first attempt.
  • · +5 XP while a 3-day streak is running.
  • · 75% or higher marks the topic complete.

Check your understanding

  1. 1. Why store facts rather than raw transcripts in long-term memory?

  2. 2. Best way to shrink an overlong scratchpad?

  3. 3. Why route memory writes through an explicit tool?

Badges

First light
Pass your first topic
Sharp shooter
3 perfect first-try quizzes
On a roll
Study 3 days in a row
Week warrior
Study 7 days in a row
Grinder
Earn 500 XP
Deep diver
Earn 1500 XP