Code

cookbook/agent_concepts/memory/05_memory_search_agentic.py
from agno.memory.v2.memory import Memory, UserMemory
from agno.models.google.gemini import Gemini

memory = Memory(model=Gemini(id="gemini-2.0-flash-exp"))

john_doe_id = "john_doe@example.com"

memory.add_user_memory(
    memory=UserMemory(memory="The user enjoys hiking in the mountains on weekends"),
    user_id=john_doe_id,
)
memory.add_user_memory(
    memory=UserMemory(
        memory="The user enjoys reading science fiction novels before bed"
    ),
    user_id=john_doe_id,
)

# This searches using a model
memories = memory.search_user_memories(
    user_id=john_doe_id,
    query="What does the user like to do on weekends?",
    retrieval_method="agentic",
)
print("John Doe's found memories:")
for i, m in enumerate(memories):
    print(f"{i}: {m.memory}")

Usage

1

Create a virtual environment

Open the Terminal and create a python virtual environment.

python3 -m venv .venv
source .venv/bin/activate
2

Set your API key

export GOOGLE_API_KEY=xxx
3

Install libraries

pip install -U agno google-generativeai
4

Run Example

python cookbook/agent_concepts/memory/05_memory_search_agentic.py