ConfigError
litellm.proxy.server.ConfigError
Stack trace
litellm.proxy.server.ConfigError: Missing required configuration key 'LITELLM_API_KEY' in environment variables
File "/usr/local/lib/python3.10/site-packages/litellm/proxy/server.py", line 45, in start_server
api_key = os.environ['LITELLM_API_KEY']
File "/usr/lib/python3.10/os.py", line 675, in __getitem__
raise KeyError(key)
KeyError: 'LITELLM_API_KEY' Why it happens
The LiteLLM proxy server requires specific environment variables and configuration keys to start properly. If these keys are missing, misspelled, or invalid, the server raises a ConfigError and refuses to start to prevent insecure or incomplete operation.
Detection
Check for ConfigError exceptions during proxy server startup and verify that all required environment variables like 'LITELLM_API_KEY' are set before launching the server.
Causes & fixes
Required environment variable 'LITELLM_API_KEY' is missing or not set
Set the 'LITELLM_API_KEY' environment variable with your valid LiteLLM API key before starting the proxy server.
Configuration file path is incorrect or file is missing required keys
Verify the config file path passed to the proxy server is correct and that the file contains all necessary keys like 'api_key' and 'server_port'.
Environment variables are set but contain empty or invalid values
Ensure environment variables are not empty strings and contain valid values expected by LiteLLM proxy server.
Proxy server startup code does not load environment variables before accessing them
Load environment variables early in the startup script using libraries like python-dotenv or ensure the shell environment exports them properly.
Code: broken vs fixed
import os
# Missing environment variable causes KeyError
api_key = os.environ['LITELLM_API_KEY'] # This line triggers ConfigError
print(f"Starting LiteLLM proxy with API key: {api_key}") import os
from dotenv import load_dotenv
load_dotenv() # Load env vars from .env file
api_key = os.environ.get('LITELLM_API_KEY')
if not api_key:
raise RuntimeError("LITELLM_API_KEY environment variable is required")
print(f"Starting LiteLLM proxy with API key: {api_key}") # Fixed: safely load env var Workaround
Wrap the environment variable access in try/except KeyError and provide a default or prompt the user to set the variable before retrying.
Prevention
Use a centralized configuration management system or environment variable validator during deployment to ensure all required LiteLLM proxy server config keys are present and valid before startup.