Why Run an AI Gateway
- One endpoint: apps use the OpenAI SDK and only change the model name to switch provider.
- Your keys stay private: provider keys live on the gateway, encrypted; apps get virtual keys you can revoke.
- Budgets and spend: a limit per key, team or user, and a record of what each one used.
- Your own models too: a model on your own GPU server sits next to the hosted ones behind the same keys.
1. Prepare the Server
On Ubuntu 24.04, secure the server and install Docker. Point an A record such as ai.example.com at it and allow ports 80 and 443.
2. Download the Compose File and Make Keys
mkdir ~/litellm && cd ~/litellm curl -sSLO https://github.com/BerriAI/litellm/raw/main/litellm/deploy/docker/docker-compose.quickstart.yml printf 'LITELLM_MASTER_KEY=sk-%s\nLITELLM_SALT_KEY=sk-%s\n' "$(openssl rand -hex 32)" "$(openssl rand -hex 32)" > .env echo "POSTGRES_PASSWORD=$(openssl rand -hex 16)" >> .env echo "LITELLM_BIND=127.0.0.1:" >> .env chmod 600 .env
- The master key authorises every management call and is the admin UI's password.
- The salt key encrypts the provider keys you add. It cannot be rotated in place: change it and every stored key becomes unreadable until you enter it again. Back up
.env. LITELLM_BIND=127.0.0.1:publishes port 4000 on the server itself only, so it is reached through Caddy. Docker would otherwise open it past ufw, as explained in why ufw does not block Docker ports.
The file uses the latest image; for production, LiteLLM advises pinning a tested release tag. Add restart: unless-stopped to both services so they come back after a reboot, then start it:
docker compose -f docker-compose.quickstart.yml up -d docker compose -f docker-compose.quickstart.yml ps
3. Add HTTPS With Caddy
sudo apt install -y caddy
sudo tee /etc/caddy/Caddyfile >/dev/null <<'EOF'
ai.example.com {
reverse_proxy 127.0.0.1:4000
}
EOF
sudo systemctl reload caddy4. Add Models
Open https://ai.example.com/ui, sign in as admin with the master key, and add your providers and models; the Compose file stores them in the database. To keep models in a file instead, LiteLLM reads a config.yaml in this form (LiteLLM config docs):
model_list:
- model_name: claude-sonnet # the name your apps send
litellm_params:
model: anthropic/claude-sonnet-5
api_key: os.environ/ANTHROPIC_API_KEY
- model_name: local-gpt-oss # your own vLLM or llama-server
litellm_params:
model: openai/openai/gpt-oss-120b
api_base: http://10.0.0.5:8000/v1
api_key: os.environ/LOCAL_LLM_KEY
general_settings:
master_key: os.environ/LITELLM_MASTER_KEYFor your own model server, the openai/ prefix tells LiteLLM it speaks the OpenAI API; the rest is the model name that server expects. Keep that server on a private network or behind its own key.
5. Hand Out Virtual Keys
Create a key for each app or person, limited to certain models:
curl https://ai.example.com/key/generate \
-H "Authorization: Bearer YOUR_MASTER_KEY" \
-H "Content-Type: application/json" \
-d '{"models": ["claude-sonnet", "local-gpt-oss"], "metadata": {"user": "support-bot"}}'A spending limit per key (max_budget) and other limits are set in the admin UI or the same API (virtual keys docs). Then any OpenAI client works with the new key:
curl https://ai.example.com/v1/chat/completions \
-H "Authorization: Bearer sk-THE-VIRTUAL-KEY" \
-H "Content-Type: application/json" \
-d '{"model": "claude-sonnet", "messages": [{"role": "user", "content": "Hello"}]}'Backups and Updates
Keys, models and spend records live in PostgreSQL; the provider keys inside are encrypted with the salt key, so back up both the database and .env:
cd ~/litellm docker compose -f docker-compose.quickstart.yml exec -T db pg_dump -U litellm litellm | gzip > ~/litellm-$(date +%F).sql.gz
Copy the dump off the server, as in backing up a database to S3. To update, change the image tag, then docker compose -f docker-compose.quickstart.yml pull and up -d. For production, LiteLLM also suggests adding NO_DOCS="True" and NO_OPENAPI="True" to the proxy's environment to hide its API docs pages.