Working with Memories
Customize how memories are created, control context inclusion, share memories across agents, and use memory tools for advanced workflows.
The basic memory setup covers most use cases, but sometimes you need more control. This guide covers advanced patterns for customizing memory behavior, controlling what gets stored, and building complex multi-agent systems with shared memory.
Customizing the Memory Manager
The MemoryManager controls which LLM creates and updates memories, plus how those memories are generated. You can customize it to use a specific model, add privacy rules, or change how memories are extracted:
Install dependencies and set your key before running the examples:
pip install agno openai sqlalchemy
export OPENAI_API_KEY="your-api-key"from agno.agent import Agent
from agno.db.sqlite import SqliteDb
from agno.memory import MemoryManager
from agno.models.openai import OpenAIResponses
# Setup your database
db = SqliteDb(db_file="agno.db")
# Setup your Memory Manager, to adjust how memories are created
memory_manager = MemoryManager(
db=db,
# Select the model used for memory creation and updates. If unset, the default model of the Agent is used.
model=OpenAIResponses(id="gpt-5.2"),
# You can also provide additional instructions
additional_instructions="Don't store the user's real name",
)
# Now provide the adjusted Memory Manager to your Agent
agent = Agent(
db=db,
memory_manager=memory_manager,
update_memory_on_run=True,
)
agent.print_response("My name is John Doe and I like to play basketball on the weekends.")
agent.print_response("What do I do on weekends?")This instruction asks the model to capture hobbies and omit real names. It does not validate or remove names from writes. If excluding a field is a requirement, enforce that rule in application filtering or validation before storage.
Memories and Context
When enabled, memories about the current user are automatically added to the agent's context on each request. But in some scenarios, like when you're building analytics on memories or want the agent to explicitly search for memories using tools, you might want to store memories without auto-including them.
Use add_memories_to_context=False to collect memories in the background while keeping the agent's context lean:
from agno.agent import Agent
from agno.db.sqlite import SqliteDb
# Setup your database
db = SqliteDb(db_file="agno.db")
# Setup your Agent with Memory
agent = Agent(
db=db,
update_memory_on_run=True, # This enables Memory for the Agent
add_memories_to_context=False, # This disables adding memories to the context
)Memory Optimization
As users accumulate memories over time, and these memories are added to your context on each request, token costs can grow significantly.
Memory optimization asks a model to combine memories into a shorter summary. Token savings and retained facts depend on the generated result. Preview with apply=False and review it before using apply=True, which replaces the user's previous memories. A call with apply=True generates a new summary; it does not apply the exact preview object.
When to optimize:
- Users with 50+ memories
- Before high-cost operations
- Periodic maintenance for long-running applications
from agno.memory import MemoryManager
from agno.memory.strategies.types import MemoryOptimizationStrategyType
from agno.models.openai import OpenAIResponses
memory_manager = MemoryManager(db=db, model=OpenAIResponses(id="gpt-5.2"))
# Optimize memories for a user
optimized = memory_manager.optimize_memories(
user_id="user_123",
strategy=MemoryOptimizationStrategyType.SUMMARIZE,
apply=False, # Generate a preview without replacing stored memories
)See the Memory Optimization guide for detailed usage and best practices.
Using Memory Tools
Instead of automatic memory management, you can give your agent explicit tools to create, retrieve, update, and delete memories. This approach gives the agent more control and reasoning ability, so it can decide when to store something versus when to search for existing memories.
When to use Memory Tools:
- You want the agent to reason about whether something is worth remembering
- You need fine-grained control over memory operations (create, update, delete separately)
- You're building a system where the agent should explicitly search memories rather than having them auto-loaded
from agno.agent import Agent
from agno.db.sqlite import SqliteDb
from agno.models.openai import OpenAIResponses
from agno.tools.memory import MemoryTools
# Create a database connection
db = SqliteDb(
db_file="tmp/memory.db"
)
memory_tools = MemoryTools(
db=db,
)
agent = Agent(
model=OpenAIResponses(id="gpt-5.2"),
tools=[memory_tools],
markdown=True,
)
if __name__ == "__main__":
agent.print_response(
"My name is John Doe and I like to hike in the mountains on weekends. "
"I like to travel to new places and experience different cultures. "
"I am planning to travel to Africa in December. ",
user_id="john_doe@example.com",
stream=True
)
# This won't use the session history, but instead will use the memory tools to get the memories
agent.print_response("What have you remembered about me?", stream=True, user_id="john_doe@example.com")See the Memory Tools documentation for more details.
Sharing Memory Between Agents
In multi-agent systems, you often want agents to share knowledge about users. For example, a support agent might learn a user's preferences, and a sales agent should be aware of them too. This is simple in Agno: just connect multiple agents to the same database.
from agno.agent import Agent
from agno.db.sqlite import SqliteDb
# Setup your database
db = SqliteDb(db_file="agno.db")
# Setup your Agents with the same database and Memory enabled
agent_1 = Agent(db=db, update_memory_on_run=True)
agent_2 = Agent(db=db, update_memory_on_run=True)
# The first Agent will create a Memory about the user name here:
agent_1.print_response("Hi! My name is John Doe")
# The second Agent will be able to retrieve the Memory about the user name here:
agent_2.print_response("What is my name?")All agents connected to the same database automatically share memories for each user. This works across agent types, teams, and workflows, as long as they use the same user_id.