跳到主要内容

提供商路由

当使用 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")
信息

提供商路由仅在使用 OpenRouter 时适用。它对直接提供商连接(例如直接连接到 Anthropic API)没有影响。

选项

sort

控制 OpenRouter 如何为你的请求对可用提供商进行排名。

描述
"price"最便宜的提供商优先
"throughput"最高 tokens/秒优先
"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"

优化吞吐量

最适合 tokens/秒重要的长篇生成:

provider_routing:
sort: "throughput"

锁定到特定提供商

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

provider_routing:
only:
- "Anthropic"

避免特定提供商

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

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

首选顺序与后备

首先尝试首选提供商,如果不可用则后备:

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

工作原理

提供商路由偏好通过 extra_body.provider 字段在每个 API 调用时传递给 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
提示

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

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

默认行为

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

提供商路由 vs 备用模型

提供商路由控制哪些OpenRouter 内的子提供商处理你的请求。对于在你的主要模型失败时自动故障转移到完全不同提供商的自动故障转移,请参阅备用提供商