Memory Manager
Use a MemoryManager to give agents persistent memory across sessions.
"""
Memory Manager
=============================
Use a MemoryManager to give agents persistent memory across sessions.
"""
from agno.agent import Agent
from agno.db.sqlite import SqliteDb
from agno.memory.manager import MemoryManager
from agno.models.openai import OpenAIResponses
# ---------------------------------------------------------------------------
# Create Agent
# ---------------------------------------------------------------------------
db = SqliteDb(db_file="tmp/memory_demo.db")
agent = Agent(
model=OpenAIResponses(id="gpt-5.2"),
db=db,
# Enable agentic memory so the agent can store and retrieve memories
enable_agentic_memory=True,
# Provide a MemoryManager for structured memory operations
memory_manager=MemoryManager(
db=db,
model=OpenAIResponses(id="gpt-5-mini"),
),
markdown=True,
)
# ---------------------------------------------------------------------------
# Run Agent
# ---------------------------------------------------------------------------
if __name__ == "__main__":
# First interaction: tell the agent something to remember
agent.print_response(
"My name is Alice and I prefer Python over JavaScript.",
stream=True,
)
print("\n--- Second interaction ---\n")
# Second interaction: the agent should recall the preference
agent.print_response(
"What programming language do I prefer?",
stream=True,
)Demonstrate recall across sessions
The source is a single-user demonstration: without user_id, memory uses the shared default user, and the two calls reuse one generated session. Memory still works in that configuration.
To demonstrate cross-session recall, replace both calls in the saved script with the following. Keep the same SQLite path and user ID across runs; separate users need separate IDs. With agentic memory, the model must choose to save the preference.
agent.print_response(
"Remember that my name is Alice and I prefer Python over JavaScript.",
user_id="alice",
session_id="alice-first-visit",
stream=True,
)
agent.print_response(
"What programming language do I prefer?",
user_id="alice",
session_id="alice-second-visit",
stream=True,
)Run the Example
Set up your virtual environment
uv venv --python 3.12
source .venv/bin/activateInstall dependencies
uv pip install -U agno openai sqlalchemyExport your OpenAI API key
export OPENAI_API_KEY="your_openai_api_key_here"Run the example
Save the code above as memory_manager.py, then run:
python memory_manager.pyFull source: cookbook/02_agents/06_memory_and_learning/memory_manager.py