[Bug] v3.1.x global Router.fallback: chain executes but the cross-provider fallback leg fails upstream with 401 (wrong credentials) — works on 3.0.22

Author: 2457205703Created Sep 26, 2026Updated Sep 26, 2026

Global fallback (default failure handling) executes the chain, but the fallback leg's upstream request is broken

Quick summary: On v3.1.1, the global Router.fallback (routing page → 默认失败处理 = 失败降级目标) does fire — response headers prove attempt #2 was dispatched to the fallback target — but that attempt fails upstream with HTTP 401, because it appears to reuse the primary's resolved context instead of re-resolving credentials per candidate. Direct requests to the same fallback provider succeed at the same moment. Rolling back to 3.0.22 with the identical config makes global fallback work correctly. Related: #1804 (rule-level fallback, same 3.1.0 regression family), #1394.

Environment

  • Claude Code Router v3.1.1 (npm global install, Windows 11), later rolled back to v3.0.22
  • Gateway http://127.0.0.1:3456, client = Claude Code (anthropic protocol)
  • Providers: Kimi Code - Coding Plan (model k3) + glm (zhipu bigmodel, model glm-5.3), both anthropic_messages endpoints

Config (no custom routing rules — global fallback only)

json
"Router": {
  "fallback": { "mode": "model-chain", "models": ["glm/glm-5.3"], "retryCount": 1 },
  "rules": []
}

Repro on 3.1.1

  1. Set the kimi provider's API key to an invalid value.
  2. curl -sS -D - http://127.0.0.1:3456/v1/messages -H "Authorization: Bearer " -H "content-type: application/json" -H "anthropic-version: 2023-06-01" -d '{"model":"k3","max_tokens":16,"messages":[{"role":"user","content":"hi"}]}'

Result: 401 {"error":{"message":"All target providers failed. ..."}} with:

  • x-ccr-fallback-attempts: 2, x-ccr-fallback-failures: 401
  • x-ccr-fallback-model: provider-glm-::anthropic_messages/glm-5.3 ← the chain did resolve the glm target
  • the error body's attempts[] only contains the kimi 401 — the fallback leg's failure detail is missing

usage_events proves both legs ran inside the same request_id: k3 → 401 (~1–2s) then glm → 401 (~2–4s), while concurrent direct requests to glm return 200 (same process, same provider, same key). So the fallback leg reaches bigmodel but its upstream request is built differently (stale credentials from the primary / wrong auth handling) — consistent with the re-resolve-per-candidate property described in #1804.

Also: ccr serve prints Using compatibility gateway server: router-fallback — from source, any fallback config (global / rule-level / profile-routing) forces ALL traffic onto the compatibility server because the new gateway doesn't honor Router.fallback yet. So the whole feature rides on the compat path in 3.1.x.

Same test on 3.0.22 (only version changed)

Invalid kimi key + one hi in Claude Code:

  • request logged as model=Kimi Code - Coding Plan/k3, provider=glm, HTTP 200, 7.6s — fallback worked
  • after restoring the key, the same request goes direct to k3 (200)

Bonus: request-log readability regression

3.0.22 logs clean provider names with requested-model vs actual-provider as separate columns (fallback is visible at a glance). 3.1.1 shows generated ids like provider-glm-03923f02d6::anthropic_messages, which makes debugging fallback much harder.

Suggestion

Fix per-attempt re-resolution in the compatibility path (or let the main gateway honor Router.fallback) before keeping the feature exposed in the routing UI. Happy to provide more traces if needed.

Source: musistudio/claude-code-router