#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.
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.
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.
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).
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
Related videos
- #06 Test Your LLM App with pytest: An Eval Harness in 40 Lines LLM engineering and evals
- #18 Prompt Injection Live: Attacking My Own LLM App AI security and governance
- #19 Redact Personal Data Before Your LLM Sees It (Python) AI security and governance
- #20 AI Coding Assistant Workflow: Copy, Test, Read the Diff AI coding and productivity
