multi_user_multi_session.py
"""
Multi-User Multi-Session
========================
Demonstrates handling multiple users and sessions with SQLite-backed agent storage.
"""
from agno.agent import Agent
from agno.db.sqlite import SqliteDb
from agno.models.openai import OpenAIChat
# ---------------------------------------------------------------------------
# Setup
# ---------------------------------------------------------------------------
db = SqliteDb(db_file="tmp/data.db")
user_1_id = "user_101"
user_2_id = "user_102"
user_1_session_id = "session_101"
user_2_session_id = "session_102"
# ---------------------------------------------------------------------------
# Create Agent
# ---------------------------------------------------------------------------
agent = Agent(
model=OpenAIChat(id="gpt-5.2"),
db=db,
update_memory_on_run=True,
add_history_to_context=True,
num_history_runs=3,
)
# ---------------------------------------------------------------------------
# Run Agent
# ---------------------------------------------------------------------------
if __name__ == "__main__":
# Start the session with user 1
agent.print_response(
"Tell me a 5 second short story about a robot.",
user_id=user_1_id,
session_id=user_1_session_id,
)
# Continue the session with user 1
agent.print_response(
"Now tell me a joke.", user_id=user_1_id, session_id=user_1_session_id
)
# Start the session with user 2
agent.print_response(
"Tell me about quantum physics.",
user_id=user_2_id,
session_id=user_2_session_id,
)
# Continue the session with user 2
agent.print_response(
"What is the speed of light?", user_id=user_2_id, session_id=user_2_session_id
)
# Ask the agent to give a summary of the conversation, this will use the history from the previous messages
agent.print_response(
"Give me a summary of our conversation.",
user_id=user_1_id,
session_id=user_1_session_id,
)
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 openai sqlalchemy
3
Export your OpenAI API key
export OPENAI_API_KEY="your_openai_api_key_here"
$Env:OPENAI_API_KEY="your_openai_api_key_here"
4
Run the example
Save the code above as
multi_user_multi_session.py, then run:python multi_user_multi_session.py