|
| 1 | +# SDK Header Support |
| 2 | + |
| 3 | +LiteLLM SDK provides comprehensive support for passing additional headers with API requests. This is essential for enterprise environments using API gateways, service meshes, and multi-tenant architectures. |
| 4 | + |
| 5 | +## Overview |
| 6 | + |
| 7 | +Headers can be passed to LiteLLM in three ways, with the following priority order: |
| 8 | +1. **Request-specific headers** (highest priority) |
| 9 | +2. **extra_headers parameter** |
| 10 | +3. **Global litellm.headers** (lowest priority) |
| 11 | + |
| 12 | +When the same header key is specified in multiple places, the higher priority value will be used. |
| 13 | + |
| 14 | +## Usage Methods |
| 15 | + |
| 16 | +### 1. Global Headers (litellm.headers) |
| 17 | + |
| 18 | +Set headers that will be included in all API requests: |
| 19 | + |
| 20 | +```python |
| 21 | +import litellm |
| 22 | + |
| 23 | +# Set global headers for all requests |
| 24 | +litellm.headers = { |
| 25 | + "X-API-Gateway-Key": "your-gateway-key", |
| 26 | + "X-Company-ID": "acme-corp", |
| 27 | + "X-Environment": "production" |
| 28 | +} |
| 29 | + |
| 30 | +# Now all completion calls will include these headers |
| 31 | +response = litellm.completion( |
| 32 | + model="claude-3-5-sonnet-latest", |
| 33 | + messages=[{"role": "user", "content": "Hello"}] |
| 34 | +) |
| 35 | +``` |
| 36 | + |
| 37 | +### 2. Per-Request Headers (extra_headers) |
| 38 | + |
| 39 | +Pass headers for specific requests using the `extra_headers` parameter: |
| 40 | + |
| 41 | +```python |
| 42 | +import litellm |
| 43 | + |
| 44 | +response = litellm.completion( |
| 45 | + model="gpt-4", |
| 46 | + messages=[{"role": "user", "content": "Hello"}], |
| 47 | + extra_headers={ |
| 48 | + "X-Request-ID": "req-12345", |
| 49 | + "X-Tenant-ID": "tenant-abc", |
| 50 | + "X-Custom-Auth": "bearer-token-xyz" |
| 51 | + } |
| 52 | +) |
| 53 | +``` |
| 54 | + |
| 55 | +### 3. Request Headers (headers parameter) |
| 56 | + |
| 57 | +Use the `headers` parameter for the highest priority header control: |
| 58 | + |
| 59 | +```python |
| 60 | +import litellm |
| 61 | + |
| 62 | +response = litellm.completion( |
| 63 | + model="claude-3-5-sonnet-latest", |
| 64 | + messages=[{"role": "user", "content": "Hello"}], |
| 65 | + headers={ |
| 66 | + "X-Priority-Header": "high-priority-value", |
| 67 | + "Authorization": "Bearer custom-token" |
| 68 | + } |
| 69 | +) |
| 70 | +``` |
| 71 | + |
| 72 | +### 4. Combining All Methods |
| 73 | + |
| 74 | +You can combine all three methods. Headers will be merged with the priority order: |
| 75 | + |
| 76 | +```python |
| 77 | +import litellm |
| 78 | + |
| 79 | +# Global headers (lowest priority) |
| 80 | +litellm.headers = { |
| 81 | + "X-Company-ID": "acme-corp", |
| 82 | + "X-Shared-Header": "global-value" |
| 83 | +} |
| 84 | + |
| 85 | +response = litellm.completion( |
| 86 | + model="gpt-4", |
| 87 | + messages=[{"role": "user", "content": "Hello"}], |
| 88 | + extra_headers={ |
| 89 | + "X-Request-ID": "req-12345", |
| 90 | + "X-Shared-Header": "extra-value" # Overrides global |
| 91 | + }, |
| 92 | + headers={ |
| 93 | + "X-Priority-Header": "important", |
| 94 | + "X-Shared-Header": "request-value" # Overrides both global and extra |
| 95 | + } |
| 96 | +) |
| 97 | + |
| 98 | +# Final headers sent to API: |
| 99 | +# { |
| 100 | +# "X-Company-ID": "acme-corp", # From global |
| 101 | +# "X-Request-ID": "req-12345", # From extra_headers |
| 102 | +# "X-Priority-Header": "important", # From headers |
| 103 | +# "X-Shared-Header": "request-value" # From headers (highest priority) |
| 104 | +# } |
| 105 | +``` |
| 106 | + |
| 107 | +## Enterprise Use Cases |
| 108 | + |
| 109 | +### API Gateway Integration (Apigee, Kong, AWS API Gateway) |
| 110 | + |
| 111 | +```python |
| 112 | +import litellm |
| 113 | + |
| 114 | +# Set up headers for API gateway routing and authentication |
| 115 | +litellm.headers = { |
| 116 | + "X-API-Gateway-Key": "your-gateway-key", |
| 117 | + "X-Route-Version": "v2" |
| 118 | +} |
| 119 | + |
| 120 | +# Per-tenant requests |
| 121 | +response = litellm.completion( |
| 122 | + model="claude-3-5-sonnet-latest", |
| 123 | + messages=[{"role": "user", "content": "Analyze this data"}], |
| 124 | + extra_headers={ |
| 125 | + "X-Tenant-ID": "tenant-123", |
| 126 | + "X-Department": "engineering" |
| 127 | + } |
| 128 | +) |
| 129 | +``` |
| 130 | + |
| 131 | +### Service Mesh (Istio, Linkerd) |
| 132 | + |
| 133 | +```python |
| 134 | +import litellm |
| 135 | + |
| 136 | +response = litellm.completion( |
| 137 | + model="gpt-4", |
| 138 | + messages=[{"role": "user", "content": "Hello"}], |
| 139 | + extra_headers={ |
| 140 | + "X-Trace-ID": "trace-abc-123", |
| 141 | + "X-Service-Name": "ai-service", |
| 142 | + "X-Version": "1.2.3" |
| 143 | + } |
| 144 | +) |
| 145 | +``` |
| 146 | + |
| 147 | +### Multi-Tenant SaaS Applications |
| 148 | + |
| 149 | +```python |
| 150 | +import litellm |
| 151 | + |
| 152 | +def make_ai_request(user_id, tenant_id, content): |
| 153 | + return litellm.completion( |
| 154 | + model="claude-3-5-sonnet-latest", |
| 155 | + messages=[{"role": "user", "content": content}], |
| 156 | + extra_headers={ |
| 157 | + "X-User-ID": user_id, |
| 158 | + "X-Tenant-ID": tenant_id, |
| 159 | + "X-Request-Time": str(int(time.time())) |
| 160 | + } |
| 161 | + ) |
| 162 | + |
| 163 | +# Usage |
| 164 | +response = make_ai_request("user-456", "tenant-org-1", "Help me write code") |
| 165 | +``` |
| 166 | + |
| 167 | +### Request Tracing and Debugging |
| 168 | + |
| 169 | +```python |
| 170 | +import litellm |
| 171 | +import uuid |
| 172 | + |
| 173 | +def traced_completion(model, messages, **kwargs): |
| 174 | + trace_id = str(uuid.uuid4()) |
| 175 | + |
| 176 | + return litellm.completion( |
| 177 | + model=model, |
| 178 | + messages=messages, |
| 179 | + extra_headers={ |
| 180 | + "X-Trace-ID": trace_id, |
| 181 | + "X-Debug-Mode": "true", |
| 182 | + "X-Source-Service": "my-app" |
| 183 | + }, |
| 184 | + **kwargs |
| 185 | + ) |
| 186 | + |
| 187 | +# Usage |
| 188 | +response = traced_completion( |
| 189 | + model="gpt-4", |
| 190 | + messages=[{"role": "user", "content": "Debug this issue"}] |
| 191 | +) |
| 192 | +``` |
| 193 | + |
| 194 | +### Custom Authentication |
| 195 | + |
| 196 | +```python |
| 197 | +import litellm |
| 198 | + |
| 199 | +def get_custom_auth_token(): |
| 200 | + # Your custom authentication logic |
| 201 | + return "custom-auth-token" |
| 202 | + |
| 203 | +response = litellm.completion( |
| 204 | + model="claude-3-5-sonnet-latest", |
| 205 | + messages=[{"role": "user", "content": "Hello"}], |
| 206 | + headers={ |
| 207 | + "X-Custom-Auth": get_custom_auth_token(), |
| 208 | + "X-Auth-Type": "custom" |
| 209 | + } |
| 210 | +) |
| 211 | +``` |
| 212 | + |
| 213 | +## Provider Support |
| 214 | + |
| 215 | +Headers are supported across all LiteLLM providers including: |
| 216 | + |
| 217 | +- **OpenAI** (GPT models) |
| 218 | +- **Anthropic** (Claude models) |
| 219 | +- **Cohere** |
| 220 | +- **Hugging Face** |
| 221 | +- **Custom providers** |
| 222 | +- **Azure OpenAI** |
| 223 | +- **AWS Bedrock** |
| 224 | +- **Google Vertex AI** |
| 225 | + |
| 226 | +Each provider will receive your custom headers along with their required authentication and API-specific headers. |
| 227 | + |
| 228 | +## Best Practices |
| 229 | + |
| 230 | +### 1. Use Meaningful Header Names |
| 231 | +```python |
| 232 | +# Good |
| 233 | +extra_headers = { |
| 234 | + "X-Request-ID": "req-12345", |
| 235 | + "X-Tenant-ID": "org-456" |
| 236 | +} |
| 237 | + |
| 238 | +# Avoid |
| 239 | +extra_headers = { |
| 240 | + "custom1": "value1", |
| 241 | + "h2": "value2" |
| 242 | +} |
| 243 | +``` |
| 244 | + |
| 245 | +### 2. Include Tracing Information |
| 246 | +```python |
| 247 | +extra_headers = { |
| 248 | + "X-Trace-ID": trace_id, |
| 249 | + "X-Span-ID": span_id, |
| 250 | + "X-Service-Name": "ai-service" |
| 251 | +} |
| 252 | +``` |
| 253 | + |
| 254 | +### 3. Handle Sensitive Information Carefully |
| 255 | +```python |
| 256 | +# Don't log sensitive headers |
| 257 | +import os |
| 258 | + |
| 259 | +if os.getenv("ENVIRONMENT") != "production": |
| 260 | + extra_headers["X-Debug-User"] = user_id |
| 261 | +``` |
| 262 | + |
| 263 | +### 4. Use Environment-Specific Headers |
| 264 | +```python |
| 265 | +import os |
| 266 | + |
| 267 | +environment = os.getenv("ENVIRONMENT", "development") |
| 268 | + |
| 269 | +litellm.headers = { |
| 270 | + "X-Environment": environment, |
| 271 | + "X-Service-Version": os.getenv("SERVICE_VERSION", "unknown") |
| 272 | +} |
| 273 | +``` |
| 274 | + |
| 275 | +## Troubleshooting |
| 276 | + |
| 277 | +### Headers Not Being Passed |
| 278 | + |
| 279 | +If your headers aren't reaching the API: |
| 280 | + |
| 281 | +1. **Check Header Names**: Ensure header names don't conflict with provider-specific headers |
| 282 | +2. **Verify Priority**: Remember that `headers` > `extra_headers` > `litellm.headers` |
| 283 | +3. **Test with Logging**: Enable verbose logging to see what headers are being sent |
| 284 | + |
| 285 | +```python |
| 286 | +import litellm |
| 287 | + |
| 288 | +# Enable debug logging |
| 289 | +litellm.set_verbose = True |
| 290 | + |
| 291 | +response = litellm.completion( |
| 292 | + model="gpt-4", |
| 293 | + messages=[{"role": "user", "content": "test"}], |
| 294 | + extra_headers={"X-Debug": "test"} |
| 295 | +) |
| 296 | +``` |
| 297 | + |
| 298 | +### Gateway or Proxy Issues |
| 299 | + |
| 300 | +If using API gateways or proxies: |
| 301 | + |
| 302 | +1. **Check Gateway Requirements**: Verify required headers for your gateway |
| 303 | +2. **Test Direct vs Gateway**: Compare direct API calls vs gateway calls |
| 304 | +3. **Validate Header Format**: Some gateways have header format requirements |
| 305 | + |
| 306 | +## Security Considerations |
| 307 | + |
| 308 | +1. **Don't Log Sensitive Headers**: Avoid logging authentication tokens or personal data |
| 309 | +2. **Use HTTPS**: Always use secure connections when passing sensitive headers |
| 310 | +3. **Validate Header Values**: Sanitize user-provided header values |
| 311 | +4. **Rotate Keys**: Regularly rotate any API keys passed in headers |
| 312 | + |
| 313 | +```python |
| 314 | +import litellm |
| 315 | +import re |
| 316 | + |
| 317 | +def safe_header_value(value): |
| 318 | + # Remove potentially dangerous characters |
| 319 | + return re.sub(r'[^\w\-.]', '', str(value)) |
| 320 | + |
| 321 | +response = litellm.completion( |
| 322 | + model="gpt-4", |
| 323 | + messages=[{"role": "user", "content": "Hello"}], |
| 324 | + extra_headers={ |
| 325 | + "X-User-ID": safe_header_value(user_id) |
| 326 | + } |
| 327 | +) |
| 328 | +``` |
0 commit comments