Implementing Memory in Our Agent › Testing Your Memory System

Michael BrenndoerferJuly 5, 20251 min read
AI Agent HandbookAll chapters
Part 6 of 15Memory and Retrieval - How Agents Remember
Chapter 17Implementing Memory in Our Agent › Testing Your Memory System
Reading section 8 of 13 · Testing Your Memory System · 1 minSection 8 of 13 · 1 min

Book contents

Section 8 of 13 · 1 min read

Implementing Memory in Our Agent › Testing Your Memory SystemOne section · Sections in this chapter8 / 13 ↓

How do you know if your memory system works well? Here are some tests:

In[2]:
Code
def test_memory_system():
    """Test that memory works correctly."""
    assistant = PersonalAssistant(api_key=os.getenv("ANTHROPIC_API_KEY"))
    
    # Test 1: Store and retrieve
    assistant.chat("Remember that my birthday is July 20")
    response = assistant.chat("When is my birthday?")
    assert "july 20" in response.lower(), "Failed to retrieve birthday"
    
    # Test 2: Semantic search
    assistant.chat("Remember that I'm allergic to peanuts")
    response = assistant.chat("What should I avoid eating?")
    assert "peanut" in response.lower(), "Failed semantic search"
    
    # Test 3: Conversation context
    assistant.chat("I'm planning a trip to Japan")
    response = assistant.chat("What's the best time to visit?")
    assert "japan" in response.lower(), "Lost conversation context"
    
    # Test 4: Persistence
    assistant2 = PersonalAssistant(api_key=os.getenv("ANTHROPIC_API_KEY"))
    response = assistant2.chat("When is my birthday?")
    assert "july 20" in response.lower(), "Facts not persisted"
    
    print("All tests passed!")

test_memory_system()

These tests verify:

  • Facts are stored and retrieved correctly
  • Semantic search finds relevant information
  • Conversation context is maintained
  • Data persists across sessions