LiteLLM Proxy Setup Guide¶
This guide explains how to configure and integrate the LiteLLM proxy with the SaaS API.
⚠️ Critical Setup Requirement¶
You MUST configure LiteLLM with provider credentials BEFORE your SaaS API will work.
The LiteLLM proxy is where you: 1. Store your LLM provider API keys (OpenAI, Anthropic, etc.) 2. Configure which models are available 3. Set up model parameters and routing
The LiteLLM UI must be exposed so you can configure these settings. All end-user requests will go through your SaaS API, but the admin must configure models in LiteLLM first.
Overview¶
SaasLiteLLM uses LiteLLM as a unified proxy layer for multiple LLM providers. LiteLLM provides:
- Unified API: Call 100+ LLM providers using the OpenAI format
- Cost Tracking: Automatic usage and cost tracking per team
- Rate Limiting: Built-in rate limiting and budget management
- Caching: Redis-based response caching for cost savings
- Load Balancing: Smart routing across multiple models
┌─────────────┐ ┌─────────────┐ ┌──────────────┐
│ Client │────▶│ SaaS API │────▶│ LiteLLM │
│ Application │ │ (Port 8003) │ │ (Port 8002) │
└─────────────┘ └─────────────┘ └──────┬───────┘
│
┌──────────────────────────┼───────────────┐
│ │ │
▼ ▼ ▼
┌──────────┐ ┌──────────┐ ┌──────────┐
│ OpenAI │ │ Anthropic│ │ Others │
└──────────┘ └──────────┘ └──────────┘
Quick Start: Exposing LiteLLM UI¶
1. Start LiteLLM with UI Exposed¶
In your docker-compose.yml, ensure the LiteLLM proxy port is exposed:
services:
litellm-proxy:
image: ghcr.io/berriai/litellm:main-latest
ports:
- "8002:8002" # This MUST be exposed
environment:
- LITELLM_MASTER_KEY=${LITELLM_MASTER_KEY}
- DATABASE_URL=${DATABASE_URL}
# ... other environment variables
2. Access the LiteLLM Admin UI¶
Once LiteLLM is running, access the admin dashboard:
Local Development:
Production (Railway/Cloud):
Login with your MASTER_KEY: - When prompted, enter your LITELLM_MASTER_KEY value - This is the same key from your .env file
3. Security for Production¶
When exposing LiteLLM in production:
-
Use strong authentication: Change
LITELLM_MASTER_KEYto a secure random key -
Restrict access by IP (recommended for Railway):
- Use Railway's private networking when possible
-
Or configure firewall rules to allow only your admin IPs
-
Use HTTPS: Always access via HTTPS in production
-
Monitor access: Review LiteLLM logs regularly for unauthorized access attempts
-
Separate keys: Never share
LITELLM_MASTER_KEYwith end users (they use team virtual keys)
Step-by-Step: Adding Provider Credentials¶
This is the most important setup step. Without provider credentials, your SaaS API cannot make LLM requests.
Adding OpenAI Credentials¶
- Navigate to Keys tab in LiteLLM UI
- Click + Add Key
- Fill in the details:
- Key Alias:
openai-main(or any descriptive name) - Provider: Select
openai - API Key: Paste your OpenAI API key (
sk-...) - Metadata (optional): Add notes like "Production OpenAI Key"
- Click Save
Adding Anthropic Credentials¶
- Navigate to Keys tab
- Click + Add Key
- Fill in:
- Key Alias:
anthropic-main - Provider:
anthropic - API Key: Your Anthropic key (
sk-ant-...) - Click Save
Adding Azure OpenAI Credentials¶
- Navigate to Keys tab
- Click + Add Key
- Fill in:
- Key Alias:
azure-openai - Provider:
azure - API Key: Your Azure key
- API Base:
https://your-resource.openai.azure.com - API Version:
2024-02-15-preview - Click Save
Adding Other Provider Credentials¶
LiteLLM supports 100+ providers. For each provider:
- Keys tab → + Add Key
- Select the provider from dropdown
- Enter the required fields (varies by provider)
- Save
Supported providers include: - Google (Vertex AI, PaLM) - AWS Bedrock - Cohere - Hugging Face - Replicate - Together AI - Anyscale - And many more...
See LiteLLM Providers for the complete list.
Step-by-Step: Adding Models¶
Once you have credentials configured, you can add models.
Via LiteLLM UI (Recommended)¶
- Navigate to Models tab
- Click + Add Model
- Fill in the model configuration:
Example: Adding GPT-4 Turbo
Model Name: gpt-4-turbo
LiteLLM Model Name: openai/gpt-4-turbo
Provider Credential: openai-main (select from dropdown)
Model Info:
- Mode: chat
- Input Cost (per 1K tokens): 0.01
- Output Cost (per 1K tokens): 0.03
Example: Adding Claude 3 Sonnet
Model Name: claude-3-sonnet
LiteLLM Model Name: anthropic/claude-3-sonnet-20240229
Provider Credential: anthropic-main
Model Info:
- Mode: chat
- Input Cost: 0.003
- Output Cost: 0.015
Example: Adding GPT-3.5 Turbo
Model Name: gpt-3.5-turbo
LiteLLM Model Name: openai/gpt-3.5-turbo
Provider Credential: openai-main
Model Info:
- Mode: chat
- Input Cost: 0.0005
- Output Cost: 0.0015
- Click Save Model
- The model is now available for your SaaS API teams!
Model Name Format¶
The LiteLLM Model Name follows this pattern:
Examples: - OpenAI: openai/gpt-4-turbo, openai/gpt-3.5-turbo - Anthropic: anthropic/claude-3-opus-20240229 - Azure: azure/gpt-4 (requires API base configured in credentials) - Cohere: cohere/command-r-plus - Bedrock: bedrock/anthropic.claude-v2
Testing Your Models¶
After adding a model, test it in the LiteLLM UI:
- Navigate to Playground tab
- Select your model from the dropdown
- Enter a test prompt: "Hello, are you working?"
- Click Send
- Verify you get a response
If you get an error: - Check that the provider credential is correct - Verify the model name format - Ensure your provider API key has access to that model - Review the error message in the LiteLLM logs
Authentication Flow¶
LITELLM_MASTER_KEY¶
The LITELLM_MASTER_KEY is used for:
- Admin access to LiteLLM's management UI (
http://localhost:8002/ui) - SaaS API authentication when creating virtual keys for teams
- Direct LiteLLM API access (not recommended for end users)
Virtual Team Keys¶
The SaaS API creates virtual keys for each team automatically:
- Teams don't use
LITELLM_MASTER_KEYdirectly - Each team gets a unique key with budget/rate limits enforced
- Keys are managed through the SaaS API, not LiteLLM directly
Environment Configuration¶
Set these required variables in your .env file:
# LiteLLM Configuration
LITELLM_MASTER_KEY=sk-litellm-$(openssl rand -hex 32)
LITELLM_PROXY_URL=http://localhost:8002
LITELLM_CONFIG_PATH=src/config/litellm_config.yaml
# Database (shared with SaaS API and LiteLLM)
DATABASE_URL=postgresql://postgres:postgres@localhost:5432/saas_llm_db
# Redis (for caching and rate limiting)
REDIS_HOST=localhost
REDIS_PORT=6379
REDIS_PASSWORD=your-redis-password
Note: Provider API keys (OpenAI, Anthropic, etc.) are added through the LiteLLM UI, not as environment variables. This keeps credentials secure and allows dynamic management without restarts.
Complete Setup Workflow¶
Here's the complete workflow from zero to working SaaS LLM API:
Step 1: Start Services¶
Wait for all services to be healthy.
Step 2: Configure LiteLLM (REQUIRED)¶
- Access LiteLLM UI: http://localhost:8002/ui
- Login with your
LITELLM_MASTER_KEY - Add Provider Credentials:
- Go to Keys tab
- Add OpenAI key (or your preferred provider)
- Example: Alias=
openai-main, Provider=openai, Key=sk-... - Add Models:
- Go to Models tab
- Add at least one model
- Example: Name=
gpt-3.5-turbo, LiteLLM Name=openai/gpt-3.5-turbo, Credential=openai-main - Test Model:
- Go to Playground tab
- Select your model and send a test message
- Verify you get a response
Step 3: Configure SaaS API¶
- Access Admin Panel: http://localhost:3000
- Login with your
MASTER_KEY(from SaaS API .env) - Create Organization:
- Go to Organizations
- Create your first organization
- Create Model Group:
- Go to Model Groups
- Create a group with the models you added in LiteLLM
- Example: Name=
standard-models, Models=gpt-3.5-turbo, claude-3-sonnet - Create Team:
- Go to Teams
- Create a team under your organization
- Assign the model group
- Set budget (credits)
Step 4: Test End-to-End¶
Your team now has a virtual key. Test the complete flow:
# Get team info (includes virtual_key)
curl http://localhost:8003/api/teams/{team_id} \
-H "X-Admin-Key: $MASTER_KEY"
# Use team's virtual key to make LLM request
curl -X POST http://localhost:8002/chat/completions \
-H "Authorization: Bearer $TEAM_VIRTUAL_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "gpt-3.5-turbo",
"messages": [{"role": "user", "content": "Hello!"}]
}'
✅ If you get a response, your setup is complete!
Model Groups (SaaS API Feature)¶
The SaaS API adds Model Groups on top of LiteLLM models:
- Group models together: Create logical groupings like "fast-models", "premium-models"
- Control team access: Assign specific model groups to teams
- Flexible pricing: Different teams can have different model access
Important: Model names in your Model Groups must match the model names you configured in LiteLLM.
Example workflow:
- ✅ Add models in LiteLLM UI (e.g.,
gpt-4-turbo,claude-3-sonnet) - Create model groups in SaaS API admin panel
- Add the same model names to your groups
- Assign groups to teams
- Teams can now use those models via their virtual keys
Testing the Setup¶
1. Test LiteLLM Directly¶
Test that LiteLLM can reach your providers:
curl -X POST http://localhost:8002/chat/completions \
-H "Authorization: Bearer $LITELLM_MASTER_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "gpt-3.5-turbo",
"messages": [{"role": "user", "content": "Hello!"}]
}'
2. Test SaaS API → LiteLLM Connection¶
Create a test organization and team:
curl -X POST http://localhost:8003/api/organizations/create \
-H "X-Admin-Key: $MASTER_KEY" \
-H "Content-Type: application/json" \
-d '{
"name": "Test Org",
"litellm_organization_id": "test-org"
}'
curl -X POST http://localhost:8003/api/teams/create \
-H "X-Admin-Key: $MASTER_KEY" \
-H "Content-Type: application/json" \
-d '{
"name": "Test Team",
"organization_id": "org_...",
"litellm_team_id": "test-team",
"max_budget": 100.0
}'
The SaaS API will create a virtual key in LiteLLM for the team.
3. Test with Team Key¶
Use the team's virtual key to make requests:
curl -X POST http://localhost:8002/chat/completions \
-H "Authorization: Bearer $TEAM_VIRTUAL_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "gpt-3.5-turbo",
"messages": [{"role": "user", "content": "Hello from team!"}]
}'
Configuration Reference¶
litellm_config.yaml Structure¶
# Model definitions (if not using database)
model_list: []
# General settings
general_settings:
master_key: os.environ/LITELLM_MASTER_KEY
database_url: os.environ/DATABASE_URL
store_model_in_db: true # Use database for model management
# Redis caching
cache: true
cache_params:
type: "redis"
host: os.environ/REDIS_HOST
port: os.environ/REDIS_PORT
password: os.environ/REDIS_PASSWORD
ttl: 600 # Cache TTL in seconds
# Cost tracking
track_cost_per_deployment: true
# Router settings
router_settings:
redis_host: os.environ/REDIS_HOST
redis_port: os.environ/REDIS_PORT
redis_password: os.environ/REDIS_PASSWORD
enable_pre_call_checks: true
routing_strategy: "usage-based-routing"
num_retries: 3
timeout: 600
redis_enabled: true
Troubleshooting¶
Can't Access LiteLLM UI¶
Symptoms: Cannot reach http://localhost:8002/ui
Solutions:
-
Verify LiteLLM container is running:
-
Check that port 8002 is exposed in docker-compose.yml:
-
View LiteLLM logs for errors:
-
Ensure no other service is using port 8002:
LiteLLM UI Login Failing¶
Symptoms: Invalid credentials error when logging in
Solutions:
- Verify you're using
LITELLM_MASTER_KEY(notMASTER_KEY) - Check the key value in your
.envfile - Ensure the key doesn't have extra whitespace or quotes
- Try restarting LiteLLM:
docker-compose restart litellm-proxy
Provider Credential Not Working¶
Symptoms: "Invalid API key" errors when testing models
Solutions:
- Verify the key is valid:
- Test directly with the provider (e.g., OpenAI playground)
- Check if the key has been revoked
-
Ensure you have sufficient credits with the provider
-
Check key format:
- OpenAI: Should start with
sk-... - Anthropic: Should start with
sk-ant-... -
Azure: Varies by deployment
-
Verify provider selection:
- Make sure you selected the correct provider in the dropdown
-
Provider name must match exactly (case-sensitive)
-
Check API base (for Azure/custom endpoints):
- Ensure the API base URL is correct
-
Verify the API version is supported
-
Review LiteLLM logs:
Model Not Working¶
Symptoms: "Model not found" or model requests failing
Solutions:
- Verify model exists in LiteLLM:
- Check the Models tab in LiteLLM UI
-
Ensure the model is saved (not just added)
-
Check model name format:
- Must use provider prefix:
openai/gpt-4-turbo(not justgpt-4-turbo) -
Check for typos (case-sensitive)
-
Verify credential is linked:
- Each model must be linked to a provider credential
-
The credential must be valid
-
Test in playground:
- Use LiteLLM's Playground tab
- Try a simple prompt
-
Review error messages
-
Check provider access:
- Ensure your API key has access to that specific model
- Some models require special access (e.g., GPT-4, Claude Opus)
SaaS API Can't Reach LiteLLM¶
Symptoms: Errors when creating teams or model groups
Solutions:
-
Verify
LITELLM_PROXY_URLin SaaS API.env: -
Test connectivity from SaaS API container:
-
Check both services are on the same Docker network:
-
Verify
LITELLM_MASTER_KEYmatches in both services' .env files
Virtual Keys Not Working¶
Symptoms: Teams can't use their assigned virtual keys
Solutions:
- Verify key was created:
- Check LiteLLM UI Keys tab
-
Look for keys with team_id in metadata
-
Check team budget:
- View team details in SaaS API admin panel
- Verify credits haven't been exhausted
-
Check budget limits in LiteLLM
-
Verify model access:
- Ensure team's model group includes the requested model
-
Check model name matches exactly what's in LiteLLM
-
Review rate limits:
- Check if team has hit rate limits (RPM/TPM)
-
View limits in LiteLLM team configuration
-
Check LiteLLM logs:
Model Group Mismatch¶
Symptoms: Team can't access models despite having a model group assigned
Solutions:
- Verify model names match:
- Model names in SaaS API Model Group MUST match LiteLLM model names
-
Check for typos, extra spaces, or case differences
-
Confirm models exist in LiteLLM:
- Open LiteLLM UI → Models tab
-
Verify each model in your group exists
-
Re-create the team (if needed):
- This refreshes the virtual key configuration in LiteLLM
- Ensures latest model group is applied
Security Best Practices¶
- Never expose
LITELLM_MASTER_KEYto end users - Use strong, random keys for production:
- Rotate keys regularly (quarterly recommended)
- Set appropriate budgets on all teams to prevent runaway costs
- Monitor usage via LiteLLM dashboard
- Use rate limits to prevent abuse
- Enable Redis caching to reduce costs
Additional Resources¶
- LiteLLM Documentation
- LiteLLM Supported Providers
- LiteLLM GitHub Repository
- OpenAI API Reference
- Anthropic Claude API
Next Steps¶
- Integration Guide - Complete authentication and connection flow
- API Reference - SaaS API endpoints
- Deployment Guide - Deploy to production