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 Agent
2 from kern.db.sqlite import SqliteDb
3 from kern.models.openai import OpenAIResponses
4 from kern.team.team import Team
5
6 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 state
13 enable_agentic_state=True,
14 )
15
16 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 state
21 enable_agentic_state=True,
22 description="You are a team that manages a shopping list and chores",
23 show_members_responses=True,
24 )
25
26
27 team.print_response("Add milk, eggs, and bread to the shopping list")
28
29 team.print_response("I picked up the eggs, now what's on my list?")
30
31print(f"Session state: {team.get_session_state()}")

Set up your virtual environment

1uv venv --python 3.12
2source .venv/bin/activate
1uv venv --python 3.12
2.venv\Scripts\activate

Install dependencies

1uv pip install -U kern-ai openai

Export 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