Long-term memory
Sessions remember conversations. Memory remembers knowledge. If you tell the assistant you are allergic to peanuts, that fact should survive a session reset, a compaction pass, and a computer reboot. The answer is a separate memory store the agent itself can manage.
from pathlib import Path
MEMORY_DIR = Path('memory')
MEMORY_DIR.mkdir(exist_ok=True)
MEMORY_TOOLS = [
{
'name': 'save_memory',
'description': 'Save an important fact as a markdown file for later recall.',
'input_schema': {
'type': 'object',
'properties': {
'topic': {'type': 'string', 'description': 'Short slug for the filename'},
'content': {'type': 'string', 'description': 'The fact to remember'},
},
'required': ['topic', 'content'],
},
},
{
'name': 'memory_search',
'description': 'Keyword search across saved memory files.',
'input_schema': {
'type': 'object',
'properties': {'query': {'type': 'string'}},
'required': ['query'],
},
},
]A writer and a reader. The agent decides when to use them based on instructions in the SOUL.
# Memory
You have a long-term memory store you can write to and search.
- When the user shares a lasting preference, fact, or decision, call save_memory.
- Before answering questions about the user, call memory_search first.
- Trust the memory over your own assumptions.The SOUL is where you teach the agent to use its memory proactively. Without these instructions the model rarely bothers.
Matching exercise: Match the memory type to what it stores
Loading practice…
Eventually yes, keyword search hits a ceiling. For a personal assistant with a few dozen memory files, plain grep is fine and refreshingly easy to debug. Upgrade to embeddings when you have thousands of memories and keyword search actually fails you. Premature vector search is a classic mistake.
Quiz: Quiz
Loading practice…
Checkpoint: Memory and intelligence check
Loading practice…