[Bug]: With fallbackModel configured, every reasoning call re-attempts the failed primary and re-spends the full retry budget
Describe the bug
When a fallback model is configured, ReActAgent builds a fresh fallback wrapper for every model call (ReActAgent#reasoningStream() invokes modelForCall() once per reasoning iteration). Each wrapper starts with the primary model as active and always attempts the primary first — the wrapper and its active-model state are recreated per call, so even the fact that the primary already failed earlier in the same run is forgotten.
The retry budget is spent inside the primary's own stream(): buildGenerateOptions() layers ModelConfig#maxRetries (default 3) as ExecutionConfig#maxAttempts, and ModelUtils#applyTimeoutAndRetry() applies it with exponential backoff (defaults: 1s initial / 10s max / 0.5 jitter) — the default retryOn predicate retries all errors, including 429. Only after that budget is exhausted does switchOnFirst see the error and switch to the fallback.
Net effect: while the primary is saturated or down, every reasoning iteration of a run burns up to maxRetries requests plus backoff delays before falling back — a run with tool calls multiplies that waste across iterations. The framework is the only component that sees every attempt; the application has no way to tell it "this model is on cooldown, skip it".
To Reproduce
Steps to reproduce the behavior:
- Configure a
ReActAgentwith a primary model whose calls fail (an endpoint returning 429, or aModelstub whosestream()errors immediately) and a workingfallbackModel; leavemaxRetriesat the default.
ReActAgent agent = ReActAgent.builder()
.name("demo")
.model(failingPrimary) // stream() errors immediately
.fallbackModel(workingModel)
.build();
- Run a prompt that requires tool calls (several reasoning iterations), e.g.
agent.streamEvents(messages).blockLast(). - Observe the logs: one full retry-then-switch group per iteration (see error messages below).
Expected behavior
Within a single run, once the primary has failed — and certainly while application-level state (e.g. a cooldown) says it is unavailable — subsequent model calls should not re-pay the full retry budget against it. The framework should expose a pre-attempt decision point, e.g. a shouldSkip(Model) method on the FailoverPolicy hook proposed in #3142:
/** Consulted before attempting the primary model; true = skip straight to the fallback. */
default boolean shouldSkip(Model model) {
return false;
}
Semantics that keep it a mechanism rather than a policy:
- consulted at the wrapper's
stream()entry for the primary only — the fallback remains the unconditional last resort; - on skip, the wrapper must set its active model to the fallback, so
getModelName()keeps reporting the model that actually serves the call (per-chunk attribution relies on this); - a skip is not a failed attempt: it does not fire
onFailover(no attempt, no failure; the cooldown state is the implementation's own); - an exception thrown by the implementation is treated as
false; - no policy configured (the default) behaves exactly as today.
Error messages
No exception reaches the caller — the symptom is repeated warning groups in the logs, one group per reasoning iteration (defaults: maxRetries = 3 → 1 initial attempt + 2 retries):
WARN Retrying model request (attempt 1/2) due to: <primary error>
WARN Retrying model request (attempt 2/2) due to: <primary error>
WARN Primary model <primary> failed, switching to fallback <fallback>
Environment (please complete the following information:)
- AgentScope-Java Version: 2.0.3-SNAPSHOT (main, commit
7d321e2b) - Java Version: 21
- OS: Windows
Additional context
Companion feature request: #3142 (failover observability hook). Both needs are served by one small FailoverPolicy interface with no-op defaults.
Withdrawing this one. On reflection, re-attempting the primary on each call is a defensible stateless default — the framework cannot distinguish a transient blip from sustained saturation, and any TTL-based memory would be node-local policy. Our actual need (feeding a cooldown that steers model selection on the next run) is fully covered by the observability hook proposed in #3142.