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
1from kern.agent import Agent2from kern.db.sqlite import SqliteDb3from kern.models.openai import OpenAIResponses4from kern.tools.yfinance import YFinanceTools56db = SqliteDb(db_file="tmp/agents.db")78agent = Agent(9 model=OpenAIResponses(id="gpt-5.2"),10 tools=[YFinanceTools()],11 db=db,12 add_history_to_context=True,13 num_history_runs=5,14 markdown=True,15)1617session_id = "finance-session"1819# Turn 1: Analyze a stock20agent.print_response(21 "Give me a quick analysis of NVIDIA",22 session_id=session_id,23 stream=True,24)2526# Turn 2: The agent remembers NVDA from turn 127agent.print_response(28 "Compare that to AMD",29 session_id=session_id,30 stream=True,31)3233# Turn 3: Ask based on full conversation34agent.print_response(35 "Which looks like the better investment?",36 session_id=session_id,37 stream=True,38)Set up your virtual environment
1uv venv --python 3.122source .venv/bin/activate1uv venv --python 3.122.venv\Scripts\activateInstall dependencies
1uv pip install -U kern-ai openai yfinance sqlalchemyExport your OpenAI API key
1export OPENAI_API_KEY="your_openai_api_key_here"1$Env:OPENAI_API_KEY="your_openai_api_key_here"Run Agent
1python 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