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 Team23 team = Team(4 members=[],5 # Initialize the session state with a variable6 session_state={"user_name": "John"},7 instructions="Users name is {user_name}",8 markdown=True,9 )1011team.print_response("What is my name?", stream=True)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 session_state_in_instructions.py