agent_with_user_memory.py
"""
Agent With User Memory
======================
Demonstrates agent with user memory.
"""
from textwrap import dedent
from agno.agent import Agent
from agno.db.sqlite import SqliteDb
from agno.memory.manager import MemoryManager
from agno.models.google import Gemini
from agno.os.app import AgentOS
from agno.os.interfaces.whatsapp import Whatsapp
from agno.tools.websearch import WebSearchTools
# ---------------------------------------------------------------------------
# Create Example
# ---------------------------------------------------------------------------
agent_db = SqliteDb(db_file="tmp/persistent_memory.db")
memory_manager = MemoryManager(
memory_capture_instructions="""\
Collect User's name,
Collect Information about user's passion and hobbies,
Collect Information about the users likes and dislikes,
Collect information about what the user is doing with their life right now
""",
model=Gemini(id="gemini-flash-latest"),
)
personal_agent = Agent(
name="Basic Agent",
model=Gemini(id="gemini-flash-latest"),
tools=[WebSearchTools()],
add_history_to_context=True,
num_history_runs=3,
add_datetime_to_context=True,
markdown=True,
db=agent_db,
memory_manager=memory_manager,
enable_agentic_memory=True,
instructions=dedent("""
You are a personal AI friend of the user, your purpose is to chat with the user about things and make them feel good.
First introduce yourself and ask for their name then, ask about themeselves, their hobbies, what they like to do and what they like to talk about.
Use DuckDuckGo search tool to find latest information about things in the conversations
"""),
)
# Setup our AgentOS app
agent_os = AgentOS(
agents=[personal_agent],
interfaces=[Whatsapp(agent=personal_agent)],
)
app = agent_os.get_app()
# ---------------------------------------------------------------------------
# Run Example
# ---------------------------------------------------------------------------
if __name__ == "__main__":
agent_os.serve(app="agent_with_user_memory:app", reload=True)
Run the Example
1
Set up your virtual environment
uv venv --python 3.12
source .venv/bin/activate
uv venv --python 3.12
.venv\Scripts\activate
2
Install dependencies
uv pip install -U "agno[os]" ddgs google-genai
3
Export environment variables
export GOOGLE_API_KEY="your_google_api_key_here"
export WHATSAPP_ACCESS_TOKEN="your_whatsapp_access_token_here"
export WHATSAPP_APP_SECRET="your_whatsapp_app_secret_here"
export WHATSAPP_PHONE_NUMBER_ID="your_whatsapp_phone_number_id_here"
export WHATSAPP_VERIFY_TOKEN="your_whatsapp_verify_token_here"
$Env:GOOGLE_API_KEY="your_google_api_key_here"
$Env:WHATSAPP_ACCESS_TOKEN="your_whatsapp_access_token_here"
$Env:WHATSAPP_APP_SECRET="your_whatsapp_app_secret_here"
$Env:WHATSAPP_PHONE_NUMBER_ID="your_whatsapp_phone_number_id_here"
$Env:WHATSAPP_VERIFY_TOKEN="your_whatsapp_verify_token_here"
4
Run the example
Save the code above as
agent_with_user_memory.py, then run:python agent_with_user_memory.py