#31557·litellm

Fallback chain silently fails when fallback model has smaller context window than primary

Author: ttxs69Created Jun 28, 2026Updated Sep 29, 2026
Labelsllm translationstale

Observed behavior

When a Router is configured with a generic fallbacks list and the primary model fails for a non-context reason (rate limit, timeout, server error), the same messages payload is forwarded to each fallback model as-is. If a fallback model has a smaller max_input_tokens than the primary and the request exceeds the fallback's context window, the fallback raises ContextWindowExceededError and the chain ends. No preflight token counting or message trimming happens between fallbacks.

Repro (router config):

yaml
model_list:
  - model_name: gpt-4-turbo
    litellm_params:
      model: gpt-4-turbo
      api_key: os.environ/OPENAI_API_KEY
  - model_name: gpt-4
    litellm_params:
      model: gpt-4
      api_key: os.environ/OPENAI_API_KEY
router_settings:
  fallbacks:
    - gpt-4-turbo: [gpt-4]

Send a ~10K-token request. If the primary fails on a rate limit (or any non-context error), the fallback gpt-4 (max_input_tokens=8192) receives the same payload and fails with ContextWindowExceededError. The user gets a context-window error from the fallback, with no indication that the fallback was a bad choice.

Why this happens

In litellm/router_utils/fallback_event_handlers.py::run_async_fallback (around line 133), only the model field is swapped before re-entering the router. No token_counter or max_input_tokens check is performed. Same shape in litellm/litellm_core_utils/fallback_utils.py::async_completion_with_fallbacks for the SDK-level path.

There is a separate context_window_fallbacks config in litellm/router.py::async_function_with_fallbacks_common_utils (around line 6721) that handles the inverse case (primary fails on context error). But for non-context errors on the primary, the generic fallbacks chain has no context-window awareness.

Possible directions

A few options, roughly in increasing cost:

  1. Config-time validation. At router init, look up each model's max_input_tokens from model_prices_and_context_window.json and warn (or hard-error in strict mode) if a fallback has a smaller context window than the model it backs up. O(1) at startup, no per-request cost.

  2. Better error enrichment. When a fallback fails with ContextWindowExceededError, append the remaining fallbacks and their max_input_tokens to the exception message so users can debug config mistakes quickly.

  3. Runtime preflight token counting. Before each fallback, count tokens and skip fallbacks whose max_input_tokens is too small. Has caveats: tokenizer accuracy across providers is uneven (only OpenAI has a faithful local counter), and there's still per-call latency cost. The "true" post-provider token count can differ from the local estimate.

  4. Docs. A clear note that fallbacks should have max_input_tokens >= primary and a pointer to context_window_fallbacks for the primary-fails-on-context case.

Curious what maintainers and others think. Is there a reason runtime preflight has been avoided? Would config-time validation (option 1) be a welcome addition, or is this something users are expected to manage themselves?