Repository navigation
Open
Labels
Description
Motivation
Model failover today is single-level: ReActAgent.Builder.fallbackModel(...) accepts exactly one backup model, and modelForCall() switches to it on the first error signal of the primary stream. In production, a provider outage or a revoked API key commonly takes down more than one model of the same vendor, so a single fallback is frequently exhausted too.
The concrete gaps this feature addresses:
- Single-layer fallback — the fallback itself has no further escape hatch; when both primary and fallback fail the request is lost even if a third model would work.
- No failure classification — a request-side error (400/422), which will fail identically on every candidate, still switches and burns the fallback budget.
- No cooldown — a persistently broken model is hammered on every request until the agent is rebuilt; there is no "skip the known-bad model for a while" state.
- No transparency — callers cannot observe which candidate actually served the request (
getModelName()reports the primary even after a switch) or that a cooldown is active.
Proposal
Add a pluggable, opt-in multi-level fallback chain as a public Model wrapper:
FallbackChainModel(new public class inio.agentscope.core.model): wraps an ordered list[primary, fb1, fb2, ...]; on a failure that switching can recover from (429 / 5xx / timeout / network / 401 / 403) it transparently moves to the next candidate; on a request-side failure (400/422 and other 4xx) it fails fast without consuming the chain.- Per-candidate cooldown: after a failure a candidate is skipped for a cooldown window (default 30s), then automatically becomes eligible again — lazy recovery verified by real traffic, no scheduler or background threads (keeps the core library lightweight).
- Mid-stream failures (first chunk already delivered) are deliberately not retried on a fallback — switching mid-response can duplicate delivered content; the failure is recorded (cooldown applied) and propagated as-is.
- Capability delegation:
getModelName()/supportsNativeStructuredOutput()/getContextWindowSize()report the currently active candidate. - Builder switch
ReActAgent.Builder.fallbackModels(Model...): when configured,modelForCall()wires the chain; when unset, behaviour is byte-for-byte the existing single-fallback path (zero default-change, backward compatible). Users can also bypass the builder and wiremodel(new FallbackChainModel(primary, fallbacks))directly. - Agent-scoped shared cooldown table: cooldown survives across the many reasoning rounds of a loop and successive requests (each
modelForCall()creates a fresh wrapper but they share the agent's cooldown table), while the active-model reference stays per-call for concurrency safety.
API shape (draft)
ReActAgent.builder()
.model(primary)
.fallbackModels(
List.of(
ModelRegistry.resolve("dashscope:qwen-max"),
ModelRegistry.resolve("openai:gpt-4o-mini")))
.build();
// or directly:
Model m = new FallbackChainModel(primary, fallbacks); // default 30s cooldown
Model m2 = new FallbackChainModel(primary, fallbacks, Duration.ofSeconds(10));Compatibility
- Legacy
fallbackModel(Model/String)andmaxRetries(int)untouched. ModelConfigrecord untouched (zero breaking change).- No new dependencies, no threads, no external state.
- Designed so a future explicit health-probe (issue follow-up) can drive recovery instead of lazy traffic verification without API changes.
Tests
FallbackChainModelTest(12 cases): primary success, switch on switchable error, full-chain exhaustion, 401/403 switch, timeout/network switch, 400 fail-fast, mid-stream no-switch + cooldown, cooldown skip + lazy recovery, shared-table persistence across wrappers, null/empty handling.ReActAgentFallbackChainTest(4 integration cases): agent falls back on 503, primary-only default, fail-fast does not consume chain, direct wrapper wiring via.model(...).
Related
- Paves the way for [Feature]: Expose typed model retry and fallback attempt events #2136 (typed retry/fallback attempt events) by making the active candidate observable through
getModelName().
Activity
feat(core): add multi-level model fallback chain (fallbackModels)
oss-maintainer commentedon Sep 14, 2026
Issue Evaluation
Category:
enhancement| Area:area/core/model| Status: Evaluated — feasibleFeasibility: feasible. Verified on current
main:ModelConfig/ReActAgent.Builderexpose a singlefallbackModel, andmodelForCall()switches to it on the first error signal of the primary stream — no failure classification, no cooldown, no multi-candidate chain. PR #3145 mergedFailoverListener(observability), which the proposed design can build on; the remaining gaps are real.Scope:
agentscope-coremodel/agent layer (builder, model selection, failover hooks); extension providers unaffected if the chain stays at theModelabstraction.Compatibility: backward compatible if
fallbackModel(...)is retained as the 1-element case of afallbackModels(...)list.Recommendation: proceed — the error classification policy (which failures disqualify a candidate) and per-candidate cooldown semantics should be specified up front.
Automated evaluation by github-manager-bot
feat(core): add multi-level model fallback chain (fallbackModels)