feat(routing): cross-key and cross-provider concurrency + rate-limit-aware failure attribution
Overview
Proposes filling three gaps in the routing layer's concurrency model:
- Cross-key concurrency: per-key concurrency budgets should stack independently across the keys of one platform.
- Cross-provider racing (hedged requests): one logical request may race on several providers and take the first valid answer, instead of today's strictly serial fallback walk.
- Adaptive rate-limiting with failure attribution: upstream 429s / connection resets should be attributed to the key/provider moment, not recorded as "this model is bad".
Current state
- Key pooling exists (#915 closed as implemented):
selectKeyForModelwalks every enabled/healthy key of the platform, skipping cooldown, over-quota, over-cap, at-concurrency-ceiling, and out-of-scope keys (server/src/services/router.ts). - Concurrency caps are optional and default to unlimited:
getKeyConcurrencyLimit(platform)(server/src/services/ratelimit.ts:98) reads onlyMAX_CONCURRENT_REQUESTS_PER_KEY_<PLATFORM>/MAX_CONCURRENT_REQUESTS_PER_KEYenv vars; unset →null→ no cap, and no built-in per-platform default. - The fallback walk is strictly serial:
for (const entry of servingChain)(server/src/services/router.ts:2110) tries one candidate at a time; no hedging path exists anywhere in the router. - Rate-limit failures and model-quality failures are recorded identically: every failed attempt lands in
requestsasstatus='error'and feeds the reliability bandit.
Measured evidence (catalog platforms, self-hosted instance)
1) One logical request fans out into several catalog-platform attempts inside the 15s retry budget, and each failure is written as a model sample. From the requests table (single deepseek-v4-flash / glm-5.3 chat requests):
2621 huggingface Qwen/Qwen3.5-122B-A10B error 10223ms
2623 modelscope Qwen/Qwen3.5-397B-A17B canceled 12226ms
2625 huggingface deepseek-ai/DeepSeek-V4-Flash error 10399ms
2628 unorouter glm-5.3:free error 1142msThe canceled row shows an attempt being abandoned mid-flight — work was spent and billed upstream with no sample value.
2) The same burst parked per-model heuristic cooldowns (rate_limit_cooldowns, all source='heuristic', set within one minute):
unorouter glm-5.3:free
huggingface Qwen/Qwen3.5-122B-A10B
huggingface deepseek-ai/DeepSeek-V4-FlashA single slow minute pushed three catalog models into cooldown; the next request that needs them starts from a smaller pool.
3) Heterogeneous failure kinds arrive mixed in one attempt trail, with no attribution split. A single walk can end with e.g. rate_limited ×1, error ×1, model_not_found ×1, out_of_credits ×1 — four different root causes (provider throttling, provider outage, stale catalog row, exhausted quota) recorded as the same status='error' model samples. The bandit cannot tell "model is bad" from "provider had a bad minute".
4) No default concurrency cap (see ratelimit.ts:98 above) — a client that parallelizes aggressively shares one provider-wide bucket with every other request until the provider starts rejecting.
Proposal
- Per-key concurrency budgets that stack: keep
canUseKeyConcurrencyper-key, add sane built-in defaults per known platform (instead of env-only), and document "N keys → N × per-key limit" at key-config time. - Optional hedged racing: a
hedge_after_msknob — if the first candidate hasn't produced a first byte after the threshold, start the next candidate in parallel; first valid response wins, the loser is canceled. Off by default per platform (token-billed providers should not double-bill silently). - Failure attribution: classify 429 / connection-reset / timeout as provider-moment failures (steer the key/provider, do not record against the model), and only 404/410
model_not_foundas model-level evidence.
Scope notes
- Some providers meter quota per account/project rather than per key (e.g. Gemini per project) — cross-key concurrency does not stack there; mark the quota-pool semantics per key (
per-key/per-account) in the key config UI. - Hedging must respect provider-level exclusion (#788): a provider-wide outage should not be amplified by racing into it.
Related
- #915 — multi-key pooling (implemented, closed)
- #919 — least-remaining quota-weighted key selection (implemented, closed)
- #788 — provider-level exclusion
- #1348 — model discovery for built-in providers (sibling issue from the same investigation)
概述
建议补齐路由层并发模型的三个缺口:
- 跨 key 并发:同平台多把 key 时,各 key 的并发预算应独立计算并叠加。
- 跨渠道竞速(hedged request):一个逻辑请求可在多个渠道并行发起、取首个有效响应,而不是现在严格串行的 fallback。
- 带归因的自适应限流:上游 429/连接重置应归因于「该 key/渠道此刻被限流」,而不是记成「这个模型不行」。
现状
- key 池已实现(#915 以"已实现"关闭):
selectKeyForModel会遍历平台上每把 enabled/healthy 的 key,跳过冷却/超额/超平台 cap/达并发上限/作用域不符的 key(server/src/services/router.ts)。 - 并发上限可选,默认不限:
getKeyConcurrencyLimit(platform)(server/src/services/ratelimit.ts:98)只读MAX_CONCURRENT_REQUESTS_PER_KEY_<PLATFORM>/MAX_CONCURRENT_REQUESTS_PER_KEY环境变量;未设置 →null→ 不限制,也没有每平台内置默认值。 - fallback 严格串行:
for (const entry of servingChain)(server/src/services/router.ts:2110)逐个尝试候选,router 中没有任何竞速路径。 - 限流失败与模型质量失败被同等记录:每次失败尝试都会以
status='error'写入requests,并进入可靠性 bandit。
实测证据(catalog 内平台,自托管实例)
1) 一个逻辑请求在 15s 重试预算内扇出到多个 catalog 平台,每次失败都被记成模型样本。 来自 requests 表(单个 deepseek-v4-flash / glm-5.3 聊天请求):
2621 huggingface Qwen/Qwen3.5-122B-A10B error 10223ms
2623 modelscope Qwen/Qwen3.5-397B-A17B canceled 12226ms
2625 huggingface deepseek-ai/DeepSeek-V4-Flash error 10399ms
2628 unorouter glm-5.3:free error 1142mscanceled 那行说明一次尝试在中途被放弃——上游已经消耗了工作,却没有产生任何样本价值。
2) 同一分钟内,这次突发给三个 catalog 模型挂上了启发式冷却(rate_limit_cooldowns,全部 source='heuristic'):
unorouter glm-5.3:free
huggingface Qwen/Qwen3.5-122B-A10B
huggingface deepseek-ai/DeepSeek-V4-Flash慢一分钟就缩小了下一次请求的可用池。
3) 一次尝试链里混杂多种失败类型,没有归因拆分。 单次 walk 可能以 rate_limited ×1, error ×1, model_not_found ×1, out_of_credits ×1 收场——四种不同根因(平台限流、平台故障、目录过期、额度耗尽)被记成同样的 status='error' 模型样本。bandit 无法区分「模型差」和「平台这一分钟不行」。
4) 没有默认并发上限(见上 ratelimit.ts:98)——客户端激进并发时,与其他请求共享同一个平台级桶,直到上游开始拒绝。
建议
- 可叠加的 per-key 并发预算:保留
canUseKeyConcurrency的 per-key 粒度,为已知平台提供合理的内置默认值(而非只靠环境变量),并在 key 配置处标注「N 把 key → N × 单 key 上限」。 - 可选的竞速模式:提供
hedge_after_ms——首候选超过阈值未出首字节时并行启动次候选,先到者胜、其余取消;按平台默认关闭(按 token 计费的平台不应默默双倍扣费)。 - 失败归因:429/连接重置/超时归为「平台瞬时失败」(调整 key/渠道选择,不记入模型),只有 404/410
model_not_found才作为模型级证据。
边界
- 部分平台按账号/项目计量而非按 key(如 Gemini 按 project)——跨 key 并发在这类平台不叠加;需在 key 配置 UI 标注每把 key 的 quota-pool 语义(
per-key/per-account)。 - 竞速须尊重平台级排除(#788):平台级故障时不应通过竞速放大失败。
相关
- #915 — 同平台多 key 池化(已实现并关闭)
- #919 — least-remaining 配额加权 key 选择(已实现并关闭)
- #788 — provider 级整体排除
- #1348 — 内建平台的模型发现路径(同一次调查的姊妹 issue)
Source: tashfeenahmed/freellmapi