LiteLLM in Python: Cloud Model with Local Fallback
LiteLLM lets Python code call many language models in the OpenAI format. This tutorial sets up a cloud model plus a local one and routes failed cloud calls to the local model.

What you will have at the end and what you need
At the end you have a short Python script that calls a cloud model and a local model under two names. If the cloud call fails, the router sends the request to the local model. The LiteLLM README describes the library as an open source gateway for more than 100 providers, used either as a Python SDK or as a proxy server.
This tutorial uses only the Python SDK, so no central service is needed. The proxy setup is a separate task. You need Python, the uv package manager, an OpenAI API key for the cloud model and a local server that LiteLLM can reach through its Ollama provider. The documentation excerpt does not cover installing Ollama itself, so that step is left open here.
The README lists Ollama among the supported providers, with support for chat completions and embeddings. It also lists other local options, such as LM Studio and Llamafile. The same router pattern should therefore apply to those servers as well, but this tutorial only shows the Ollama prefix.
Step 1: install the SDK and call one model
The README recommends uv for installing the SDK. The following command adds the litellm package to your project.
uv add litellmThe next script sets the API key as an environment variable and sends one message. The completion function takes a model name with a provider prefix, here openai, and a list of messages in OpenAI format. In real use the key belongs in your secret store and should stay out of the source file.
from litellm import completion
import os
os.environ["OPENAI_API_KEY"] = "your-openai-key"
response = completion(
model="openai/gpt-6.1-sol",
messages=[{"role": "user", "content": "Hello!"}],
)
print(response.choices[0].message.content)Other providers work the same way. The README shows an Anthropic call that differs only in the model string and the matching key variable.
Step 2: bundle cloud and local model in a router
The Router class holds a list of deployments. Each entry has a model_name, which is the alias your code uses, and litellm_params, which name the real model. The local entry uses the ollama provider prefix, followed by the name of a model that you have pulled on your server. The documentation excerpt shows no Ollama example, so the model string is left out of the code below. Check the provider page for the exact model string and the api_base setting.
from litellm import Router
router = Router(model_list=[
{"model_name": "cloud",
"litellm_params": {"model": "openai/gpt-6.1-sol"}},
{"model_name": "lokal",
"litellm_params": {"model": "ollama/<model-name>"}},
])From here on, application code asks for an alias and does not need to know the provider. The README lists retry and fallback logic across several deployments as a feature of the SDK router, and it gives Azure and OpenAI as an example of such a pair.
Step 3: set up the fallback to the local model
The block below uses the alias cloud and names a second alias, lokal, as its fallback. It then calls the alias cloud and prints response.model, which reports the model that answered. The lokal alias must exist as a second deployment in the model list, with the Ollama model string taken from the provider page, otherwise the mapping has nothing to point to.
router = Router(
model_list=[
{"model_name": "cloud",
"litellm_params": {"model": "openai/gpt-6.1-sol"}},
{"model_name": "lokal",
"litellm_params": {"model": "ollama/<model-name>"}},
],
# Route failed cloud calls to the local model
fallbacks=[{"cloud": ["lokal"]}],
)
response = router.completion(
model="cloud",
messages=[{"role": "user", "content": "Hello!"}],
)
print(response.model)The excerpt does not say which error types trigger the fallback or how many retries happen first. Read the routing documentation linked from the README before you rely on this in production.
Check that the fallback works
Run the script once with a valid key and the local server running. The printed model name should point to the cloud model. Then start the script again with an invalid or missing OPENAI_API_KEY, so the cloud call fails. If the fallback works, the call still returns an answer and the printed model name now refers to the local model.
Typical pitfalls follow from the setup. The local server must be running and reachable at the address you configured, otherwise the fallback fails as well. Model names need their provider prefix. The README does not document a version pin, so test upgrades of litellm before rolling them out. Also test the failure case on purpose, because a fallback that was never triggered tends to hide configuration mistakes until the day the provider is actually down.
Where this fits in a company
A fallback to a local model keeps an application answering when an external provider is down. It also means requests can end up on either path, so teams usually check which data may go to the cloud model at all. That question touches GDPR and, for law firms and tax advisers, professional secrecy under § 203 StGB. See GDPR and AI and local AI for the background.
Someone in IT should own the script, including keys, model names, the local host and updates of the library. If you want a central service with virtual keys instead of code in each application, the README points to the proxy server. For comparison, souveraen.ai offers an OpenAI-compatible developer API and a model catalogue with local models, models hosted in Germany and external EU models.
Sources
- LiteLLM README, LiteLLM


