LiteLLM Proxy Tutorial: One API for Every LLM You Use
Why one gateway beats five SDKs
Every LLM provider wants its own SDK, its own key, and its own model names. Add a local Ollama server and a second cloud provider, and a small project suddenly has four ways to make the same request: send messages, get text back. That sprawl is where bugs hide. A key lands in the wrong app, a model name changes upstream, and a provider outage breaks a feature that had a perfectly good backup sitting idle.
LiteLLM Proxy sits between your apps and your providers. Your code talks to one OpenAI-compatible endpoint, and one YAML file decides where each request goes. Change the model behind an alias and every app using it follows, with no redeploy. Add a fallback and a failed provider becomes a quiet detour. This guide sets that up locally in about 30 minutes.
- Python 3.10 or newer, and pip.
- Ollama installed and running. Our guide to running an open model locally with Ollama covers the install and the first pull.
- One cloud provider key, if you want the multi-provider part. The local-only setup works without one.
Step 1: Install the proxy
Create a folder and a virtual environment, then install the proxy extra. The [proxy] extra matters: plain litellm is the Python library, while the proxy server needs server dependencies too.
mkdir litellm-gateway && cd litellm-gateway
python3 -m venv .venv
source .venv/bin/activate
pip install 'litellm[proxy]'
Meanwhile, pull a small local model. Llama 3.1 8B is a sensible test default: big enough for real answers, small enough for a modest machine.
ollama pull llama3.1:8b
ollama list
Step 2: Write the config file
Create config.yaml. Each entry has two names: model_name is the alias your apps use, and model under litellm_params is the real provider model. The prefix before the slash picks the provider, so ollama/llama3.1:8b means Ollama serving that tag.
model_list:
- model_name: local-llama
litellm_params:
model: ollama/llama3.1:8b
api_base: "http://localhost:11434"
- model_name: cloud-cheap
litellm_params:
model: openai/gpt-4o-mini
api_key: os.environ/OPENAI_API_KEY
litellm_settings:
drop_params: true
general_settings:
master_key: os.environ/LITELLM_MASTER_KEY
Two details matter. First, os.environ/OPENAI_API_KEY reads the key from the environment at startup, so the secret never sits in a file you might commit. Second, drop_params: true quietly drops parameters a provider does not support, so one request shape works across different backends. Generate a real master key, never a demo placeholder:
export LITELLM_MASTER_KEY="sk-$(openssl rand -hex 32)"
export OPENAI_API_KEY="sk-your-real-key"
With no cloud key, delete the cloud-cheap entry for now and add it later without touching your apps.
Step 3: Start the proxy and test it
Start the server on port 4000, the proxy default, and wait for the startup log to report it running:
litellm --config config.yaml --port 4000
Then send a chat request through the proxy. This curl call uses the OpenAI chat completions shape and asks for the alias local-llama:
curl http://localhost:4000/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $LITELLM_MASTER_KEY" \
-d '{
"model": "local-llama",
"messages": [{"role": "user", "content": "Say hello in one sentence."}]
}'
A healthy reply is JSON with a choices array and a usage block. The Python version shows the real payoff: any OpenAI-compatible client works, with the proxy address as the only change.
from openai import OpenAI
client = OpenAI(base_url="http://localhost:4000/v1", api_key="sk-your-master-key")
reply = client.chat.completions.create(
model="local-llama",
messages=[{"role": "user", "content": "Explain what a proxy server does in two sentences."}],
)
print(reply.choices[0].message.content)
Note the /v1 on the Python base URL: the SDK appends the endpoint path itself, while raw curl spells out the full /v1/chat/completions path. Mixing those up is the most common first-run mistake.
Step 4: Add a fallback so outages stop hurting
A gateway that only routes is convenient. One that reroutes is insurance. Add a router_settings block naming a fallback for the local model, restart the proxy, then test it deliberately: stop Ollama, request local-llama, and watch the cloud model answer instead of failing.
router_settings:
fallbacks:
- local-llama:
- cloud-cheap
That controlled failure is worth doing once, because it proves the safety net before you need it. It also enables a cheap operating pattern: everyday traffic stays local, and the cloud model handles failures or hard questions. Our hosted versus self-hosted inference cost comparison breaks down where that trade-off pays off, and where paying a provider outright is still cheaper.
The proxy binds to all interfaces by default, and anyone reaching port 4000 with a weak master key could spend your provider budget. For local development, keep it on localhost. For anything shared, use a strong master key, keep keys in environment variables only, and put a firewall or reverse proxy in front. Never expose the proxy directly to the internet unauthenticated: it is a doorway to every provider key behind it.
When you want to manage models without editing YAML, the proxy ships a web UI at http://localhost:4000/ui. The file-based setup here remains the better starting point: a config file is version-controlled and reviewable.
Troubleshooting
- Connection refused on port 4000: the proxy is not running, or it crashed on a config error. Read the startup log first; a bad YAML indent fails fast.
- Model not found: check the alias spelling, then the provider prefix and tag.
- Local requests time out: the model may still be loading. Wait for the first load, then retry. For larger models served regularly, our vLLM serving guide covers a faster server.
- Cloud auth error: the provider key is missing or was not exported in the same terminal session that started the proxy.
- Unsupported parameter errors: restore
drop_params: trueand restart.
The bottom line
Start with one alias and one local model, prove the path with curl, then grow the config: a cloud model, a fallback, a master key, and only then a UI or database. Stable names outside, changeable machinery inside, secrets in one place. Next time a provider changes pricing or a model disappears, you edit one line instead of rewriting every app you own.


Comments
Post a Comment