> ## Documentation Index
> Fetch the complete documentation index at: https://agno-v2.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# 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

```python dynamic_instructions.py theme={null}
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 Python file**

Create `dynamic_instructions.py` with the code above.

<Snippet file="create-venv-step.mdx" />

**2. Install dependencies**

```bash theme={null}
uv pip install -U agno openai
```

**3. Export your OpenAI API key**

```bash Mac/Linux theme={null}
export OPENAI_API_KEY="your_openai_api_key_here"
```

```bash Windows theme={null}
$Env:OPENAI_API_KEY="your_openai_api_key_here"
```

**4. Run Agent**

```bash theme={null}
python dynamic_instructions.py
```


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.