跳到主要内容
LangGraph 允许在运行时配置动态修改代理行为和权限。当使用 LangSmith 部署时,您可以在请求正文 (config) 或特定请求头中传递此配置。这使得可以根据用户身份或其他请求进行调整。 为了隐私,通过 langgraph.json 文件中的 http.configurable_headers 部分控制哪些标头传递给运行时配置。 以下是如何自定义包含和排除的标头:
{
  "http": {
    "configurable_headers": {
      "include": ["x-user-id", "x-organization-id", "my-prefix-*"],
      "exclude": ["authorization", "x-api-key"]
    }
  }
}
includeexclude 列表接受精确的标头名称或使用 * 匹配任意数量字符的模式。为了您的安全,不支持其他正则表达式模式。

在图中使用

您可以使用任何节点的 config 参数在图中访问包含的标头。
def my_node(state, config):
  organization_id = config["configurable"].get("x-organization-id")
  ...
或者通过从上下文获取(在工具或在其他嵌套函数中很有用)。
from langgraph.config import get_config

def search_everything(query: str):
  organization_id = get_config()["configurable"].get("x-organization-id")
  ...
您甚至可以使用它来动态编译图。
# 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

{
  "graphs": {"agent": "my_grph.py:generate_agent"}
}

选择退出可配置标头

如果您想选择退出可配置标头,您只需在 exclude 列表中设置一个通配符模式
{
  "http": {
    "configurable_headers": {
      "exclude": ["*"]
    }
  }
}
这将排除所有标头添加到您的运行配置中。 请注意,排除优先于包含。
以编程方式连接这些文档到 Claude、VSCode 等,通过 MCP 获取实时答案。
© . This site is unofficial and not affiliated with LangChain, Inc.