Part 02›
✎

04. Memory and Knowledge for Agents#

Level: Practitioner · Reading time: 30 min · Exercise: 60 min Prerequisites: The Anatomy of an Agent (the loop and its messages list)

In one sentence: After this chapter you can decide what an agent keeps in its context and what it stores outside, compact a long run without losing the thread, and build retrieval that answers questions spread over several documents.

Why this matters#

A support agent is on turn 40 of a session. Its context is full. It reads the same file for the third time, it has lost the order number from turn 3, and every turn costs more than the last.

A second agent is asked: "Which of our engineers worked on both the payments migration and the iOS app?" No single document says both. The agent searches, finds twenty passages that each match half of the question, and gives up.

Both are memory problems. The first is about the context. The second is about how knowledge outside the context is found.

The core idea#

Memory for an agent is three decisions: what to write down, where to store it, and what to read back before each step.

Working memory Long-term memory
Where it lives The context window: the messages list Files, a database, a search index
Size Small As large as you like
How the model reaches it It is already in front of the model Your code must retrieve it and add it to the context
Cost Paid again at every step Paid once per lookup
Typical failure Overflow, or old facts pushed out Wrong or missing item retrieved

This is like the difference between RAM and a database. The analogy breaks twice. A program follows a pointer to exactly the item it wants; a model sees only what your code put in front of it. And a model does not weight every line of a long context evenly: it tends to use what sits at the start and the end better than what sits in the middle.

To store knowledge outside the context you need three terms. A chunk is one piece of a document, a few hundred tokens. An embedding is a list of numbers that stands for a piece of text, placed so that similar meanings get similar numbers. Vector search finds the stored chunks whose embeddings are closest to the embedding of the question.

How it works, step by step#

A. Context management: five rules#

  1. Keep the front stable. System prompt and task come first and never change. Providers charge less for a repeated prefix, and the model sees the rules every turn.
  2. Keep tool results small. Return counts and a page, not whole files. Save large outputs to a file and keep only the path. (Chapter 02)
  3. Compact when the history passes a limit. Replace the oldest steps with a summary (next subsection).
  4. Let the agent take notes. Give it a notes file it reads at the start and writes before each compaction. The file keeps exact values that a summary would garble: IDs, paths, numbers.
  5. Load on demand. Keep instructions and reference material outside the context and retrieve them when they are relevant.

B. Compaction#

Compaction is shortening the history by summarizing it. It is lossy, so the summary must keep what the task cannot run without: the goal, each decision and its reason, every ID and path learned, each failure and its error text, and the open questions.

def maybe_compact(messages, limit, count_tokens, summarize):
    """Illustrative pseudocode: shrink the history once it passes a limit."""
    if count_tokens(messages) <= limit:
        return messages
    system, task, history = messages[0], messages[1], messages[2:]
    summary = summarize(system, task, history)        # one model call
    return [system, task,
            {"role": "assistant", "content": "State so far:\n" + summary}]

A prompt for summarize:

Summarize this conversation for an agent that will continue the task. Keep: the goal; every decision and why it was made; every fact, ID and file path learned; what failed and the exact error text; open questions. Drop: file contents, repeated tool output, and successful steps that need no follow-up. At most 300 words.

C. Retrieval: the standard pipeline#

Retrieval-augmented generation (RAG) is the common way to add knowledge: find relevant chunks and put them in the prompt.

  1. Split documents into chunks.
  2. Compute an embedding for each chunk and store all of them in an index, a data structure that makes nearest-neighbor lookup fast.
  3. When a question arrives, compute its embedding.
  4. Fetch the nearest few chunks.
  5. Add them to the prompt, each with its source.
  6. Measure two things separately: whether the right chunk was fetched, and whether the answer was correct.

Step 6 matters because the two failures need different fixes. If the chunk was never fetched, improve the index or the query. If it was fetched and the answer is still wrong, the problem is the prompt or the model (evaluation).

D. Retrieval that follows associations#

Vector search compares each chunk with the question on its own. That fails when the answer is a combination of facts that no single chunk contains. Yu Su's research group built HippoRAG for this case. The idea: add structure, a network of concepts and their links, on top of the embeddings, and let retrieval spread across the links.

Worked example#

Six short passages and a two-part question. Which person is at Stanford and works on Alzheimer's? Run it: it needs only Python.

PASSAGES = [("Ada", "Stanford"), ("Ada", "systems"), ("Linus", "Stanford"),
            ("Linus", "Alzheimer's"), ("Grace", "MIT"), ("Grace", "Alzheimer's")]
query = {"Stanford", "Alzheimer's"}

# 1. Similarity search: rank passages by how much of the question each one covers.
ranked = sorted(PASSAGES, key=lambda p: -len(query & {p[1]}))
print([p for p in ranked if query & {p[1]}])
# [('Ada', 'Stanford'), ('Linus', 'Stanford'), ('Linus', "Alzheimer's"), ('Grace', "Alzheimer's")]
# Four passages tie at half of the question. No single passage holds the answer.

# 2. Association: join what is said about each person, then test the whole question.
joined = {person: {topic for name, topic in PASSAGES if name == person}
          for person, _ in PASSAGES}
print([person for person, topics in joined.items() if query <= topics])   # ['Linus']

The first search ranks by similarity and returns a four-way tie. The second joins the passages that share an entity, in this case a person's name, and only then tests the question. The join is what a structured index does.

What memory buys and what it costs#

Memory buys Memory costs
Facts newer than the model's training, or private to a user A retrieval step that can miss or return noise
Continuity from one session to the next Stale or contradictory entries
More knowledge than fits in any context window Latency and money on every lookup
Lessons from earlier runs Engineering: indexing, updates, access control, evaluation

A concrete risk comes from Yu Su's own study. He describes language models as "highly receptive" to outside evidence (24:00). Correct retrieved facts repair wrong answers. Coherent false evidence replaces right answers with wrong ones. So whatever an agent retrieves must be treated as data to check, not as truth or as instructions (prompt injection).

What the practitioners say#

Yu Su (Spring 2025) devotes the first third of his lecture to memory.

Figure 1. HippoRAG: the index is built once from the passages, and each question is then answered by a search over that index.

flowchart LR
    P["Passages"] --> L["Language model extracts concepts and relations"]
    L --> K["Knowledge graph of nodes and links"]
    Q["Question"] --> N["Concepts found in the question"]
    K --> S["Matching nodes become seeds"]
    N --> S
    S --> R["Personalized PageRank spreads weight along the links"]
    R --> W["Nodes linked to several seeds get the top weight"]
    W --> T["Passages ranked by the weight of their concepts"]

The passages are processed once, when the index is built; each question then needs only a concept lookup and a graph search to find the passage that joins both facts.

Yangqing Jia (Fall 2025) makes one point from the infrastructure side: retrieval and accurate data-processing pipelines are what matter (68:00). The hard part of moving AI workloads is data locality, getting the data next to the machines that read it (70:00). For an agent, the knowledge store is infrastructure: where it lives, how fast it can be read, and how it is kept accurate.

Pitfalls#

  1. Storing everything. An index of everything retrieves noise. Fix: decide per source whether it belongs in memory, and test retrieval hit rate.
  2. Summarizing away the state. A summary that drops the goal or an ID sends the agent off course. Fix: a fixed summary format and a notes file for exact values.
  3. Compacting too late. Fix: compact at a threshold well below the window's limit, because the next tool result still has to fit.
  4. Trusting retrieved text. Fix: keep sources on every chunk, and treat text as data, never as instructions.
  5. Forgetting access rules. A shared index can surface a document the current user may not see. Fix: filter by permission before retrieval, not after.
  6. Assuming long context solves it. A big window still costs more at every step and is read unevenly. Fix: retrieve what is needed and leave the rest outside.

Try it#

Add memory to the agent from Chapter 01.

  1. Create a notes file. The agent reads it at the start and rewrites it before every compaction.
  2. Implement maybe_compact with a token counter and summarize. With a stub model, write summarize as plain code that copies every history line containing an ID or a path.
  3. Run a scripted task of at least 15 tool calls with the deterministic stub model. Set a low limit and let each tool return long text, so the limit is passed twice.

Deliverable: the code, the notes file after the run, and the log of when compaction fired.

You are done when: an ID learned early in the run still appears verbatim in the final answer; the system prompt is never summarized; and the run's largest context stays under your limit.

Stretch goal: add vector retrieval over a folder of documents and measure separately how often the right chunk is fetched and how often the answer is right.

Key takeaways#

Self-check#

  1. Why must the front of the context stay unchanged from step to step?
  2. Name four things a compaction summary must keep.
  3. In the worked example, why does similarity search return a four-way tie?
  4. What does the association step do that similarity search does not?
  5. What did Su's study find about models and outside evidence, and what are the two consequences?
  6. Why measure chunk retrieval and answer correctness separately?
Show answers 1. The model sees the rules and the task on every turn, and the provider can reuse the unchanged prefix at a lower price instead of processing it as new text. 2. Any four of: the goal; each decision and its reason; facts, IDs and file paths learned; what failed and the exact error text; open questions. 3. Each passage holds one topic, so each covers exactly half of the two-part question. All four matching passages score the same. 4. It joins the passages that share an entity, so the facts about one person are combined before the question is tested. That exposes the one person connected to both topics. 5. Models follow coherent outside evidence even when it conflicts with what they learned. Correct evidence fixes wrong answers, and false evidence overwrites right ones. 6. Because they fail for different reasons. A missed chunk calls for a better index or query. A wrong answer despite the right chunk calls for a better prompt or model.

Sources#

Next#

→ 05. Compound AI Systems and DSPy, because tools, planning and memory turn a single prompt into a pipeline, and pipelines need a way to be written and tuned.