Skip to content

feat(ai): Route around a stalled or offline model provider #5990

Closed

Description

@dan-petty

Problem

A model provider can stall or go offline at any time: a cloud provider behind OpenRouter, the gateway itself, or a local Ollama server. The gateway does not route around a stalled provider, so the roadmap jobs fail until a person changes the routing.

On 2026-11-16 from about 10:00Z, openrouter/deepseek/deepseek-v4.1-flash-1017 waited about 300 s for its first token. Every stage, refine, implement and alternatives call on it failed for more than 5 hours (class=provider), and a person changed the routing twice (#5978, #5988). Why the existing tools did not help:

  • LiteLLM falls back from the group deepseek-v4.1-flash-1017 to deepseek-v4.1-flash, but OpenRouter sends its timeout inside a stream that already started, as code 408. LiteLLM (1.103.11, and 1.109.1 in _handle_stream_fallback_error) raises an in-stream 4xx other than 429 with no retry and no fallback; only a transport timeout (httpx.TimeoutException) is exempt.
  • LiteLLM has no deadline for the first token. OpenRouter keeps the stream open while it waits, so the gateway timeout (900 s) and the client deadline (1200 s) do not end the stalled call first.
  • The client resend of fix(ai): one retry layer for a gateway call #5965 sends the call again to the same group, and it stalls again.
  • No metric or alert names a stalled group: the median time to the first token of the group went from 2 s to 150 s to no token, and nothing reacted.

Expected behavior

  • A group with a stalled or offline deployment stops getting new calls within a short time (for example 60 s), and the calls go to the next healthy deployment in an order that the gateway config sets, for a cloud or a local backend.
  • A call that has no first token within a deadline is ended and sent to the next deployment, before any content reaches the client.
  • When the stalled deployment is healthy again, it gets its calls back with no change by a person.
  • A metric and an alert name each group whose time to the first token or failure rate is above a limit.
  • When no deployment of a job's model is healthy, the jobs wait (feat(roadmap): park jobs while the gateway is offline #5967) instead of failing.

Use the open tools first: LiteLLM deployment order within one group, allowed_fails and cooldowns, background health checks, router fallbacks, and an upstream LiteLLM change so that a 408 before the first chunk (is_pre_first_chunk) can fall back. Build only what they cannot do, with the cost written.

Related

Activity

  1. added
    type/feature
    scope/ai
    on Nov 16, 2026
  2. dan-petty commented on Nov 16, 2026

    @dan-petty
    OwnerAuthor
  3. added
    source/agent
    status/new
    effort/high
    value/high
    priority/p2-medium
    owner/agent
    on Nov 16, 2026
  4. dan-petty commented on Nov 16, 2026

    @dan-petty
    OwnerAuthor

    Design: route around a stalled or offline provider

    Evidence: a mock SSE server (comments each second, then an error chunk) drove litellm.Router 1.103.11 and 1.109.1 and the 1.103.11 proxy. Lines are from 1.103.11.

    Findings (1.103.11)

    Question Result
    1. order Works before the call: the router removes cooled and unhealthy deployments, then keeps the lowest order (router.py:12846-12917). Order 2 answered in 0.01 s; order 1 got no request.
    2. Cooldown allowed_fails: 3 equals the default (__init__.py:538), so the rate rule applies (cooldown_handlers.py:364-405). A one-deployment group never cooled (6 of 6 calls failed); a two-deployment group cooled after 5. Order-1 model_info.allowed_fails: 0 cooled after 1. Router allowed_fails: 1 cooled a one-deployment group, then the fallback started before the call. TimeoutErrorAllowedFails does nothing: the callback gets the raw OpenRouterException. The fail counter lives for cooldown_time (:595-598), so stalls of 300 s never add up at 30 s.
    3. Health checks Work (proxy_server.py:4025-4100). Interval 5 s, health_check_timeout 3 s: a call 9 s into a stall got order 2 in 0.02 s; order 1 came back 6.1 s after recovery. A call in flight still failed after the stall.
    4. Deadline None. Comments reset the read timeout: with timeout and stream_timeout at 3 s, a comments-only stream did not end in 15 s.
    5. Upstream 1.109.1 reads the first chunk inside _acompletion (BerriAI/litellm#46493): an in-stream 408 is retried, cools, and falls back. TTFT deadline requests #5859 and #17991 closed as not planned. #49795 is open (a reasoning prefix blocks the fallback).
    6. OpenRouter No timeout. preferred_max_latency and sort only reorder.

    Also: an offline local tier costs 25 s on each call today, and 2.1 s once with allowed_fails: 2. Live: the 1017 TTFT p50 went from 2 s to 117 s, then had no sample.

    Recommended setup

    model_list:
      - model_name: deepseek-v4.1-flash-1017
        litellm_params: {model: openrouter/deepseek/deepseek-v4.1-flash-1017, order: 1}
        model_info: {allowed_fails: 0, cooldown_time: 120, health_check_timeout: 30}
      - model_name: deepseek-v4.1-flash-1017
        litellm_params: {model: openrouter/deepseek/deepseek-v4.1-flash, order: 2}
    router_settings: {allowed_fails: 2}
    general_settings:
      background_health_checks: true
      health_check_interval: 60
      enable_health_check_routing: true
      background_health_check_model_groups: [deepseek-v4.1-flash-1017, deepseek-v4.1-flash]

    First-token deadline

    LiteLLM cannot end a stream with no first chunk.

    1. Upstream PR (preferred; needs the owner's OK). A 1.109.1 prototype with a 3 s deadline: 3.8 s for the first call, then 0.01 s. Cost: 200 lines with tests, 150k tokens, 3 h, plus review time. Draft:

      feat(router): stream_first_chunk_timeout. SSE comments reset the read timeout, so stream_timeout never fires while a provider waits. _aread_first_chunk waits at most this long, closes the stream, and raises litellm.Timeout (cause httpx.ReadTimeout) through _log_stream_failure_and_raise, so cooldown, retries and fallbacks run.

    2. Gateway callback: rejected. On 1.103.11 the router returns the stream first, so a callback cannot fall back.
    3. Client: a 120 s first-event deadline in GatewayCallModel (src/devops_cli/ai/models/gateway.py); the resend then gets order 2. Cost: 80k tokens, 1.5 h.

    Metrics and alert

    New group llm-gateway in k8s/devops/monitoring/prometheus-rules-values.yaml, severity: warning:

    • LlmGatewayFirstTokenSlow: histogram_quantile(0.5, sum by (le, requested_model) (rate(litellm_llm_api_time_to_first_token_metric_bucket{job="llm-gateway", api_provider="openrouter"}[15m]))) > 30, for: 10m.
    • LlmGatewayNoFirstToken: 3 or more calls of a hosted group in 30 min (litellm_proxy_total_requests_metric_total) unless a TTFT sample. A stall gives no TTFT sample, and the failure counters miss in-stream errors (4 in 12 h).

    Link to #5967

    The Service reads the gateway /health (cached background results) and parks the jobs of a group with no healthy deployment.

    Build plan

    1. feat(ai): Route around a stalled or offline model provider #5990: the config above, with per-deployment prices in _HOSTED_GROUP_PRICES. Test first with the mock server: an in-stream 408 moves the next call to order 2 in less than 1 s.
    2. Alerts, with a promtool test of the 2026-11-16 data.
    3. The client deadline.
    4. LiteLLM v1.109.1, with the same test, the keepalive before the first token, and #49795.
    5. Upstream PR, then set 60 s and remove step 3.
    6. feat(roadmap): park jobs while the gateway is offline #5967.
  5. added
    status/in-progress
    status/in-review
    and removed
    status/new
    status/in-progress
    on Nov 16, 2026
  6. added a commit that references this issue on Nov 16, 2026
    5d8f7a2
  7. removed
    status/in-review
    owner/agent
    on Nov 16, 2026
  8. devops-cli-service commented on Nov 16, 2026

    @devops-cli-service
    Contributor

    Live since 2026-11-16 18:14 UTC

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    effort/highpriority/p2-mediumscope/aisource/agenttype/featurevalue/high

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions