Dynamic Instructions
Generate agent instructions from a function that reads session state, so behavior changes per user.
Build instructions from session state so the agent behaves differently for each user.
Code
from agno.agent import Agent
from agno.run import RunContext
def get_instructions(run_context: RunContext):
if not run_context.session_state:
run_context.session_state = {}
if run_context.session_state.get("current_user_id"):
return f"Make the story about {run_context.session_state.get('current_user_id')}."
return "Make the story about the user."
agent = Agent(instructions=get_instructions)
agent.print_response("Write a 2 sentence story", user_id="john.doe")Usage
Create a Python file
Create dynamic_instructions.py with the code above.
Set up your virtual environment
uv venv --python 3.12
source .venv/bin/activateInstall dependencies
uv pip install -U agno openaiExport your OpenAI API key
export OPENAI_API_KEY="your_openai_api_key_here"Run Agent
python dynamic_instructions.py