Agent with Storage
Persist conversation history across runs.
Storage lets your agent remember conversations. With the same session_id, it picks up where you left off, even after restarting.
Create a Python file
from agno.agent import Agent
from agno.db.sqlite import SqliteDb
from agno.models.openai import OpenAIResponses
from agno.tools.yfinance import YFinanceTools
db = SqliteDb(db_file="tmp/agents.db")
agent = Agent(
model=OpenAIResponses(id="gpt-5.2"),
tools=[YFinanceTools(all=True)],
db=db,
add_history_to_context=True,
num_history_runs=5,
markdown=True,
)
session_id = "finance-session"
# Turn 1: Analyze a stock
agent.print_response(
"Give me a quick analysis of NVIDIA",
session_id=session_id,
stream=True,
)
# Turn 2: The agent remembers NVDA from turn 1
agent.print_response(
"Compare that to AMD",
session_id=session_id,
stream=True,
)
# Turn 3: Ask based on full conversation
agent.print_response(
"Which looks like the better investment?",
session_id=session_id,
stream=True,
)Set up your virtual environment
uv venv --python 3.12
source .venv/bin/activateInstall dependencies
uv pip install -U agno openai yfinance sqlalchemyExport your OpenAI API key
export OPENAI_API_KEY="your_openai_api_key_here"Run Agent
python agent_with_storage.pyKey Concepts
- Session: A conversation thread identified by
session_id - Same
session_id= continuous conversation, even across script runs add_history_to_context=True: Includes previous messages in contextnum_history_runs=5: Number of previous runs to include