The simplest way to use memories is to enable automatic memory creation on your agent:
from agno.agent import Agentfrom agno.db.postgres import PostgresDbagent = Agent( db=PostgresDb(db_url="postgresql+psycopg://ai:ai@localhost:5532/ai"), update_memory_on_run=True,)agent.print_response( "My name is John Doe and I like to hike in the mountains on weekends.", user_id="[email protected]")
The agent automatically extracts and stores relevant information:
memory_manager.replace_user_memory( memory_id="123e4567-e89b-12d3-a456-426614174000", memory=UserMemory( memory="The user's name is John Mary Doe", topics=["name"] ), user_id="[email protected]")
Use an LLM to find semantically relevant memories:
memories = memory_manager.search_user_memories( query="What foods does the user like?", user_id="[email protected]", retrieval_method="agentic", limit=5)
Control what information gets captured as memories:
from textwrap import dedentmemory_manager = MemoryManager( db=db, memory_capture_instructions=dedent(""" Capture the following information as memories: - User's name and personal details - Professional information (job, company, industry) - Preferences and interests - Important dates and events - Goals and objectives Do NOT capture: - Temporary information (weather, time) - Transactional data (order IDs, payment info) - Sensitive information (passwords, SSN) """))
Memories persist across sessions for the same user:
from uuid import uuid4user_id = "[email protected]"# Session 1agent.print_response( "My name is John and I like pizza", user_id=user_id, session_id=str(uuid4()))# Session 2 (different session, same user)agent.print_response( "What food do I like?", # Agent remembers: "You like pizza" user_id=user_id, session_id=str(uuid4()))
Multiple agents can share the same memory database:
db = PostgresDb(db_url="postgresql+psycopg://ai:ai@localhost:5532/ai")agent1 = Agent(name="Sales Agent", db=db, update_memory_on_run=True)agent2 = Agent(name="Support Agent", db=db, update_memory_on_run=True)# Agent 1 creates memoriesagent1.print_response( "I'm interested in your premium plan", user_id="[email protected]")# Agent 2 can access the same memoriesagent2.print_response( "What plan is the user interested in?", # Knows about premium plan user_id="[email protected]")
class UserMemory: memory_id: Optional[str] # Unique identifier memory: str # The actual memory text user_id: Optional[str] # User this memory belongs to agent_id: Optional[str] # Agent that created the memory team_id: Optional[str] # Team that created the memory topics: Optional[List[str]] # Categories/tags updated_at: Optional[int] # Unix timestamp
Complete example with multiple users and sessions:
libs/agno/agno/memory/manager.py:368
import asynciofrom agno.agent import Agentfrom agno.db.postgres import PostgresDbdb = PostgresDb(db_url="postgresql+psycopg://ai:ai@localhost:5532/ai")agent = Agent(db=db, update_memory_on_run=True)async def multi_user_chat(): # User 1 await agent.aprint_response( "My name is Alice and I like anime", user_id="[email protected]", session_id="session_1" ) # User 2 await agent.aprint_response( "My name is Bob and I like hiking", user_id="[email protected]", session_id="session_2" ) # User 1 in new session - agent remembers await agent.aprint_response( "What do I like?", # Response: "You like anime" user_id="[email protected]", session_id="session_3" )asyncio.run(multi_user_chat())