State in Instructions

This example demonstrates how to use session state variables directly in team instructions using template syntax. The session state values are automatically injected into the instructions, making them available to the team during execution.

Create a Python file

1from kern.team.team import Team
2
3 team = Team(
4 members=[],
5 # Initialize the session state with a variable
6 session_state={"user_name": "John"},
7 instructions="Users name is {user_name}",
8 markdown=True,
9 )
10
11team.print_response("What is my name?", stream=True)

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 session_state_in_instructions.py