#9427: fix: trigger model fallback on all 4xx HTTP errors
agents
stale
## Summary
Fixes #9297
When an LLM provider responds with a 4xx HTTP error, the system now falls back to the configured alternative model instead of returning the raw error to the user.
## Problem
Previously, only specific 4xx status codes (401, 402, 403, 408, 429) would trigger model fallback. Other 4xx errors like:
- 400 Bad Request (e.g., model not found, invalid endpoint)
- 404 Not Found (e.g., non-existent endpoint)
...would bubble up directly to the user without attempting the fallback.
## Solution
Extended `resolveFailoverReasonFromError()` to treat all 4xx client errors as fallback-worthy. This allows the system to try alternative models when the primary provider returns any client error.
## Changes
- `src/agents/failover-error.ts`: Added catch-all for 4xx status codes
- `src/agents/model-fallback.test.ts`: Added tests for 400 and 404 fallback scenarios
## Testing
- Added unit tests for 400 Bad Request fallback
- Added unit tests for 404 Not Found fallback
<!-- greptile_comment -->
<h2>Greptile Overview</h2>
<h3>Greptile Summary</h3>
This PR broadens model failover triggering by treating any HTTP 4xx response from an LLM provider as failover-worthy, and adds unit coverage for 400/404 scenarios in `runWithModelFallback`.
Core behavior change is implemented in `src/agents/failover-error.ts` via `resolveFailoverReasonFromError()`, which now maps previously-unhandled 4xx status codes into a failover reason that `runWithModelFallback` recognizes, causing it to try configured fallback models instead of surfacing the raw provider error.
<h3>Confidence Score: 3/5</h3>
- This PR is moderately safe to merge once 4xx-to-reason mapping is corrected to avoid misclassifying format errors as auth.
- Change is small and covered by tests for 400/404 fallback, but the implementation reuses the "auth" reason for all 4xx, which can incorrectly affect downstream cooldown/telemetry and user-visible error handling for real 400-format failures.
- src/agents/failover-error.ts
<!-- greptile_other_comments_section -->
<!-- /greptile_comment -->
Most Similar PRs
#22064: fix(failover): bypass models allowlist for configured fallback models
by winston-bepresent · 2026-02-20
85.0%
#21152: fix(agents): throw FailoverError for unknown model so fallback chai...
by Mellowambience · 2026-02-19
84.8%
#21049: fix(failover): treat HTTP 5xx as rate-limit for model fallback
by maximalmargin · 2026-02-19
84.7%
#11349: fix(agents): do not filter fallback models by models allowlist
by liuxiaopai-ai · 2026-02-07
84.2%
#15815: Fallback LLM doesn't trigger if primary model is local
by shihanqu · 2026-02-13
83.6%
#13658: fix: silent model failover with fallback notification
by taw0002 · 2026-02-10
83.4%
#19252: fix(agents): continue model fallback on failover text payloads
by mahsumaktas · 2026-02-17
83.0%
#21017: fix: treat HTTP 502/503/504 as failover-eligible (timeout reason)
by taw0002 · 2026-02-19
82.7%
#12314: fix: treat HTTP 5xx server errors as failover-worthy
by hsssgdtc · 2026-02-09
82.5%
#4462: fix: prevent gateway crash when all auth profiles are in cooldown
by garnetlyx · 2026-01-30
81.9%