Code

cookbook/agent_concepts/memory/02_persistent_memory.py
"""
This example shows how to use the Memory class to create a persistent memory.

Every time you run this, the `Memory` object will be re-initialized from the DB.
"""

from typing import List

from agno.memory.v2.db.schema import MemoryRow
from agno.memory.v2.db.sqlite import SqliteMemoryDb
from agno.memory.v2.memory import Memory
from agno.memory.v2.schema import UserMemory

memory_db = SqliteMemoryDb(table_name="memory", db_file="tmp/memory.db")
memory = Memory(db=memory_db)

john_doe_id = "john_doe@example.com"

# Run 1
memory.add_user_memory(
    memory=UserMemory(memory="The user's name is John Doe", topics=["name"]),
    user_id=john_doe_id,
)

# Run this the 2nd time
# memory.add_user_memory(
#     memory=UserMemory(memory="The user works at a softward company called Agno", topics=["name"]),
#     user_id=john_doe_id,
# )


memories: List[MemoryRow] = memory_db.read_memories()
print("All the DB memories:")
for i, m in enumerate(memories):
    print(f"{i}: {m.memory['memory']} ({m.last_updated})")

Usage

1

Create a virtual environment

Open the Terminal and create a python virtual environment.

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

Install libraries

pip install -U agno
3

Run Example

python cookbook/agent_concepts/memory/02_persistent_memory.py