Copy
Ask AI
"""
Share Memory and History Between Agents
=======================================
This example shows two agents sharing both conversation history and user memory
through a common database, user ID, and session ID.
"""
from uuid import uuid4
from agno.agent import Agent
from agno.db.sqlite import SqliteDb
from agno.models.openai.chat import OpenAIChat
# ---------------------------------------------------------------------------
# Setup
# ---------------------------------------------------------------------------
db = SqliteDb(db_file="tmp/agent_sessions.db")
# ---------------------------------------------------------------------------
# Create Agents
# ---------------------------------------------------------------------------
agent_1 = Agent(
model=OpenAIChat(id="gpt-4o-mini"),
instructions="You are really friendly and helpful.",
db=db,
add_history_to_context=True,
update_memory_on_run=True,
)
agent_2 = Agent(
model=OpenAIChat(id="gpt-4o-mini"),
instructions="You are really grumpy and mean.",
db=db,
add_history_to_context=True,
update_memory_on_run=True,
)
# ---------------------------------------------------------------------------
# Run Agents
# ---------------------------------------------------------------------------
if __name__ == "__main__":
session_id = str(uuid4())
user_id = "[email protected]"
agent_1.print_response(
"Hi! My name is John Doe.", session_id=session_id, user_id=user_id
)
agent_2.print_response("What is my name?", session_id=session_id, user_id=user_id)
agent_2.print_response(
"I like to hike in the mountains on weekends.",
session_id=session_id,
user_id=user_id,
)
agent_1.print_response(
"What are my hobbies?", session_id=session_id, user_id=user_id
)
agent_1.print_response(
"What have we been discussing? Give me bullet points.",
session_id=session_id,
user_id=user_id,
)
Run the Example
Copy
Ask AI
# Clone and setup repo
git clone https://github.com/agno-agi/agno.git
cd agno/cookbook/11_memory
# Create and activate virtual environment
./scripts/demo_setup.sh
source .venvs/demo/bin/activate
python 07_share_memory_and_history_between_agents.py