Skip to content

fix(router): skip retry backoff when a fallback can take over - #42450

Open
RosieOh wants to merge 5 commits into
BerriAI:mainfrom
RosieOh:fix-router-fallback-retry-backoff
Open

RosieOh wants to merge 5 commits into
BerriAI:mainfrom
RosieOh:fix-router-fallback-retry-backoff

Conversation

@RosieOh

@RosieOh RosieOh commented Sep 22, 2026 •

Copy link
Copy Markdown

TLDR

Problem this solves:

  • A model group with one deployment waits out retry backoff before falling back
  • During a provider outage every request gains about 5 seconds of latency

How it solves it:

  • Retries skip the backoff sleep when the request has an untried fallback
  • The check follows the fallback dispatcher's own order and lists
  • Client-side fallback lists sent in the request count too

User Flow

Before: an app whose primary model has one deployment and a fallback gets answers about 5 seconds late while the primary provider is down

  1. The proxy admin configures primary with one deployment, backup as its fallback, and num_retries: 2
  2. The primary provider starts answering every request with HTTP 500
  3. The app sends POST http://localhost:4000/v1/chat/completions with "model": "primary"
  4. It gets a 200 answered by backup after about 5.5 seconds

After: the same request is answered by the fallback as soon as the retries fail

  1. The proxy admin configures primary with one deployment, backup as its fallback, and num_retries: 2
  2. The primary provider starts answering every request with HTTP 500
  3. The app sends POST http://localhost:4000/v1/chat/completions with "model": "primary"
  4. It gets the same 200 answered by backup in under a second

Relevant issues

Addresses the latency part of #40405. The cooldown and docs asks in that issue are left for a separate change

Affected release

Linear ticket

Pre-Submission checklist

Please complete all items before asking a LiteLLM maintainer to review your PR

  • I have added meaningful tests
  • The handful of test files covering my change pass locally, e.g. uv run pytest tests/test_litellm/<your_test_file>.py -v. Leave the suites (make test-unit-*, make test-unit) to CI: it finishes in ~15 minutes where a laptop takes an hour or more
  • My PR passes all required CI/CD checks (e.g., lint, schema.d.ts sync check, etc.)
  • My PR's scope is as isolated as possible; it only solves 1 specific problem
  • I have received a Greptile Confidence Score of at least 4/5 before requesting a maintainer review (Greptile reviews automatically once the PR is opened; only comment @greptileai to re-request a review after pushing changes)

Delays in PR merge?

If you're seeing a delay in your PR being merged, ping the LiteLLM Team on Slack (#pr-review).

Screenshots / Proof of Fix

The fallback is a real OpenAI call. The primary is a small local server that answers every request with HTTP 500, since a real provider outage cannot be triggered on demand

primary_down.py

import http.server
import json


class PrimaryDown(http.server.BaseHTTPRequestHandler):
    def do_POST(self):
        self.rfile.read(int(self.headers.get("Content-Length", 0)))
        body = json.dumps({"error": {"message": "primary provider is down", "type": "server_error"}}).encode()
        self.send_response(500)
        self.send_header("Content-Type", "application/json")
        self.send_header("Content-Length", str(len(body)))
        self.end_headers()
        self.wfile.write(body)

    def log_message(self, format, *args):
        print("primary hit", flush=True)


http.server.ThreadingHTTPServer(("127.0.0.1", 18081), PrimaryDown).serve_forever()

config.yaml

model_list:
  - model_name: primary
    litellm_params:
      model: openai/gpt-5.4-mini
      api_base: http://127.0.0.1:18081/v1
      api_key: unused
  - model_name: backup
    litellm_params:
      model: openai/gpt-5.4-mini
      api_key: os.environ/OPENAI_API_KEY

router_settings:
  num_retries: 2
  fallbacks: [{"primary": ["backup"]}]

general_settings:
  master_key: os.environ/LITELLM_MASTER_KEY

run.sh sends 10 requests to the primary model and prints the status, total time, the fallback header, and the reply. run_client.sh is the same loop with a client-side fallback list in the request body, "fallbacks": [{"model": "backup"}]

for i in $(seq 1 10); do
  curl -s -o resp.json -D headers.txt -w '%{http_code} %{time_total}s' http://localhost:4100/v1/chat/completions \
    -H "Authorization: Bearer $LITELLM_MASTER_KEY" -H 'Content-Type: application/json' \
    -d '{"model": "primary", "messages": [{"role": "user", "content": "Reply with the single word OK"}]}'
  echo " fallbacks=$(grep -i '^x-litellm-attempted-fallbacks' headers.txt | tr -d '\r' | cut -d' ' -f2) model=$(jq -r .model resp.json) reply=$(jq -r '.choices[0].message.content' resp.json)"
done

For both sides, python3 primary_down.py runs in one shell and python litellm/proxy/proxy_cli.py --config config.yaml --port 4100 in another

Before (7d9cc28)

Fallbacks from the router config

  1. zsh run.sh
200 7.616244s fallbacks=1 model=gpt-5.4-mini-2026-03-17 reply=OK
200 5.935633s fallbacks=1 model=gpt-5.4-mini-2026-03-17 reply=OK
200 5.382487s fallbacks=1 model=gpt-5.4-mini-2026-03-17 reply=OK
200 6.027541s fallbacks=1 model=gpt-5.4-mini-2026-03-17 reply=OK
200 6.348864s fallbacks=1 model=gpt-5.4-mini-2026-03-17 reply=OK
200 5.923693s fallbacks=1 model=gpt-5.4-mini-2026-03-17 reply=OK
200 5.411781s fallbacks=1 model=gpt-5.4-mini-2026-03-17 reply=OK
200 4.916888s fallbacks=1 model=gpt-5.4-mini-2026-03-17 reply=OK
200 6.206349s fallbacks=1 model=gpt-5.4-mini-2026-03-17 reply=OK
200 5.329290s fallbacks=1 model=gpt-5.4-mini-2026-03-17 reply=OK
  1. Every request is answered by the fallback after 4.9 to 7.6 seconds (median 5.93s)

Client-side fallback list in the request

  1. zsh run_client.sh
200 5.166431s fallbacks=1 model=gpt-5.4-mini-2026-03-17 reply=OK
200 5.542447s fallbacks=1 model=gpt-5.4-mini-2026-03-17 reply=OK
200 5.484136s fallbacks=1 model=gpt-5.4-mini-2026-03-17 reply=OK
200 4.801993s fallbacks=1 model=gpt-5.4-mini-2026-03-17 reply=OK
200 4.632703s fallbacks=1 model=gpt-5.4-mini-2026-03-17 reply=OK
200 5.690383s fallbacks=1 model=gpt-5.4-mini-2026-03-17 reply=OK
200 5.652623s fallbacks=1 model=gpt-5.4-mini-2026-03-17 reply=OK
200 5.246833s fallbacks=1 model=gpt-5.4-mini-2026-03-17 reply=OK
200 5.317220s fallbacks=1 model=gpt-5.4-mini-2026-03-17 reply=OK
200 5.637238s fallbacks=1 model=gpt-5.4-mini-2026-03-17 reply=OK
  1. Every request is answered by the fallback after 4.6 to 5.7 seconds (median 5.40s)

After (fca7a3d)

Fallbacks from the router config

  1. zsh run.sh
200 1.174062s fallbacks=1 model=gpt-5.4-mini-2026-03-17 reply=OK
200 0.732363s fallbacks=1 model=gpt-5.4-mini-2026-03-17 reply=OK
200 0.695868s fallbacks=1 model=gpt-5.4-mini-2026-03-17 reply=OK
200 1.641306s fallbacks=1 model=gpt-5.4-mini-2026-03-17 reply=OK
200 0.751415s fallbacks=1 model=gpt-5.4-mini-2026-03-17 reply=OK
200 0.816856s fallbacks=1 model=gpt-5.4-mini-2026-03-17 reply=OK
200 0.741629s fallbacks=1 model=gpt-5.4-mini-2026-03-17 reply=OK
200 0.990606s fallbacks=1 model=gpt-5.4-mini-2026-03-17 reply=OK
200 1.214625s fallbacks=1 model=gpt-5.4-mini-2026-03-17 reply=OK
200 0.723085s fallbacks=1 model=gpt-5.4-mini-2026-03-17 reply=OK
  1. Every request is answered by the fallback after 0.7 to 1.6 seconds (median 0.78s against 5.93s before)

Client-side fallback list in the request

  1. zsh run_client.sh
200 0.695961s fallbacks=1 model=gpt-5.4-mini-2026-03-17 reply=OK
200 0.756758s fallbacks=1 model=gpt-5.4-mini-2026-03-17 reply=OK
200 0.710945s fallbacks=1 model=gpt-5.4-mini-2026-03-17 reply=OK
200 1.170847s fallbacks=1 model=gpt-5.4-mini-2026-03-17 reply=OK
200 1.066847s fallbacks=1 model=gpt-5.4-mini-2026-03-17 reply=OK
200 0.748272s fallbacks=1 model=gpt-5.4-mini-2026-03-17 reply=OK
200 0.940839s fallbacks=1 model=gpt-5.4-mini-2026-03-17 reply=OK
200 0.975789s fallbacks=1 model=gpt-5.4-mini-2026-03-17 reply=OK
200 0.776680s fallbacks=1 model=gpt-5.4-mini-2026-03-17 reply=OK
200 0.846996s fallbacks=1 model=gpt-5.4-mini-2026-03-17 reply=OK
  1. Every request is answered by the fallback after 0.7 to 1.2 seconds (median 0.81s against 5.40s before)

Across both cases the down primary logged 62 attempts before the change and 62 after, so the retries still happen and only the waiting between them is gone

An earlier run of the same two cases hit backup-provider latency, one request at 22.3s and another at 12.0s, with medians of 1.01s and 1.41s. That is the fallback call itself, not backoff: under backoff every request in the run is slow, as the before blocks show. Over both runs (20 requests per case) the medians are 0.80s and 0.96s

Type

🐛 Bug Fix

Caveats (if any)

Medium

  • A rate-limited single deployment with a fallback no longer waits for Retry-After
    • Its retries fail fast and the fallback serves the request instead

Low

  • The failing deployment still gets num_retries + 1 attempts per request
    • allowed_fails with cooldown_time is still how to stop those
  • Instant retries when fallbacks exist first landed in 3e6097d9f8 and were removed an hour later in 4d648a6d89
    • That version checked the router-wide fallbacks list, so any group's fallback disabled backoff everywhere
    • This change only skips backoff when this request's own chain resolves an untried target
  • A chain configured as an empty list (fallbacks: [{"primary": []}]) keeps the backoff
    • has_unattempted_fallback_target reports an unattempted target for an empty chain while the request carries no attempt record, so the gate guards that case before asking
  • Context-window and content-policy errors check their dedicated list first when one is set
    • The dispatcher never falls back to regular fallbacks in that case, so backoff stays

Final Attestation

  • The tests check the right things, including the edge cases, and regressions in the respective real-world customer use-cases are not possible after this PR

@RosieOh
RosieOh requested a review from a team September 22, 2026 08:33
@CLAassistant

CLAassistant commented Sep 22, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@greptile-apps

greptile-apps Bot commented Sep 22, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerConfidence Score: 5/5

[Medium risk] Adjusts retry backoff logic when fallback targets are available.

The PR appears safe to merge based on the reviewed changes.

Summary

The PR skips retry backoff when the request has an untried fallback and adds clock-independent tests for fallback and no-fallback cases.

  • The current fallback check follows client-side, dedicated, and regular fallback selection.
  • No new actionable issue was identified in the changes since the previous review.

Reviews (6) · Last reviewed commit: "test(router): assert the chosen retry de..."

Comment thread litellm/router.py Resolved
@greptile-apps

greptile-apps Bot commented Sep 22, 2026 •

Copy link
Copy Markdown
Contributor

Comments Outside Diff

These findings could not be posted inline.

  • P2 Client fallbacks retain backoff litellm/router.py:8394 ▶

    The dispatcher supports client-side string and request-dictionary fallback lists, but this resolver does not handle those formats consistently. These requests still wait through retry backoff even when an untried fallback could take over, leaving the reported latency problem unresolved for supported configurations

@codspeed

codspeed Bot commented Sep 22, 2026 •

Copy link
Copy Markdown
Contributor

Merging this PR will not alter performance

✅ 31 untouched benchmarks


Comparing RosieOh:fix-router-fallback-retry-backoff (fca7a3d) with main (cd0ac30)1

Open in CodSpeed

Footnotes

  1. No successful run was found on main (7d9cc28) during the generation of this report, so cd0ac30 was used instead as the comparison base. There might be some changes unrelated to this pull request in this report. ↩

@codecov

codecov Bot commented Sep 22, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

Comment thread litellm/router.py
2. there are fallbacks for the completion call
"""
if fallback_available:
return 0

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Low: Unbounded immediate retry burst

An authenticated client can send x-litellm-num-retries: 100 with a short client-controlled timeout when a configured or allowed fallback exists, causing one proxy request to issue 101 immediate primary attempts before falling back. The header is accepted without a cap, and router retries reuse the original request's rate-limit reservation, so returning zero here bypasses the intended pacing; transfer control directly to the fallback or enforce a bounded retry count/minimum delay.

@veria-ai

veria-ai Bot commented Sep 22, 2026

Copy link
Copy Markdown
Contributor

PR overview

This PR changes router retry behavior to skip backoff when a configured fallback can take over a failed request.

One security issue remains open. An authenticated client can request an uncapped number of retries and trigger a burst of immediate primary attempts before fallback handling, potentially amplifying backend load from a single proxy request. No issues have yet been addressed.

Open issues (1)

Fixed/addressed: 0 · PR risk: 5/10

@RosieOh

RosieOh commented Sep 22, 2026

Copy link
Copy Markdown
Author

@greptileai

@RosieOh
RosieOh force-pushed the fix-router-fallback-retry-backoff branch from f1fef4d to 08b1354 Compare September 28, 2026 01:59
@RosieOh

RosieOh commented Sep 28, 2026

Copy link
Copy Markdown
Author

@greptileai rebased onto current main with the test file's new path, and re-measured the real provider proof at this tip.

@RosieOh

RosieOh commented Sep 29, 2026

Copy link
Copy Markdown
Author

@mateo-berri could you take a look when you have a chance? This addresses the latency part of #40405: when a fallback can take over, the router skips the retry backoff instead of sleeping first. Greptile's P1 about an unusable fallback is handled in 2a12366, which checks the fallback the dispatcher will actually use before skipping the backoff, with tests in 08b1354. Rebased on main on 9/28.

@RosieOh
RosieOh force-pushed the fix-router-fallback-retry-backoff branch from 08b1354 to 40ed433 Compare September 30, 2026 03:27
@RosieOh

RosieOh commented Sep 30, 2026

Copy link
Copy Markdown
Author

@greptileai rebased to resolve a conflict with #43600, which added its own fallback helpers next to these. Both kept, proof re-measured.

@RosieOh
RosieOh force-pushed the fix-router-fallback-retry-backoff branch from 40ed433 to 073de7b Compare September 30, 2026 04:07
@RosieOh

RosieOh commented Sep 30, 2026

Copy link
Copy Markdown
Author

@greptileai good catch on the empty chain. The gate now requires a resolved chain with entries, with an integration and a unit regression test.

@greptile-apps

greptile-apps Bot commented Sep 30, 2026

Copy link
Copy Markdown
Contributor

Confirmed. Requiring a non-empty resolved chain fixes the empty-chain case, and the regression coverage verifies that {"primary": []} retains retry backoff while a real untried target skips it. The resolver also handles client-side fallback entries and follows the dispatcher’s dedicated-fallback precedence, so the original finding is addressed.

@RosieOh

RosieOh commented Sep 30, 2026

Copy link
Copy Markdown
Author

@greptileai please re-review the current commit 073de7b and refresh the confidence score; the badge still points at the previous one.

@RosieOh
RosieOh force-pushed the fix-router-fallback-retry-backoff branch from 073de7b to fca7a3d Compare September 30, 2026 05:21
@RosieOh

RosieOh commented Sep 30, 2026

Copy link
Copy Markdown
Author

@greptileai the backoff tests now assert the delay the router chose, with asyncio.sleep patched, so nothing reads the clock.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants