
LiteLLM is an open-source AI gateway that provides a unified, OpenAI-compatible API for over 100 large language model (LLM) providers, removing the need to integrate with each provider's Software Development Kit (SDK) and authentication scheme. In production, teams use LiteLLM to issue virtual keys with budget limits, track token usage and spend across providers, and configure centralized access control and routing.
This article explains how to deploy LiteLLM on a Linux server using Docker Compose with PostgreSQL for persistent storage, Prometheus for metrics collection, and Traefik as the reverse proxy for HTTPS access.
Before you begin, you need to:
litellm.example.com, pointing to your server's IP address.LiteLLM requires a configuration file to define model providers and routing rules, a Prometheus configuration file for metrics scraping, and environment variables for secrets and database credentials. This section prepares all three before deploying the stack.
Create the project directory with subdirectories for persistent data.
letsencrypt: Stores SSL/TLS certificates.postgres: Persists PostgreSQL database files.prometheus: Persists Prometheus metrics data.Navigate to the project directory.
Prometheus runs as UID 65534 inside the container. Set matching ownership on its host directory so the container can write to it.
Generate a master key and salt key for LiteLLM. Run this command twice to produce two separate values.
Save both values for the LITELLM_MASTER_KEY and LITELLM_SALT_KEY fields in the next step.
Create the .env file to store credentials and secrets.
Add the following values:
Replace the placeholders with your own values:
litellm.example.com: Domain pointing to your server's IP address.admin@example.com: Email address for Let's Encrypt certificate notifications.sk-YOUR_MASTER_KEY: Admin key used to authenticate with the LiteLLM API. Replace YOUR_MASTER_KEY with the first value you generated, and keep the sk- prefix.sk-YOUR_SALT_KEY: Key used to encrypt provider credentials stored in PostgreSQL. Replace YOUR_SALT_KEY with the second value you generated, and keep the sk- prefix. This value cannot be changed after the first model is added.YOUR_LLM_PROVIDER_API_KEY: API key for the LLM provider configured in config.yaml.STRONG_DATABASE_PASSWORD: PostgreSQL password used by both the database container and the connection string. Replace with a strong password of your choice.admin: Username for the LiteLLM dashboard. Change to your preferred username.YOUR_UI_PASSWORD: Password for the LiteLLM dashboard. Use a strong password distinct from your master key.Save and close the file.
Create the LiteLLM configuration file.
Add the following contents:
Replace my-model with a name of your choice to identify this model within LiteLLM. Replace provider/model with the LiteLLM provider prefix followed by the model identifier, in the format provider/model-name. For example, to expose Anthropic's Claude Haiku, use anthropic/claude-haiku-4-5. The os.environ/ prefix tells LiteLLM to read the value from an environment variable at runtime rather than hardcoding it in the file. For the provider prefix and exact model identifiers for each provider, see the LiteLLM providers documentation.
The litellm_settings block enables Prometheus metrics collection via the callbacks field. Setting require_auth_for_metrics_endpoint to true restricts the /metrics endpoint to authenticated requests only, so only Prometheus, which sends the master key as a Bearer token, can scrape metrics successfully.
The general_settings block defines the master key LiteLLM uses to authenticate admin API requests and virtual key management operations. Reading it from an environment variable keeps the actual key value out of the configuration file.
Save and close the file.
Create the Prometheus configuration file.
Add the following contents:
Replace sk-YOUR_MASTER_KEY with the value of LITELLM_MASTER_KEY from your .env file. Prometheus uses this token to authenticate its scrape requests against the protected /metrics endpoint.
Save and close the file.
The deployment stack runs LiteLLM behind Traefik, which handles Transport Layer Security (TLS) termination and automatic certificate provisioning through Let's Encrypt. PostgreSQL provides persistent storage for virtual keys, spend data, and usage logs. Prometheus collects gateway metrics by scraping the LiteLLM /metrics endpoint every 15 seconds over the internal Docker network.
Add your user account to the Docker group.
Apply the new group membership.
Create the Docker Compose manifest file.
Add the following contents:
Save and close the file.
This Docker Compose configuration deploys four services behind a single HTTPS endpoint:
traefik: Acts as the reverse proxy and TLS terminator, listening on ports 80 and 443 and automatically redirecting HTTP traffic to HTTPS using Let's Encrypt.litellm: Runs the LiteLLM proxy image pinned to a specific release tag for reproducible deployments, mounting config.yaml for model configuration and waiting for PostgreSQL to become healthy before starting. Traefik routes HTTPS traffic for ${DOMAIN} to port 4000.db: Runs PostgreSQL 16 as the persistent backend for virtual keys, spend data, and usage logs, using a named volume so data survives container restarts.prometheus: Scrapes LiteLLM metrics from port 4000 every 15 seconds and stores time-series data in a named volume with a 15-day retention window. The volumes block declares the named volumes used by db and prometheus, ensuring their data persists independently of the container lifecycle and survives container removal or recreation.
LiteLLM Docker images are signed with Cosign. Verifying the image signature before deployment confirms the image has not been tampered with since it was published by the LiteLLM team.
Download and install the Cosign binary.
Move the binary into your system path.
Make the binary executable.
Verify the installation.
The output displays the installed Cosign version, confirming the binary is available in your system path.
Verify the LiteLLM image signature using the pinned public key. This checks the same release tag deployed in the Docker Compose file in the next step. LiteLLM publishes signatures for the ghcr.io registry specifically, so this confirms the v1.89.1 release itself, not the docker.litellm.ai mirror byte-for-byte.
A successful verification outputs a JSON payload confirming the image was signed with the LiteLLM public key. If verification fails, confirm you copied the key URL and image tag, then run the command again.
With the configuration files and images in place, bring up the full deployment.
Start the services.
Verify all containers are running.
Verify that all four containers show an Up status, with litellm and db marked (healthy) and traefik listing ports 80 and 443 under PORTS.
For more information on managing a Docker Compose stack, see the How to Use Docker Compose article.
LiteLLM exposes a web-based admin dashboard at the /ui path of your configured domain. The dashboard provides visibility into model configuration, virtual keys, usage, and spend tracking.
Open your web browser and navigate to https://litellm.example.com/ui, replacing litellm.example.com with your configured domain.
Log in using the credentials from your .env file. Enter the value of UI_USERNAME in the Username field and the value of UI_PASSWORD in the Password field, then click Login. Verify that the dashboard loads and displays the main navigation panels.
Click Models + Endpoints in the left sidebar to verify the configured model providers. The model defined in config.yaml appears in the list with its alias and underlying provider model.
The Model Management page lists all configured model aliases and their underlying providers. The left sidebar also provides access to Virtual Keys for managing scoped access credentials, Usage for per-model and per-key request tracking, and Logs for spend and cost breakdowns by model, key, and team.
LiteLLM exposes an OpenAI-compatible API, so any application built for the OpenAI SDK works with LiteLLM by pointing base_url at the gateway. Virtual keys provide scoped, credential-isolated access without exposing the master key. Each key carries its own model access list, budget limit, and rate limit.
Install the Python virtual environment package.
Create a Python virtual environment.
Activate the virtual environment.
Install the OpenAI Python SDK.
Export your master key as an environment variable. Replace sk-YOUR_MASTER_KEY with the LITELLM_MASTER_KEY value from your .env file.
Export your gateway domain as an environment variable. Replace litellm.example.com with your configured domain.
Create the test script.
Add the following contents, replacing my-model with the model name you set in config.yaml:
Save and close the file.
Run the script.
The script returns a response from the configured LLM provider, confirming the gateway is routing requests correctly.
Create a virtual key using the LiteLLM API. Replace my-model with the model name you set in config.yaml.
The command prints the generated virtual key, similar to sk-M9M0_aUDLi7AuhXtOAw6uw. Virtual keys let you issue scoped credentials to individual users or applications without exposing the master key. Each key can have its own model access list, budget limit, and rate limit.
Export the virtual key returned in the previous step. Replace sk-your-virtual-key with the full key string printed by the previous command.
Send a request using the virtual key. Replace my-model with the model name you set in config.yaml.
The gateway authenticates the virtual key, routes the request to the configured provider, and returns the model response.
Return to the dashboard at https://litellm.example.com/ui and click Logs in the left sidebar.
Each entry shows the model alias, key alias, token count, and estimated cost for the requests sent through both the master key and the virtual key.
You have deployed LiteLLM on a Linux server using Docker Compose with PostgreSQL for persistent storage, Prometheus for authenticated metrics collection, and Traefik for automatic HTTPS through Let's Encrypt. The deployment provides a fully functional, OpenAI-compatible AI gateway accessible over a custom domain, with virtual key management, spend tracking, and provider routing configured. For more information, refer to the official LiteLLM documentation.
0 Comments
Be the first to comment and share your perspective with the community.