Agentic State
This example demonstrates how to enable agentic session state in teams and agents, allowing them to automatically manage and update their session state during interactions. The agents can modify the session state autonomously based on the conversation context.
Create a Python file
1from kern.agent import Agent2 from kern.db.sqlite import SqliteDb3 from kern.models.openai import OpenAIResponses4 from kern.team.team import Team56 db = SqliteDb(db_file="tmp/agents.db")7 shopping_agent = Agent(8 name="Shopping List Agent",9 role="Manage the shopping list",10 model=OpenAIResponses(id="gpt-5.2"),11 db=db,12 add_session_state_to_context=True, # Required so the agent is aware of the session state13 enable_agentic_state=True,14 )1516 team = Team(17 members=[shopping_agent],18 session_state={"shopping_list": []},19 db=db,20 add_session_state_to_context=True, # Required so the team is aware of the session state21 enable_agentic_state=True,22 description="You are a team that manages a shopping list and chores",23 show_members_responses=True,24 )252627 team.print_response("Add milk, eggs, and bread to the shopping list")2829 team.print_response("I picked up the eggs, now what's on my list?")3031print(f"Session state: {team.get_session_state()}")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 openaiExport your OpenAI API key
Set OpenAI Key
Set your OPENAI_API_KEY as an environment variable. You can get one from OpenAI.
1export OPENAI_API_KEY=sk-***1setx OPENAI_API_KEY sk-***Run Team
1python agentic_session_state.py