This example demonstrates how to inject session_state
variables directly into the agent’s instructions using add_state_in_messages
.
Code
cookbook/agent_concepts/state/state_in_prompt.py
from agno.agent import Agent
from agno.models.openai import OpenAIChat
agent = Agent(
model=OpenAIChat(id="gpt-4o-mini"),
# Initialize the session state with a variable
session_state={"user_name": "John"},
# You can use variables from the session state in the instructions
instructions="Users name is {user_name}",
show_tool_calls=True,
add_state_in_messages=True,
markdown=True,
)
agent.print_response("What is my name?", stream=True)
Usage
Create a virtual environment
Open the Terminal
and create a python virtual environment.
python3 -m venv .venv
source .venv/bin/activate
Set your API key
export OPENAI_API_KEY=xxx
Install libraries
pip install -U openai agno
Run Example
python cookbook/agent_concepts/state/state_in_prompt.py