Fallback chain silently fails when fallback model has smaller context window than primary
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):
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:
Config-time validation. At router init, look up each model's
max_input_tokensfrommodel_prices_and_context_window.jsonand 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.Better error enrichment. When a fallback fails with
ContextWindowExceededError, append the remaining fallbacks and theirmax_input_tokensto the exception message so users can debug config mistakes quickly.Runtime preflight token counting. Before each fallback, count tokens and skip fallbacks whose
max_input_tokensis 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.Docs. A clear note that fallbacks should have
max_input_tokens >= primaryand a pointer tocontext_window_fallbacksfor 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?
Source: BerriAI/litellm