Examples
- Examples
- Getting Started
- Agents
- Teams
- Workflows
- Applications
- Streamlit Apps
- Evals
Agent Concepts
- Reasoning
- Multimodal
- RAG
- User Control Flows
- Knowledge
- Memory
- Built-in Memory
- Standalone Memory Operations
- Persistent Memory with SQLite
- Custom Memory Creation
- Memory Search
- Agent With Memory
- Agentic Memory
- Agent with Session Summaries
- Multiple Agents Sharing Memory
- Custom Memory
- Multi-User Multi-Session Chat
- Multi-User Multi-Session Chat Concurrent
- Memory References
- Session Summary References
- Mem0 Memory
- DB
- Async
- Hybrid Search
- Storage
- Tools
- Vector Databases
- Context
- Embedders
- Agent State
- Observability
- Miscellaneous
Models
- Anthropic
- AWS Bedrock
- AWS Bedrock Claude
- Azure AI Foundry
- Azure OpenAI
- Cerebras
- Cerebras OpenAI
- Cohere
- DeepInfra
- DeepSeek
- Fireworks
- Gemini
- Groq
- Hugging Face
- IBM
- LM Studio
- LiteLLM
- LiteLLM OpenAI
- Meta
- Mistral
- NVIDIA
- Ollama
- OpenAI
- Perplexity
- Together
- XAI
- Vercel
- vLLM
Memory
Agent With Memory
This example shows you how to use persistent memory with an Agent.
After each run, user memories are created/updated.
To enable this, set enable_user_memories=True
in the Agent config.
Code
cookbook/agent_concepts/memory/06_agent_with_memory.py
Copy
Ask AI
from agno.agent.agent import Agent
from agno.memory.v2.db.sqlite import SqliteMemoryDb
from agno.memory.v2.memory import Memory
from agno.models.openai import OpenAIChat
from agno.storage.sqlite import SqliteStorage
from rich.pretty import pprint
from utils import print_chat_history
memory_db = SqliteMemoryDb(table_name="memory", db_file="tmp/memory.db")
# No need to set the model, it gets set by the agent to the agent's model
memory = Memory(db=memory_db)
# Reset the memory for this example
memory.clear()
session_id = "session_1"
john_doe_id = "john_doe@example.com"
agent = Agent(
model=OpenAIChat(id="gpt-4o-mini"),
memory=memory,
storage=SqliteStorage(
table_name="agent_sessions", db_file="tmp/persistent_memory.db"
),
enable_user_memories=True,
)
agent.print_response(
"My name is John Doe and I like to hike in the mountains on weekends.",
stream=True,
user_id=john_doe_id,
session_id=session_id,
)
agent.print_response(
"What are my hobbies?", stream=True, user_id=john_doe_id, session_id=session_id
)
# -*- Print the chat history
session_run = memory.runs[session_id][-1]
print_chat_history(session_run)
memories = memory.get_user_memories(user_id=john_doe_id)
print("John Doe's memories:")
pprint(memories)
agent.print_response(
"Ok i dont like hiking anymore, i like to play soccer instead.",
stream=True,
user_id=john_doe_id,
session_id=session_id,
)
# -*- Print the chat history
session_run = memory.runs[session_id][-1]
print_chat_history(session_run)
# You can also get the user memories from the agent
memories = agent.get_user_memories(user_id=john_doe_id)
print("John Doe's memories:")
pprint(memories)
Usage
1
Create a virtual environment
Open the Terminal
and create a python virtual environment.
Copy
Ask AI
python3 -m venv .venv
source .venv/bin/activate
2
Set your API key
Copy
Ask AI
export GOOGLE_API_KEY=xxx
3
Install libraries
Copy
Ask AI
pip install -U agno google-generativeai
4
Run Example
Copy
Ask AI
python cookbook/agent_concepts/memory/06_agent_with_memory.py
Was this page helpful?
Assistant
Responses are generated using AI and may contain mistakes.