Sub topic 16
Testing whether memory helps
This chapter assumes you have read the earlier chapters on short-term and long-term memory. It also assumes you can write basic Python and know that an agent calls an LLM in a loop. Memory adds code, storage, and prompt tokens. You should confirm it improves behavior before you keep it.
The core idea: compare with and without memory
Run the same scripted conversations twice. In one run memory is on, and in the other it is off. Then ask a probe question that can only be answered correctly if the agent remembered something. If both runs score the same, memory is not helping.
What a good test case contains
Each case needs four parts:
- Setup turns: messages that state a fact, such as "My name is Priya."
- Probe: a later question that depends on that fact, asked in a new session so the buffer cannot help.
- Expected text: a string the correct reply must contain.
- Forbidden text: a string that must not appear, such as another user's fact. This checks scoping.
A runnable harness
The agent below is a stub that stores a name. In a real test you would pass in your actual agent with the same chat(user_id, text) method.
class StubAgent:
def __init__(self, use_memory):
self.use_memory = use_memory
self.facts = {}
def chat(self, user_id, text):
if text.startswith("My name is "):
if self.use_memory:
self.facts[user_id] = text[len("My name is "):].rstrip(".")
return "Nice to meet you."
if "my name" in text.lower():
name = self.facts.get(user_id)
return f"Your name is {name}." if name else "I don't know your name."
return "OK."
CASES = [
{"user": "u1", "setup": ["My name is Priya."],
"probe": "What is my name?", "expected": "Priya", "forbidden": "Sam"},
{"user": "u2", "setup": ["My name is Sam."],
"probe": "What is my name?", "expected": "Sam", "forbidden": "Priya"},
]
def evaluate(use_memory):
agent = StubAgent(use_memory)
for case in CASES:
for text in case["setup"]:
agent.chat(case["user"], text)
passed = 0
for case in CASES:
reply = agent.chat(case["user"], case["probe"])
if case["expected"] in reply and case["forbidden"] not in reply:
passed += 1
return passed / len(CASES)
print("without memory:", evaluate(False))
print("with memory: ", evaluate(True))This prints 0.0 without memory and 1.0 with memory. The setup for all users runs before any probe, so a store that ignores user_id would fail the forbidden-text check.
Common errors
- Leaky probes: if the probe repeats the fact, the model can answer without memory. Phrase it indirectly.
- Too few cases: model replies vary. Use many cases and repeat runs before trusting a small difference.
- Only testing recall: also test cases where memory should stay silent, such as a question unrelated to stored facts. Irrelevant memories can distract the model.
- Ignoring stale facts: add a case where the user changes a fact, then check the newest value wins.
- Measuring only accuracy: record prompt tokens and latency too. A small gain may not justify a large cost.
Exercises
Exercise
Add a test case that checks an update. The user says "My name is Priya." and later "My name is Priya Rao." What should the expected and forbidden values be, and what memory feature does it test?
Show answer
Expect "Priya Rao" in the reply to the probe. A reply that gives only the old value is wrong, so the check should confirm the newest value is returned. This tests updating and deduplication: the store must replace or supersede the old fact instead of returning both.