Memory retrieval

Saving facts is half the story. The harder half is pulling the right one back when the user asks a question. Storage without retrieval is a landfill. The agent needs to reach into memory before it answers, not after.

Search before answer

A question triggers memory_search first. Results enter the context, and only then does the agent compose the reply.

08-long-term-memory/bot.py
python
def memory_search(query: str) -> str:
    query_lower = query.lower()
    hits = []
    for path in MEMORY_DIR.glob('*.md'):
        text = path.read_text()
        if query_lower in text.lower() or query_lower in path.stem.lower():
            hits.append(f'## {path.stem}\n{text}')

    if not hits:
        return 'No matching memories found.'
    return '\n\n'.join(hits[:5])

A plain keyword scan across the memory directory. The agent calls this tool, reads the returned snippets, and cites them in its reply.

The SOUL tells it to search first whenever a question touches the user. In practice that means anything starting with what, when, which, or any question that sounds like recall. When in doubt, search. A failed search returns a short no-match string and the agent falls back to asking. Cheap lookup, honest answer.

Quiz: Quiz

Loading practice…