Copy
Ask AI
"""
Session State Basic
=============================
Session State Basic.
"""
from agno.agent import Agent, RunOutput # noqa
from agno.db.sqlite import SqliteDb
from agno.models.openai import OpenAIResponses
from agno.run import RunContext
def add_item(run_context: RunContext, item: str) -> str:
"""Add an item to the shopping list."""
if run_context.session_state is None:
run_context.session_state = {}
run_context.session_state["shopping_list"].append(item) # type: ignore
return f"The shopping list is now {run_context.session_state['shopping_list']}" # type: ignore
# Create an Agent that maintains state
# ---------------------------------------------------------------------------
# Create Agent
# ---------------------------------------------------------------------------
agent = Agent(
model=OpenAIResponses(id="gpt-5-mini"),
# Initialize the session state with a counter starting at 0 (this is the default session state for all users)
session_state={"shopping_list": []},
db=SqliteDb(db_file="tmp/agents.db"),
tools=[add_item],
# You can use variables from the session state in the instructions
instructions="Current state (shopping list) is: {shopping_list}",
markdown=True,
)
# ---------------------------------------------------------------------------
# Run Agent
# ---------------------------------------------------------------------------
if __name__ == "__main__":
# Example usage
agent.print_response("Add milk, eggs, and bread to the shopping list", stream=True)
print(f"Final session state: {agent.get_session_state()}")
# Alternatively,
# response: RunOutput = agent.run("Add milk, eggs, and bread to the shopping list")
# print(f"Final session state: {response.session_state}")
Run the Example
Copy
Ask AI
# Clone and setup repo
git clone https://github.com/agno-agi/agno.git
cd agno/cookbook/02_agents/05_state_and_session
# Create and activate virtual environment
./scripts/demo_setup.sh
source .venvs/demo/bin/activate
python session_state_basic.py