SDK Proxy Authentication (OAuth2/JWT Auto-Refresh)
Automatically obtain and refresh OAuth2/JWT tokens when using the LiteLLM Python SDK with a LiteLLM Proxy that requires JWT authentication.
Overview
When your LiteLLM Proxy is protected by an OAuth2/OIDC provider (Azure AD, Keycloak, Okta, Auth0, etc.), your SDK clients need valid JWT tokens for every request. Instead of manually managing token lifecycle, litellm.proxy_auth handles this automatically:
- Obtains tokens from your identity provider
- Caches tokens to avoid unnecessary requests
- Refreshes tokens before they expire (60-second buffer)
- Injects
Authorization: Bearer <token>headers into every request
Quick Start
Azure AD
- DefaultAzureCredential
- ClientSecretCredential
Uses the DefaultAzureCredential chain (environment variables, managed identity, Azure CLI, etc.):
import litellm
from litellm.proxy_auth import AzureADCredential, ProxyAuthHandler
# One-time setup
litellm.proxy_auth = ProxyAuthHandler(
credential=AzureADCredential(), # uses DefaultAzureCredential
scope="api://my-litellm-proxy/.default"
)
litellm.api_base = "https://my-proxy.example.com"
# All requests now include Authorization headers automatically
response = litellm.completion(
model="gpt-6.1-sol",
messages=[{"role": "user", "content": "Hello!"}]
)
Use a specific Azure AD app registration:
import litellm
from azure.identity import ClientSecretCredential
from litellm.proxy_auth import AzureADCredential, ProxyAuthHandler
azure_cred = ClientSecretCredential(
tenant_id="your-tenant-id",
client_id="your-client-id",
client_secret="your-client-secret"
)
litellm.proxy_auth = ProxyAuthHandler(
credential=AzureADCredential(credential=azure_cred),
scope="api://my-litellm-proxy/.default"
)
litellm.api_base = "https://my-proxy.example.com"
response = litellm.completion(
model="gpt-6.1-sol",
messages=[{"role": "user", "content": "Hello!"}]
)
Required package: uv add azure-identity
Generic OAuth2 (Okta, Auth0, Keycloak, etc.)
Works with any OAuth2 provider that supports the client_credentials grant type:
import litellm
from litellm.proxy_auth import GenericOAuth2Credential, ProxyAuthHandler
litellm.proxy_auth = ProxyAuthHandler(
credential=GenericOAuth2Credential(
client_id="your-client-id",
client_secret="your-client-secret",
token_url="https://your-idp.example.com/oauth2/token"
),
scope="litellm_proxy_api"
)
litellm.api_base = "https://my-proxy.example.com"
response = litellm.completion(
model="gpt-6.1-sol",
messages=[{"role": "user", "content": "Hello!"}]
)
Custom Credential Provider
Implement the TokenCredential protocol to use any authentication mechanism:
import time
import litellm
from litellm.proxy_auth import AccessToken, ProxyAuthHandler
class MyCustomCredential:
"""Any class with a get_token(scope) -> AccessToken method works."""
def get_token(self, scope: str) -> AccessToken:
# Your custom logic to obtain a token
token = my_auth_system.get_jwt(scope=scope)
return AccessToken(
token=token,
expires_on=int(time.time()) + 3600
)
litellm.proxy_auth = ProxyAuthHandler(
credential=MyCustomCredential(),
scope="my-scope"
)
Supported Endpoints
Auth headers are automatically injected for:
| Endpoint | Function |
|---|---|
| Chat Completions | litellm.completion() / litellm.acompletion() |
| Embeddings | litellm.embedding() / litellm.aembedding() |
How It Works
┌──────────┐ ┌──────────────────┐ ┌──────────────┐ ┌──────────────┐
│ Your │ │ ProxyAuthHandler │ │ Identity │ │ LiteLLM │
│ Code │──── │ (token cache) │──── │ Provider │ │ Proxy │
│ │ │ │ ────│ (Azure AD, │ │ │
│ │ │ │ │ Okta, etc) │ │ │
│ │ └────────┬─────────┘ └──────────────┘ │ │
│ │ │ Authorization: Bearer <token> │ │
│ │──────────────┼─────────────────────────────────── │ │
│ │ ─────────────┼────────────────────────────────────│ │
└──────────┘ │ └──────────────┘
- You set
litellm.proxy_authonce at startup - On each SDK call (
completion(),embedding()), the handler checks its cached token - If the token is missing or expires within 60 seconds, it requests a new one from your identity provider
- The
Authorization: Bearer <token>header is injected into the request - If token retrieval fails, a warning is logged and the request proceeds without auth headers