This example demonstrates how to create dynamic instructions that change based on session state, allowing personalized agent behavior for different users.

Code

cookbook/agents/context_management/dynamic_instructions.py
from agno.agent import Agent


def get_instructions(session_state):
    if session_state and session_state.get("current_user_id"):
        return f"Make the story about {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

1

Create a virtual environment

Open the Terminal and create a python virtual environment.
python3 -m venv .venv
source .venv/bin/activate
2

Install libraries

pip install -U agno
3

Run Agent

python cookbook/agents/context_management/dynamic_instructions.py