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

# 使用 HTTP 头进行运行时配置

LangGraph 允许通过运行时配置动态修改代理行为和权限。当使用 [LangSmith 部署](/langsmith/deployment-quickstart) 时，您可以在请求体 (`config`) 或特定请求头中传递此配置。这使得基于用户身份或其他请求进行调整成为可能。

出于隐私考虑，您可以通过 [`langgraph.json`](/langsmith/application-structure#configuration-file) 文件中的 `http.configurable_headers` 部分控制哪些头被传递到运行时配置。

以下是自定义包含和排除头的方法：

```json theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
{
  "http": {
    "configurable_headers": {
      "includes": ["x-user-id", "x-organization-id", "my-prefix-*"],
      "excludes": ["authorization", "x-api-key"]
    }
  }
}
```

`includes` 和 `excludes` 列表接受精确的头名称或使用 `*` 匹配任意数量字符的模式。为了您的安全，不支持其他正则表达式模式。

## 在图中使用

您可以使用任何节点的 `config` 参数在图中访问包含的头。

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
def my_node(state, config):
  organization_id = config["configurable"].get("x-organization-id")
  ...
```

或者通过从上下文中获取（在工具中或其他嵌套函数内很有用）。

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
from langgraph.config import get_config

def search_everything(query: str):
  organization_id = get_config()["configurable"].get("x-organization-id")
  ...
```

您甚至可以使用此功能动态编译图。

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
# my_graph.py.
import contextlib

@contextlib.asynccontextmanager
async def generate_agent(config):
  organization_id = config["configurable"].get("x-organization-id")
  if organization_id == "org1":
    graph = ...
    yield graph
  else:
    graph = ...
    yield graph

```

```json theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
{
  "graphs": {"agent": "my_grph.py:generate_agent"}
}
```

### 选择退出可配置头

如果您希望选择退出可配置头，只需在 `excludes` 列表中设置一个通配符模式：

```json theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
{
  "http": {
    "configurable_headers": {
      "excludes": ["*"]
    }
  }
}
```

这将排除所有头被添加到您的运行配置中。

请注意，排除优先于包含。

***

<div className="source-links">
  <Callout icon="terminal-2">
    [将这些文档连接](/use-these-docs)到 Claude、VSCode 等，通过 MCP 获取实时答案。
  </Callout>

  <Callout icon="edit">
    [在 GitHub 上编辑此页面](https://github.com/langchain-ai/docs/edit/main/src/langsmith/configurable-headers.mdx) 或 [提交问题](https://github.com/langchain-ai/docs/issues/new/choose)。
  </Callout>
</div>
