提供商路由

当使用 OpenRouter 作为 LLM 提供商时,Hermes Agent 支持提供商路由——精细控制哪些底层 AI 提供商处理您的请求以及它们的优先级顺序。

OpenRouter 将请求路由到众多提供商(例如 Anthropic、Google、AWS Bedrock、Together AI)。提供商路由让您可以优化成本、速度、质量,或强制特定提供商要求。

配置

~/.hermes/config.yaml 中添加 provider_routing 部分:

provider_routing:
  sort: "price"           # 如何排名提供商
  only: []                # 白名单:仅使用这些提供商
  ignore: []              # 黑名单:从不使用这些提供商
  order: []               # 明确的提供商优先级顺序
  require_parameters: false  # 仅使用支持所有参数的提供商
  data_collection: null   # 控制数据收集("allow" 或 "deny")

:::info 提供商路由仅在使用 OpenRouter 时适用。直接连接提供商(例如直接连接到 Anthropic API)无效。 :::

选项

sort

控制 OpenRouter 如何为您的请求排名可用提供商。

描述
"price"最便宜的提供商优先
"throughput"每秒 token 数最快的优先
"latency"首 token 延迟最低的优先
provider_routing:
  sort: "price"

only

提供商名称的白名单。设置后,使用这些提供商。其他所有提供商均被排除。

provider_routing:
  only:
    - "Anthropic"
    - "Google"

ignore

提供商名称的黑名单。这些提供商永远不会被使用,即使它们提供最便宜或最快的选项。

provider_routing:
  ignore:
    - "Together"
    - "DeepInfra"

order

明确的优先级顺序。列表中的提供商优先。未列出的提供商作为后备。

provider_routing:
  order:
    - "Anthropic"
    - "Google"
    - "AWS Bedrock"

require_parameters

当为 true 时,OpenRouter 仅路由到支持您请求中所有参数(如 temperaturetop_ptools 等)的提供商。这避免了静默参数丢弃。

provider_routing:
  require_parameters: true

data_collection

控制提供商是否可以使用您的提示进行训练。选项为 "allow""deny"

provider_routing:
  data_collection: "deny"

实用示例

优化成本

路由到最便宜的可用提供商。适合高容量使用和开发:

provider_routing:
  sort: "price"

优化速度

优先选择低延迟提供商以实现交互式使用:

provider_routing:
  sort: "latency"

优化吞吐量

最适合长文本生成,每秒 token 数很重要:

provider_routing:
  sort: "throughput"

锁定到特定提供商

确保所有请求通过特定提供商以保持一致性:

provider_routing:
  only:
    - "Anthropic"

避免特定提供商

排除您不想使用的提供商(例如出于数据隐私原因):

provider_routing:
  ignore:
    - "Together"
    - "Lepton"
  data_collection: "deny"

按优先级排序并带后备

先尝试您的首选提供商,如果不可用则退回到其他提供商:

provider_routing:
  order:
    - "Anthropic"
    - "Google"
  require_parameters: true

工作原理

提供商路由偏好通过每次 API 调用的 extra_body.provider 字段传递给 OpenRouter API。这适用于:

  • CLI 模式 — 在 ~/.hermes/config.yaml 中配置,启动时加载
  • 网关模式 — 相同的配置文件,网关启动时加载

路由配置从 config.yaml 读取,并在创建 AIAgent 时作为参数传递:

providers_allowed  ← 来自 provider_routing.only
providers_ignored  ← 来自 provider_routing.ignore
providers_order    ← 来自 provider_routing.order
provider_sort      ← 来自 provider_routing.sort
provider_require_parameters ← 来自 provider_routing.require_parameters
provider_data_collection    ← 来自 provider_routing.data_collection

:::tip 您可以组合多个选项。例如,按价格排序但排除某些提供商并要求参数支持:

provider_routing:
  sort: "price"
  ignore: ["Together"]
  require_parameters: true
  data_collection: "deny"

:::

默认行为

当未配置 provider_routing 部分时(默认情况),OpenRouter 使用自己的默认路由逻辑,通常会平衡成本和可用性。

:::tip 提供商路由 vs. 后备模型 提供商路由控制 OpenRouter 内的子提供商处理您的请求。有关在主模型失败时自动故障转移到完全不同提供商的信息,请参阅后备提供商。 :::