Vocabulary for this course

The terms the chapters assume, in plain words. You can open this from any chapter.

action space
The full set of moves an agent can choose from, usually the list of tools it is given.
agent
A program that repeats a loop: read the state, choose a move, look at the result, then decide again.
budget
A hard limit on how many steps, seconds or moves a run may spend before it must stop.
delegation
Handing a smaller task to another agent, with its own instructions, tools, budget and required result shape.
guardrail
Code that checks a proposed move against the rules before it runs, and can refuse it or ask a person.
loop
The repeated cycle of choosing a move, carrying it out and reading the result, which makes a program an agent.
model
The trained program that reads text and chooses what to say or do next.
observation
What came back from a tool or a step, added to the state for the next decision.
plan
A written list of steps for the task, which can be revised when a step fails or the facts change.
scratchpad
The working notes for one run: the plan, what was tried and what came back, discarded when the run ends.
state
The record of the question, the plan and the observations so far, passed to the model on every turn.
sub-agent
A separate agent given one bounded job, its own tools and its own budget, whose answer returns to the caller.
tool
A named function an agent may ask to run, with declared inputs and a result the loop can read.
trace
The ordered record of every decision, tool use and result in a run, used to explain what happened.

AI agents from the ground up

Memory for agents

Three kinds of memory, three jobs: the scratchpad for this run, the transcript for this conversation, and a store for facts that outlive both.

Part 5 of 10

Intermediate15 min readBack to Courses

Three memories, not one

The scratchpad is the agent’s state for the current run: the plan, the observations, what has been tried. It is discarded when the run ends and it is the only memory the loop actually needs.

The transcript is the conversation with the user. It outlives the run and grows, so it needs trimming. The long-term store is facts worth keeping across conversations: preferences, identifiers, decisions. It is retrieved, not replayed.

A support run shows the split. The scratchpad holds the current ticket, the policy passages retrieved for it, and the draft answer. The transcript holds the customer’s messages across the session. The long-term store holds the fact that this customer prefers email and has a team plan. None of those belongs in the other two, and moving one is how the run gets slower or the answer gets stranger.

Mixing them is the usual bug

Putting the whole transcript in the scratchpad makes every step expensive and buries the current observations. Putting the scratchpad in the transcript leaks internal reasoning to the user and to the next turn.

Keeping a fact only in the transcript means it disappears when the window trims. Keeping everything in the long-term store means retrieving noise forever. Each memory has a job; assign the data accordingly.

The scratchpad should be rebuilt for each run, not accumulated across runs. A cached observation from yesterday is stale evidence for today’s question, and it is exactly the kind of thing that makes an agent answer a question nobody asked. Start clean, load only what the current task needs, and let the long-term store supply the durable facts through retrieval.

Writing a memory worth keeping

memory = {
    "user_id": user,
    "fact": "prefers email over phone",
    "source": "conversation 2026-10-29",
    "confidence": 0.9,
    "expires": None,
}

store.put(key=f"{user}:contact-preference", value=memory)

Keep the scratchpad small

The run appends every observation. Run it, then prune to the last three observations and confirm the plan and the answer still work.

Output
Click "Run" to execute your code

Write less, retrieve better

A memory is useful in proportion to how precisely it can be found. “The user likes email” is a fact; “contact preference: email, source: conversation on 29 October, confidence 0.9” is a fact you can rank, expire and explain. The extra fields are not bureaucracy — they are what lets retrieval prefer the recent, sourced item over an old guess.

A common mistake is storing a model-written summary as if it were an observation. The summary says “the user seems to prefer email”, the store records “prefers email”, and three months later the agent states it as a settled preference. The symptom is a memory that is confidently wrong and impossible to trace. Store the source beside the claim, and treat model-written text as a claim.

Retrieval beats replay

A long-term store is only useful if you can find the right item. Replaying everything is the same as having no store; it just costs more. Retrieve by relevance to the current task, with recency and confidence as tie-breakers.

That means the store needs the same machinery as RAG: an index, a query, a cutoff. A memory system without retrieval is a log file with optimistic branding.

A memory written by the model is a claim, not a fact. “The user is an admin” stored from a conversation must never be read as authorisation.

Forgetting is a feature

Memories expire. A preference from two years ago may be wrong; an order number from last month is noise. Store a timestamp and a confidence, and let old low-confidence items fall out of retrieval.

Users also need a way to see and delete what is remembered. That is a product requirement in most jurisdictions and a trust requirement everywhere.

Set the expiry by the kind of fact, not by a single global number. A contact preference might last a year, an order number a month, a session detail a day. A single TTL is easier to configure and almost always wrong for at least one kind of memory. Write the rule down beside the store, so the next person knows why an order number vanishes after thirty days and a preference does not.

Retrieve a memory, not all of them

The store has five facts. Score each by relevance and recency, then retrieve the top two for a question about contact preferences.

Output
Click "Run" to execute your code

Check what you learned

One question at a time. A wrong answer explains itself — take another go.

Part 1 of 5

Which memory holds the plan and observations for one run?