🐳 Docker, Deploying LiteLLM Proxy
You can find the Dockerfile to build litellm proxy here
Quick Start
- Basic
- With CLI Args
- use litellm as a base image
- Kubernetes
See the latest available ghcr docker image here: https://github.com/berriai/litellm/pkgs/container/litellm
docker pull ghcr.io/berriai/litellm:main-latest
docker run ghcr.io/berriai/litellm:main-latest
Run with LiteLLM CLI args
See all supported CLI args here:
Here's how you can run the docker image and pass your config to litellm
docker run ghcr.io/berriai/litellm:main-latest --config your_config.yaml
Here's how you can run the docker image and start litellm on port 8002 with num_workers=8
docker run ghcr.io/berriai/litellm:main-latest --port 8002 --num_workers 8
# Use the provided base image
FROM ghcr.io/berriai/litellm:main-latest
# Set the working directory to /app
WORKDIR /app
# Copy the configuration file into the container at /app
COPY config.yaml .
# Make sure your entrypoint.sh is executable
RUN chmod +x entrypoint.sh
# Expose the necessary port
EXPOSE 4000/tcp
# Override the CMD instruction with your desired command and arguments
CMD ["--port", "4000", "--config", "config.yaml", "--detailed_debug", "--run_gunicorn"]
Deploying a config file based litellm instance just requires a simple deployment that loads the config.yaml file via a config map. Also it would be a good practice to use the env var declaration for api keys, and attach the env vars with the api key values as an opaque secret.
apiVersion: v1
kind: ConfigMap
metadata:
name: litellm-config-file
data:
config.yaml: |
model_list:
- model_name: gpt-3.5-turbo
litellm_params:
model: azure/gpt-turbo-small-ca
api_base: https://my-endpoint-canada-berri992.openai.azure.com/
api_key: os.environ/CA_AZURE_OPENAI_API_KEY
---
apiVersion: v1
kind: Secret
type: Opaque
metadata:
name: litellm-secrets
data:
CA_AZURE_OPENAI_API_KEY: bWVvd19pbV9hX2NhdA== # your api key in base64
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: litellm-deployment
labels:
app: litellm
spec:
selector:
matchLabels:
app: litellm
template:
metadata:
labels:
app: litellm
spec:
containers:
- name: litellm
image: ghcr.io/berriai/litellm:main-latest # it is recommended to fix a version generally
ports:
- containerPort: 4000
volumeMounts:
- name: config-volume
mountPath: /app/proxy_server_config.yaml
subPath: config.yaml
envFrom:
- secretRef:
name: litellm-secrets
volumes:
- name: config-volume
configMap:
name: litellm-config-file
[!TIP] To avoid issues with predictability, difficulties in rollback, and inconsistent environments, use versioning or SHA digests (for example,
litellm:main-v1.30.3
orlitellm@sha256:12345abcdef...
) instead oflitellm:main-latest
.
Deploy with Database
We maintain a seperate Dockerfile for reducing build time when running LiteLLM proxy with a connected Postgres Database
- Dockerfile
- Kubernetes
- Helm
docker pull docker pull ghcr.io/berriai/litellm-database:main-latest
docker run --name litellm-proxy \
-e DATABASE_URL=postgresql://<user>:<password>@<host>:<port>/<dbname> \
-p 4000:4000 \
ghcr.io/berriai/litellm-database:main-latest
Your OpenAI proxy server is now running on http://0.0.0.0:4000
.
Step 1. Create deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: litellm-deployment
spec:
replicas: 1
selector:
matchLabels:
app: litellm
template:
metadata:
labels:
app: litellm
spec:
containers:
- name: litellm-container
image: ghcr.io/berriai/litellm-database:main-latest
env:
- name: DATABASE_URL
value: postgresql://<user>:<password>@<host>:<port>/<dbname>
kubectl apply -f /path/to/deployment.yaml
Step 2. Create service.yaml
apiVersion: v1
kind: Service
metadata:
name: litellm-service
spec:
selector:
app: litellm
ports:
- protocol: TCP
port: 4000
targetPort: 4000
type: NodePort
kubectl apply -f /path/to/service.yaml
Step 3. Start server
kubectl port-forward service/litellm-service 4000:4000
Your OpenAI proxy server is now running on http://0.0.0.0:4000
.
Step 1. Clone the repository
git clone https://github.com/BerriAI/litellm.git
Step 2. Deploy with Helm
helm install \
--set masterkey=SuPeRsEcReT \
mydeploy \
deploy/charts/litellm
Step 3. Expose the service to localhost
kubectl \
port-forward \
service/mydeploy-litellm \
8000:8000
Your OpenAI proxy server is now running on http://127.0.0.1:8000
.
Best Practices for Deploying to Production
1. Switch of debug logs in production
don't use --detailed-debug
, --debug
or litellm.set_verbose=True
. We found using debug logs can add 5-10% latency per LLM API call
Advanced Deployment Settings
Customization of the server root path
In a Kubernetes deployment, it's possible to utilize a shared DNS to host multiple applications by modifying the virtual service
Customize the root path to eliminate the need for employing multiple DNS configurations during deployment.
👉 Set SERVER_ROOT_PATH
in your .env and this will be set as your server root path
Setting SSL Certification
Use this, If you need to set ssl certificates for your on prem litellm proxy
Pass ssl_keyfile_path
(Path to the SSL keyfile) and ssl_certfile_path
(Path to the SSL certfile) when starting litellm proxy
docker run ghcr.io/berriai/litellm:main-latest \
--ssl_keyfile_path ssl_test/keyfile.key \
--ssl_certfile_path ssl_test/certfile.crt
Provide an ssl certificate when starting litellm proxy server
Platform-specific Guide
- AWS Cloud Formation Stack
- Google Cloud Run
- Render deploy
- Railway
AWS Cloud Formation Stack
LiteLLM AWS Cloudformation Stack - Get the best LiteLLM AutoScaling Policy and Provision the DB for LiteLLM Proxy
This will provision:
- LiteLLMServer - EC2 Instance
- LiteLLMServerAutoScalingGroup
- LiteLLMServerScalingPolicy (autoscaling policy)
- LiteLLMDB - RDS::DBInstance
Using AWS Cloud Formation Stack
LiteLLM Cloudformation stack is located here - litellm.yaml
1. Create the CloudFormation Stack:
In the AWS Management Console, navigate to the CloudFormation service, and click on "Create Stack."
On the "Create Stack" page, select "Upload a template file" and choose the litellm.yaml file
Now monitor the stack was created successfully.
2. Get the Database URL:
Once the stack is created, get the DatabaseURL of the Database resource, copy this value
3. Connect to the EC2 Instance and deploy litellm on the EC2 container
From the EC2 console, connect to the instance created by the stack (e.g., using SSH).
Run the following command, replacing <database_url> with the value you copied in step 2
docker run --name litellm-proxy \
-e DATABASE_URL=<database_url> \
-p 4000:4000 \
ghcr.io/berriai/litellm-database:main-latest
4. Access the Application:
Once the container is running, you can access the application by going to http://<ec2-public-ip>:4000
in your browser.
Deploy on Google Cloud Run
Click the button to deploy to Google Cloud Run
Testing your deployed proxy
Assuming the required keys are set as Environment Variables
https://litellm-7yjrj3ha2q-uc.a.run.app is our example proxy, substitute it with your deployed cloud run app
curl https://litellm-7yjrj3ha2q-uc.a.run.app/v1/chat/completions \
-H "Content-Type: application/json" \
-d '{
"model": "gpt-3.5-turbo",
"messages": [{"role": "user", "content": "Say this is a test!"}],
"temperature": 0.7
}'
Deploy on Render https://render.com/
Deploy on Railway https://railway.app
Step 1: Click the button to deploy to Railway
Step 2: Set PORT
= 4000 on Railway Environment Variables
Extras
Run with docker compose
Step 1
- (Recommended) Use the example file
docker-compose.yml
given in the project root. e.g. https://github.com/BerriAI/litellm/blob/main/docker-compose.yml
Here's an example docker-compose.yml
file
version: "3.9"
services:
litellm:
build:
context: .
args:
target: runtime
image: ghcr.io/berriai/litellm:main-latest
ports:
- "8000:8000" # Map the container port to the host, change the host port if necessary
volumes:
- ./litellm-config.yaml:/app/config.yaml # Mount the local configuration file
# You can change the port or number of workers as per your requirements or pass any new supported CLI augument. Make sure the port passed here matches with the container port defined above in `ports` value
command: [ "--config", "/app/config.yaml", "--port", "8000", "--num_workers", "8" ]
# ...rest of your docker-compose config if any
Step 2
Create a litellm-config.yaml
file with your LiteLLM config relative to your docker-compose.yml
file.
Check the config doc here
Step 3
Run the command docker-compose up
or docker compose up
as per your docker installation.
Use
-d
flag to run the container in detached mode (background) e.g.docker compose up -d
Your LiteLLM container should be running now on the defined port e.g. 8000
.