Memory Patterns
Common patterns for managing memory in AI agents and LLM applications.
In-Memory Storage
Store conversation in application memory.
from agno import Agent
from agno.memory import Memory
agent = Agent(
model = OpenAI( id = "gpt-4o" ),
memory = Memory(),
add_history_to_messages = True
)
# Conversation persists across runs
response1 = agent.run( "My name is John" )
response2 = agent.run( "What is my name?" ) # Remembers "John"
Database Storage
Persist conversations to database.
from agno.memory.db.sqlite import SqliteMemory
agent = Agent(
memory = SqliteMemory(
table_name = "agent_sessions" ,
db_file = "conversations.db"
),
session_id = "user-123"
)
Semantic Memory (Mem0)
Store and retrieve memories by semantic similarity.
from mem0 import Memory
import qdrant_client
# Initialize Mem0 with Qdrant
memory = Memory.from_config({
"vector_store" : {
"provider" : "qdrant" ,
"config" : {
"host" : "localhost" ,
"port" : 6333
}
}
})
# Add memories
memory.add(
"User prefers Python over JavaScript" ,
user_id = "user-123"
)
# Search memories
results = memory.search(
"What programming language?" ,
user_id = "user-123"
)
Shared Memory
Share memory across multiple agents.
from agno.memory import Memory
shared_memory = Memory()
agent1 = Agent( memory = shared_memory, name = "Researcher" )
agent2 = Agent( memory = shared_memory, name = "Writer" )
# Both agents access same conversation history
research = agent1.run( "Research quantum computing" )
article = agent2.run( "Write article based on research" )
Memory Configuration
Store user-specific memories
Generate summaries of conversations
update_user_memories_after_run
Update memories after each interaction
Best Practices
Use in-memory storage for short sessions, database storage for production applications.
Be mindful of PII and sensitive data in memory stores. Implement appropriate data retention and privacy policies.
Memory Management Learn advanced memory techniques
AI Agents Build stateful agents