提供商路由(Provider Routing)
在使用 OpenRouter 作为 LLM 提供商时,Hermes Agent 支持提供商路由——精细控制哪些底层 AI 提供商处理你的请求,以及它们的优先级。
OpenRouter 将请求路由到多个提供商(如 Anthropic、Google、AWS Bedrock、Together AI)。提供商路由让你可以优化成本、速度、质量,或强制指定特定提供商要求。
配置
在 ~/.hermes/config.yaml 中添加 provider_routing 部分:
provider_routing:
sort: "price" # How to rank providers
only: [] # Whitelist: only use these providers
ignore: [] # Blacklist: never use these providers
order: [] # Explicit provider priority order
require_parameters: false # Only use providers that support all parameters
data_collection: null # Control data collection ("allow" or "deny")
提供商路由仅在使用 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 仅路由到支持你请求中所有参数(如 temperature、top_p、tools 等)的提供商。这能避免参数被静默丢弃。
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 ← from provider_routing.only
providers_ignored ← from provider_routing.ignore
providers_order ← from provider_routing.order
provider_sort ← from provider_routing.sort
provider_require_parameters ← from provider_routing.require_parameters
provider_data_collection ← from provider_routing.data_collection
可以组合多个选项。例如,按价格排序,但排除某些提供商并要求参数支持:
provider_routing:
sort: "price"
ignore: ["Together"]
require_parameters: true
data_collection: "deny"
默认行为
当没有配置 provider_routing 部分时(默认情况),OpenRouter 使用自己的默认路由逻辑,通常会自动平衡成本和可用性。
提供商路由控制 OpenRouter 内部哪些子提供商处理你的请求。如需在主要模型失败时自动切换到完全不同的提供商,请参阅备用提供商。