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

# 在指令中注入状态

此示例演示了如何使用 `add_state_in_messages` 将 `session_state` 变量直接注入代理的指令中。

## 代码

```python cookbook/agent_concepts/state/state_in_prompt.py theme={null}
from agno.agent import Agent
from agno.models.openai import OpenAIChat

agent = Agent(
    model=OpenAIChat(id="gpt-4o-mini"),
    # 使用变量初始化 session state
    session_state={"user_name": "John"},
    # 您可以在指令中使用来自 session state 的变量
    instructions="用户的名字叫 {user_name}",
    show_tool_calls=True,
    add_state_in_messages=True,
    markdown=True,
)

agent.print_response("What is my name?", stream=True)
```

## 使用方法

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

  <Step title="设置您的 API 密钥">
    ```bash theme={null}
    export OPENAI_API_KEY=xxx
    ```
  </Step>

  <Step title="安装库">
    ```bash theme={null}
    pip install -U openai agno
    ```
  </Step>

  <Step title="运行示例">
    <CodeGroup>
      ```bash Mac theme={null}
      python cookbook/agent_concepts/state/state_in_prompt.py
      ```

      ```bash Windows theme={null}
      python cookbook/agent_concepts/state/state_in_prompt.py
      ```
    </CodeGroup>
  </Step>
</Steps>
