AI › AI Tools

Tracing a model fallback through LiteLLM

Trace a LiteLLM fallback from a failing mock upstream to its backup using the proxy response and the upstream call order.

Tracing a model fallback through LiteLLM

The app asked LiteLLM for the release summary once. The primary route returned 503, and the backup supplied the final response. I changed the retry setting and compared the order in the upstream log with the headers the app received.

I ran LiteLLM Proxy v1.111.1 under Docker Compose and sent real HTTP requests to it. A local fixture supplied both upstream responses: the primary’s failure and the backup’s completion. LiteLLM release

Decide when a retry should become a fallback

The app requests release-summary in both runs. A retry gives the primary route another chance after a failure. A fallback sends the request to a different model group when the primary can’t serve it. LiteLLM can also choose among several deployments of one model group. Here I gave each route a different group name so the change would be visible in the response. LiteLLM fallbacks · Routing strategies

What the app needsSetting to inspect
Send another attempt to the primary after an eligible failurenum_retries and the error and timeout policies
Move to a different model group after the primary failsfallbacks and its ordered list of alternatives
Choose a deployment within the same grouprouting_strategy and the deployments in that model group

In the OpenRouter routing exercise, I inspected the model and provider returned by an upstream gateway. When LiteLLM sits in front of another gateway, each layer can add attempts. A single local fixture made it possible to count the calls LiteLLM sent in this experiment.

Make the primary route return a controlled 503

I ran an OpenAI-compatible fixture as the fixture service in Docker Compose. It always returns HTTP 503 on POST /primary/v1/chat/completions and returns HTTP 200 with a fixed completion on POST /backup/v1/chat/completions. Both paths use the same public Jekyll 4.4.0 excerpt. The LiteLLM port was bound to the host loopback on 18476. LiteLLM Docker quickstart

This is the model and routing section of the proxy configuration. The fixture uses a disposable FIXTURE_API_KEY environment value, and LiteLLM has its own administrator key. I restarted the proxy for each run and changed num_retries from 1 to 0. I disabled cooldowns for the lab so a previous failure would have less influence on a later attempt.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
model_list:
  - model_name: release-summary
    litellm_params:
      model: openai/fixture-primary
      api_base: http://fixture:8080/primary/v1
      api_key: os.environ/FIXTURE_API_KEY
    model_info:
      id: fixture-primary
  - model_name: release-summary-backup
    litellm_params:
      model: openai/fixture-backup
      api_base: http://fixture:8080/backup/v1
      api_key: os.environ/FIXTURE_API_KEY
    model_info:
      id: fixture-backup

router_settings:
  num_retries: 1
  max_fallbacks: 1
  disable_cooldowns: true
  fallbacks:
    - release-summary: [release-summary-backup]

Send one request with the original model alias

Set LITELLM_MASTER_KEY in Bash and save the public release-note excerpt as release-notes.md. This sample sends one POST, then saves the headers and response body for inspection. In the isolated lab I used the administrator key. For app traffic, use a virtual key with model access scoped to the model groups the app needs.

1
2
3
4
5
6
7
8
9
jq -n --rawfile notes release-notes.md '{
  model: "release-summary",
  messages: [{role: "user", content: ("Summarize these release notes: " + $notes)}],
  stream: false
}' |
  curl --silent --show-error --max-time 30 --dump-header response.headers \
    'http://127.0.0.1:18476/v1/chat/completions' \
    -H 'Content-Type: application/json' \
    -H "Authorization: Bearer ${LITELLM_MASTER_KEY:?}" --data-binary @- > response.json

I extracted the status, deployment and model group, and the retry and fallback headers from that response. The body shows the completion that the app got. LiteLLM response headers

1
2
3
4
awk 'tolower($0) ~ /^http\/|^x-litellm-(attempted-retries|attempted-fallbacks|model-id|model-group|version):/ {
  sub(/\r$/, ""); print
}' response.headers
jq -r '"body.model: \(.model)", "body.content: \(.choices[0].message.content)"' response.json

Compare the final response with the upstream call order

SettingCalls in the mock upstream logFinal response
num_retries: 1Primary 503 → primary 503 → backup 200HTTP 200 from release-summary-backup
num_retries: 0Primary 503 → backup 200HTTP 200 from release-summary-backup

The response’s x-litellm-model-id was fixture-backup and x-litellm-attempted-fallbacks was 1 in both runs. I also saw x-litellm-attempted-retries: 0 in both final responses. The fixture log still showed two primary calls in the first run and one in the second. In this test, the final header had less information about the primary’s call count than the log of each upstream request. I use the final model group to identify the route that served the reply and the upstream events to count its attempts.

LiteLLM test output showing two primary 503 responses with one retry configured and one primary 503 with retries disabled, followed by backup success in both cases Figure 1. The captured output pairs the mock upstream call order with the final LiteLLM response in each run.

Keep enough detail to explain a model change

A successful fallback can keep the app working and still change the provider’s response shape or latency. Save the requested alias and the actual model group and deployment so you can trace that change back to a route. If the primary is expected to use tools or produce a schema, exercise those features with the fallback model and the same input. This local fixture established the order under two known settings, which gives me a baseline to compare with an upstream’s own records.

  • Keep the requested alias, final model group, and deployment ID together.
  • Use records from the upstream attempts when you need a total call count.
  • Account for retries and timeouts at each layer if one gateway calls another.
This post is licensed under CC BY 4.0 by the author.