High severity HTTP 401 beginner · Fix: 2-5 min

AuthenticationError

litellm.errors.AuthenticationError

What this error means
LiteLLM throws AuthenticationError when the required provider API key is missing or not set in environment variables.

Stack trace

traceback
litellm.errors.AuthenticationError: Provider API key missing. Please set your API key in the environment variable before making requests.
  File "app.py", line 12, in <module>
    client = LiteLLMClient()
  File "litellm/client.py", line 45, in __init__
    raise AuthenticationError("Provider API key missing.")
QUICK FIX
Set your provider API key in the environment variable LITELLM_API_KEY before initializing the LiteLLM client.

Why it happens

LiteLLM requires a valid provider API key to authenticate requests. If the API key environment variable is not set or is empty, the client initialization fails and raises AuthenticationError to prevent unauthorized access.

Detection

Check for AuthenticationError exceptions during client initialization or API calls. Monitor logs for missing or empty API key environment variables before making requests.

Causes & fixes

1

Environment variable for the provider API key is not set

✓ Fix

Set the required API key environment variable (e.g., LITELLM_API_KEY) before running your application.

2

Environment variable is set but empty or contains only whitespace

✓ Fix

Ensure the API key environment variable contains a valid non-empty string with no extra spaces.

3

Using incorrect environment variable name for the provider API key

✓ Fix

Verify and use the exact environment variable name expected by LiteLLM, typically LITELLM_API_KEY.

Code: broken vs fixed

Broken - triggers the error
python
from litellm import LiteLLMClient

client = LiteLLMClient()  # Raises AuthenticationError: Provider API key missing
response = client.chat('Hello')
print(response)
Fixed - works correctly
python
import os
from litellm import LiteLLMClient

os.environ['LITELLM_API_KEY'] = 'your_actual_api_key_here'  # Set your API key here
client = LiteLLMClient()  # Now initializes without error
response = client.chat('Hello')
print(response)
Added setting of the required LITELLM_API_KEY environment variable before client initialization to provide authentication credentials.

Workaround

Catch AuthenticationError exceptions and prompt the user or system to set the API key environment variable before retrying the request.

Prevention

Always configure and verify the provider API key environment variable is set in your deployment environment before starting the application to avoid authentication failures.

Python 3.7+ · litellm >=0.1.0 · tested on 0.2.0
Verified 2026-04
Verify ↗

Community Notes

No notes yetBe the first to share a version-specific fix or tip.