Get Started
Agent engineering

Mission 4 of 8

Give your agent memory it can trust, and see how it goes wrong.

Your agent has perfect recall for one task and total amnesia after it. You'll build short-term and long-term memory, feel the before and after, then poison it on purpose to see the controls that stop it.

70 minIntermediateUnder $0.50 of API creditStep 1 free

What you'll do

Prove the window is the memory

Run the agent twice and see why the second run can't answer, then learn what memory actually has to do.

Build two layers of memory

Add a token budget with compaction, and a SQLite long-term store with search, age, expiry and supersede.

Break it and fix it

Demonstrate stale memory and a poisoning attempt from a file, then switch on the controls that stop each.

  • Run a session agent that recalls before each task and asks before it remembers
  • Know what a memory=True flag would need to do to be safe
Before you start
  • Mission 3 done: tools.py, toolbox.py, tools_v2.py and expenses.txt in agent-lab, venv active, key set
  • Python 3.10+ with SQLite FTS5 (standard in python.org builds; check in step 3)
  • The willingness to type the same question into two terminals and compare. The before/after is the lesson
Your progress0/6
01/06The context window is the only memory. Prove it0 done
STEP 01 of 06~8 min

The context window is the only memory. Prove it

Everything called memory is a way of getting the right text into the next request. Once that is obvious, the design choices are obvious.

Before building any memory, it helps to see the problem for yourself. A short experiment with the agent you already have will show it better than an explanation.

  1. Run mission 3's agent twice:

    Terminal
    python agent2.py "My name is Joe and I keep my expenses in expenses.txt. Remember that."
    python agent2.py "What file do I keep my expenses in?"

    The first run says something warm about remembering. The second has no idea.

    Nothing was stored anywhere, because run() builds a fresh messages list each time and the API is stateless. The model's "I'll remember that" is a sentence, not an action.

Two different questions

So: the model sees exactly one thing, the request you send. Memory, of every kind, is code that decides what to put in that request. There are two questions and they are different:

  • Short-term: what from this session stays in the request? The messages list grows with every tool call. Past about 30,000 tokens, quality drops, cost rises and old tool results crowd out the current task. You need a budget and a way to shrink.
  • Long-term: what from past sessions gets pulled back in? A store the agent writes facts to and reads facts from. The hard part is not storage. It is deciding what to write, what to trust, and when a fact stops being true.

Frameworks sell both as a memory=True flag. By the end of this mission you will know what that flag would have to do to be safe, and you will have seen that most do not.

Tip
Anthropic's API has a server-side memory tool and a context compaction beta that do versions of both layers for you. They are worth knowing about. Build it by hand once first, so you can judge what they are doing with your data and your context.
Checkpoint
You have run the two commands and the second run could not answer. You can say in one sentence why.
Stuck? Ask the community
Next mission · Agent engineeringPlan, act, verify: a research-and-write agent that checks its own facts and knows when to stop
→