Shared User Memories Guide¶
Overview¶
This guide explains how to configure Agno agents to store and share user memories in the auth_db database, allowing memories to be shared across multiple agents and applications.
Reference: Agno Documentation - Sharing Memory Between Agents
Architecture¶
According to Agno documentation: "just connect multiple agents to the same database" - agents connected to the same database automatically share memories for each user.
Current Design¶
- User Information: Stored in
auth_dbdatabase (seeuser_db_mongo.py) - User Memories: Should be stored in
auth_dbdatabase to share across: - Multiple agents
- Multiple applications
- Teams and workflows
- All using the same
user_id
Database Structure¶
auth_db/
├── users (collection) # User information
└── user_memories (collection) # Shared user memories for all agents/apps
Implementation¶
1. Using Shared Storage for All Agents¶
Configure all agents to use the shared auth_db storage:
from bast_ai_agno.bp.mongodb_resources import AgnoMongoDBResources
from bast_ai_agno.bp.agno_agents import AgnoAgentFactory
from agno.agent import Agent
# Get shared storage for user memories
resources = AgnoMongoDBResources(catname="testcat")
shared_storage = resources.get_shared_user_memory_storage(
db_name='auth_db', # Same database as user info
memory_collection='user_memories' # Shared collection for all memories
)
# Create agent factory
agent_factory = AgnoAgentFactory(catname="testcat")
# Create agents with shared storage
agent1 = agent_factory.create_openai_chat_agent(
name="support_agent",
db=shared_storage, # Use shared storage
config_raw={
"enable_user_memories": True,
"add_memories_to_context": True
}
)
agent2 = agent_factory.create_openai_chat_agent(
name="sales_agent",
db=shared_storage, # Same shared storage
config_raw={
"enable_user_memories": True,
"add_memories_to_context": True
}
)
# Agent 1 stores a memory
agent1.print_response(
"My name is John Doe and I like hiking",
user_id="user123"
)
# Agent 2 can retrieve the same memory
agent2.print_response(
"What do I like?",
user_id="user123" # Same user_id
)
# Agent 2 will know about hiking because memories are shared
2. Using MemoryRetriever Service¶
The MemoryRetriever service can be configured to use shared storage:
from bast_ai_agno.svc.memory_retriever import MemoryRetriever
# Create MemoryRetriever with shared storage (default)
memory_retriever = MemoryRetriever(
catname="testcat",
use_shared_storage=True, # Use auth_db (default: True)
db_name='auth_db' # Database name (default: 'auth_db')
)
# Retrieve memories for a user
memories = memory_retriever.get_user_memories(user_id="user123")
3. Configuration Options¶
Option A: Shared Storage (Recommended for Multi-Agent/App Systems)¶
# All agents use auth_db for user memories
shared_storage = resources.get_shared_user_memory_storage(
db_name='auth_db',
memory_collection='user_memories'
)
agent = Agent(
db=shared_storage,
enable_user_memories=True
)
Benefits:
- ✅ Memories shared across all agents and apps
- ✅ Single source of truth in auth_db
- ✅ Consistent with user data storage
- ✅ Easy to query and manage
Option B: Agent-Specific Storage (For Isolated Agents)¶
# Each agent uses its own storage
agent_storage = resources.get_agent_storage('my_agent')
agent = Agent(
db=agent_storage,
enable_user_memories=True
)
Use Case: - Isolated agents that shouldn't share memories - Testing and development - Agent-specific memory requirements
4. MemoryRetriever Configuration¶
# Shared storage (default) - uses auth_db
retriever = MemoryRetriever(
catname="testcat",
use_shared_storage=True, # Default
db_name='auth_db' # Default
)
# Agent-specific storage
retriever = MemoryRetriever(
catname="testcat",
use_shared_storage=False # Uses agent-specific storage
)
Complete Example¶
Setting Up Shared Memories¶
from bast_ai_agno.bp.mongodb_resources import AgnoMongoDBResources
from bast_ai_agno.bp.agno_agents import AgnoAgentFactory
# Initialize resources
resources = AgnoMongoDBResources(catname="testcat")
agent_factory = AgnoAgentFactory(catname="testcat")
# Get shared storage for user memories
shared_storage = resources.get_shared_user_memory_storage(
db_name='auth_db',
memory_collection='user_memories'
)
# Create multiple agents with shared storage
support_agent = agent_factory.create_openai_chat_agent(
name="support_agent",
db=shared_storage,
config_raw={
"enable_user_memories": True,
"add_memories_to_context": True
}
)
sales_agent = agent_factory.create_openai_chat_agent(
name="sales_agent",
db=shared_storage,
config_raw={
"enable_user_memories": True,
"add_memories_to_context": True
}
)
# User interacts with support agent
support_agent.print_response(
"My name is John Doe and I prefer email communication",
user_id="user123"
)
# Sales agent can access the same memory
sales_agent.print_response(
"How should I contact you?",
user_id="user123"
)
# Sales agent knows about email preference from shared memory
Retrieving Shared Memories¶
from bast_ai_agno.svc.memory_retriever import MemoryRetriever
# Create retriever with shared storage
retriever = MemoryRetriever(
catname="testcat",
use_shared_storage=True,
db_name='auth_db'
)
# Retrieve all memories for a user
result = retriever.get_user_memories(user_id="user123")
print(f"User: {result['user_id']}")
print(f"Memories: {result['memory_count']}")
for memory in result['memories']:
print(f" - {memory.get('content', memory)}")
Migration Strategy¶
For Existing Agents¶
-
Update agent creation to use shared storage:
-
Update MemoryRetriever:
For New Agents¶
Always use shared storage for user memories:
shared_storage = resources.get_shared_user_memory_storage()
agent = Agent(db=shared_storage, enable_user_memories=True)
Best Practices¶
- Consistent Database: Use
auth_dbfor all user-related data (info + memories) - Shared Collection: Use
user_memoriescollection name consistently - Same user_id: Ensure all agents/apps use the same
user_idformat - MemoryRetriever: Use
MemoryRetrieverservice for retrieving memories - Configuration: Set
use_shared_storage=Trueby default
Database Schema¶
auth_db.user_memories Collection¶
Agno stores memories in the following format:
{
"_id": ObjectId("..."),
"user_id": "user123",
"memory": "User's name is John Doe.",
"memory_id": "uuid-here",
"created_at": ISODate("..."),
"updated_at": ISODate("..."),
...
}
All memories are stored in the same collection, indexed by user_id.
Troubleshooting¶
Memories Not Shared¶
Issue: Agent A stores memories, but Agent B can't see them.
Solution: Ensure both agents use the same storage:
shared_storage = resources.get_shared_user_memory_storage()
agent_a = Agent(db=shared_storage, ...)
agent_b = Agent(db=shared_storage, ...)
Different user_id Formats¶
Issue: Memories stored with different user_id formats.
Solution: Standardize user_id format across all agents/apps:
# Use consistent user_id format
user_id = str(user['_id']) # From auth_db
agent.print_response("...", user_id=user_id)
Related Files¶
src/python/agno/bast_ai_agno/bp/mongodb_resources.py-get_shared_user_memory_storage()methodsrc/python/agno/bast_ai_agno/svc/memory_retriever.py-MemoryRetrieverservicesrc/python/mongo/bast_ai_mongo/bp/user_db_mongo.py- User database structure