[Feature]: 关于备用模型轮换机制
Version line
v2 — Go rewrite (1.x), main-v2 (active development)
What problem does this solve?
Proposed solution
Reasonix 三类模型 fallback 轮换机制复现方案
本文用于以后软件更新、卸载重装、重新拉取源码后,重新实现本次改造。
目标很窄:只给三类模型增加“失败后按用户配置切换备用模型”的机制。
- 默认模型,也就是 executor 主执行模型
- 独立规划模型,也就是 planner 模型
- 子代理模型,也就是 task / runAs=subagent skill 使用的模型
不要借这个改造重写设置页,不要删除或弱化原有功能。Bots、Hooks、MCP、Skills、Memory、权限、沙箱、网络、外观、更新、状态栏、概览都必须保留。
一、最终行为
- 三类模型各有独立 fallback 列表:
default_fallback_modelsplanner_fallback_modelssubagent_fallback_models
-
fallback 不是轮询、不是负载均衡。健康主模型始终优先使用,只有主模型在首个可见内容前失败,才切备用。
-
可切换失败包括:
- HTTP 408 / 429 / 5xx
- 连接重置
- 首响应前超时,例如
net/http: timeout awaiting response headers - 首 token 前 stream 中断
- 空成功流,也就是只收到 usage / done / EOF,没有任何可见内容
- 不切换失败包括:
- HTTP 400 / 422
- 用户取消
context canceled- 普通
context deadline exceeded - 已经输出可见内容后再报错
注意:net/http: timeout awaiting response headers 可能被 Go 包装得像 context deadline exceeded,但它表示首响应前没有等到响应头,仍属于可切换失败;不要把它和用户取消、上层整体超时混为一类。
- 认证失败的规则:
- 401 / 403 可以跨 provider 或跨
api_key_env切 fallback。 - 不能在同一 provider 且同一
api_key_env上继续撞同一把坏 key。 - 错误信息要能看出 provider/key 边界。
- 失败候选进入短冷却:
- 默认 60 秒。
- 冷却期内跳过该候选,避免每次请求都先撞失败主模型。
- 冷却不是为了延迟切备用;主模型失败后应立刻尝试下一个候选。
- 冷却是为了防止所有候选都失败后,在同一轮里反复回撞失败候选,形成
1 -> 2 -> 1 -> 2的死循环。 - 只有主模型
1+ 备用模型2时,正确流程是:- 先试
1;1在首个可见内容前失败,1进入冷却,并立刻试2。 2成功:本轮使用2完成,不在本轮中途切回1。2也失败:2进入冷却,本轮立即结束并返回明确错误(例如ModelRotationError);不要在同一轮静默等待 60 秒后再试1。- 下一轮用户继续或发送新消息时,再从候选列表开头重新判断:
1冷却已过就优先试1,未过就跳过1试2;如果两者都还在冷却,直接提示稍后重试。
- 先试
-
只有真正切到备用模型,并且备用模型开始输出可见内容时,才提示一次:
已切换到备用模型 provider/model
提示必须包含完整 provider/model ref,不能只写 provider 名。
- usage/cost 要按实际成功模型归属。如果 fallback 成功,usage 事件使用 fallback 模型自己的 pricing,而不是主模型 pricing。
非常需要,支持
This is a good spec — leaving it open as the design of record rather than closing it.
The part I want to underline, because it's the part that makes this safe: failover, not rotation. A healthy primary is always preferred, and a switch only happens before the first visible token. That keeps two things intact that a round-robin would destroy:
- Reproducibility. A turn is answered by one model, not by whichever was up when the request left.
- The prompt cache. Each model has its own prefix cache. Sending alternate turns to alternate models means neither ever gets a warm prefix, and prefix caching is the entire reason this thing is cheap to run. Failover pays that cost only when the primary is actually down, which is the right trade.
Your switch/no-switch split is right, including the two easy things to get wrong: context deadline exceeded is not switchable (it's usually the local deadline, not the provider), and an error after visible output is not switchable (the user already saw a partial answer; restarting it on another model produces two half-answers). I'd add one more to the switchable side: an empty successful stream — usage and [DONE] with no content — which you already have, and which is the failure mode that most looks like a hang.
Worth deciding before implementing:
- Does a fallback stick for the session, or is the primary retried next turn? Sticky avoids re-paying the failure timeout on every turn; non-sticky avoids silently spending the rest of a long session on a model the user didn't choose. I'd go non-sticky with a short cooldown, and say so in the transcript when it happens — a silent model swap is the kind of thing people discover from the bill.
- Where the list lives.
default_fallback_models/planner_fallback_models/subagent_fallback_modelsas you have them is right; they're separate axes andagent.subagent_modelsalready keys per skill. - Interaction with the existing retry. 408/429/5xx are already retried up to 10 times with backoff and
Retry-After. Fallback should engage after that budget, not instead of it, or a single transient 429 will bounce you off your chosen model.
I'm not scheduling this myself, but the design is sound and I'll review a PR that follows it. Keep the diff to the fallback path — the note in your write-up about not rewriting the settings page while you're in there is exactly right.