Skip to main content

Documentation Index

Fetch the complete documentation index at: https://spacesail.mintlify.app/llms.txt

Use this file to discover all available pages before exploring further.

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

Code

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

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

Export your OpenAI API key

  export OPENAI_API_KEY="your_openai_api_key_here"
4

Create a Python file

Create a Python file and add the above code.
touch dynamic_instructions.py
5

Run Agent

python dynamic_instructions.py
6

Find All Cookbooks

Explore all the available cookbooks in the Agno repository. Click the link below to view the code on GitHub:Agno Cookbooks on GitHub