learning-it by example

#23 AI agents and MCP 6.2 min

Agent Memory: What to Keep and What to Forget

My agent forgot my name after four messages. When I let it remember everything, it remembered my card number too.

1. In the SQLite agent memory, why is the slot (such as home city) the key of each row?
2. In the Python agent memory demo, on the scripted chat, how did curated memory score on the six questions, and was the card number stored?
3. In the Python agent memory demo, what does the window mode, which gives the agent only the last four messages and keeps no memories, lose?
4. In the Python agent memory demo, why did curated memory miss the question about the user's name even though the name Priya was stored?
5. In the agent memory video, what stops a card number from being stored?

Practice only: nothing is saved and no score counts towards anything. Each answer comes with the reason.

  1. Try the never store guard

    Copy the code folder first. In a Python prompt, import is_sensitive from guard and call it on these messages: 'my pin is 1234', 'I am pinned down at work', 'Order 5521 is late' and 'card 4111111111111111'. Predict each answer first. When we ran it, the answers were True, False, False and True. Write down why the second one is False.

    A hint

    Look at the word boundaries, written as backslash b, in the SECRET_WORDS pattern.

  2. Add a word to the guard

    Call is_sensitive('my passphrase is blue') and note the answer. Then add passphrase to the SECRET_WORDS pattern in guard.py and call it again. When we did this, the answer changed from False to True. Also try 'I love a good passphrase idea' and say what that shows about a word list guard.

    A hint

    Add |passphrase inside the brackets of the pattern. The guard only looks for the word, not what the sentence means.

  3. Watch a slot replace and a memory expire

    Make sure the Ollama app is running with the all-minilm model. In a Python prompt create a Memory from memory.py. Remember the slot home_city as 'home city: Leeds' of kind profile on day 1, then again as 'home city: York' on day 6, and print memory.rows(). Next remember trip_date of kind task on day 8, then print memory.forget(21) and memory.forget(22). Predict the numbers first. With a stand-in for the embedding step we saw one home city row, then 0 forgotten, then 1.

    A hint

    The task kind lives for 14 days, so a task saved on day 8 expires on day 22. The call is memory.remember(slot, text, kind, day).

  4. Predict a shorter lifetime for tasks

    In memory.py change the task entry in TTL_DAYS from 14 to 3. Predict what python chat.py curated would now print for the number of memories kept and for the day 30 line. Then run it with the Ollama app running and compare. This is a prediction task: we did not run the changed model code.

    A hint

    The trip date is said on day 8, and chat.py forgets expired memories on day 12 before the quiz.

These tasks are for you to try on your own machine. Nothing is checked or saved.

What this does not show

One small local model, one made-up ten-message chat and six questions written by the author, one run. Scoring is strict; the curated mode's one miss is a hedged answer, not a lost memory. No size cap or eviction is built. The guard is two patterns and is not enough for real sensitive data. The card number is a standard test number.

Up next in AI agents and MCP

More in AI agents and MCP

All AI agents and MCP videos

Related videos