惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Jina AI
Jina AI
博客园 - Franky
Apple Machine Learning Research
Apple Machine Learning Research
酷 壳 – CoolShell
酷 壳 – CoolShell
阮一峰的网络日志
阮一峰的网络日志
量子位
雷峰网
雷峰网
宝玉的分享
宝玉的分享
V
Visual Studio Blog
博客园_首页
小众软件
小众软件
The Cloudflare Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
大猫的无限游戏
大猫的无限游戏
博客园 - 聂微东
S
SegmentFault 最新的问题
博客园 - 【当耐特】
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
博客园 - 叶小钗
月光博客
月光博客
博客园 - 三生石上(FineUI控件)
人人都是产品经理
人人都是产品经理
WordPress大学
WordPress大学

DEV Community

Authentication Security Deep Dive: From Brute Force to Salted Hashing (With Java Examples) Why AI Systems Don’t Fail — They Drift Spilling beans for how i learn for exam😁"Reinforcement Learning Cheat Sheet" I Replaced Chrome with Safari for AI Browser Automation. Here's What Broke (and What Finally Worked) How Python Borrows Other People's Work The $40 Architecture: Processing 1 Billion API Requests with 99.99% Uptime Vibe Coding: A Workflow Guide (From Zero to SaaS) Most webhook security guides protect the wrong side. The scary part is delivery. Headless CMS for TanStack Start: Build a Blog with Cosmic EU Age Verification App "Hacked in 2 Minutes" — What Actually Happened Comfy Cloud’s delete function does not actually remove files Running AI Models on GPU Cloud Servers: A Beginner Guide Event-driven media intelligence with AWS Step Functions and Bedrock I scored 500 AI prompts across 8 quality dimensions — here's what broke How to Call Google Gemini API from Next.js (Free Tier, No Backend Needed) The Portal Protocol: Reclaiming Human Connection in the Age of AI How to Fix Your Team's Scattered Knowledge Problem With a Self-Hosted Forum Intro to tc Cloud Functors: A Graph-First Mental Model for the Modern Cloud Designing Multi-Tenant Backends With Both Ownership and Team Access I Built a Neumorphic CSS Library with 77+ Components — Here's What I Learned PostgreSQL Performance Optimization: Why Connection Pooling Is Critical at Scale Cómo construí un SaaS multi-rubro para gestionar expensas en Argentina con FastAPI + Vue 3 🚀 I Built an Ethical Hacking Scanner Tool – Open Source Project I Replaced /usage and /context in Claude Code With a Single Statusline A Pythonic Way to Handle Emails (IMAP/SMTP) with Auto-Discovery and AI-Ready Design I Collected 8.9 Million Polymarket Price Points — Here's What I Found About How Markets Really Move EcoTrack AI — Carbon Footprint Tracker & Dashboard Everyone's Using AI. No One Agrees How. 5 self-hosted ebook managers worth trying in 2026 Building Your First AI Agent with LangChain: From Chatbot to Autonomous Assistant
The #3 Production Killer in Your LiteLLM Setup: Key Cache...
Hanlin Xiang · 2026-06-19 · via DEV Community

Hanlin Xiang

This is the pitfall that cost me 3 hours at 2 AM. If you're running LiteLLM Proxy in production, it will hit you too — usually at the worst possible time.


What Happened

I run LiteLLM Proxy + New API in front of 18 provider channels. One night, I rotated an API key for a provider that had been flagged for unusual spending.

Standard procedure:

  1. Generate new key in provider dashboard
  2. Update config.yaml with new key
  3. Run litellm --config config.yaml --reload

The reload succeeded. No errors. The config showed the new key. I went to sleep.

The next morning, the old key was still being used. Every single request was still authenticating with the rotated-out key. The provider's dashboard showed traffic from both keys — the new one (from config validation) and the old one (from actual API calls).

Why It Happens

LiteLLM caches API keys in-memory for performance. When you --reload, the config is reloaded, but the key store is not purged. The worker process holds the old keys in a dictionary that persists across config reloads.

This means:

  • config.yaml shows the new key ✅
  • litellm --model_cost_map shows the new key ✅
  • The actual HTTP requests use the old key ❌

You won't notice until the old key expires or is revoked — at which point every request to that provider starts returning 401, and your fallback chain kicks in, routing traffic to your most expensive model.

The Fix

Option 1: Purge the cache manually (no downtime)

curl -X POST http://localhost:4000/cache/purge \
  -H "Authorization: Bearer $LITELLM_MASTER_KEY"

This clears the in-memory key cache. The next request will pull the key from the freshly reloaded config.

Option 2: Use Redis for shared key state (recommended for multi-worker)

Set REDIS_HOST in your environment:

# docker-compose.yml
environment:
  - REDIS_HOST=redis://redis:6379
  - REDIS_CONNECTION_POOL_SIZE=5

With Redis, keys are stored externally. A config reload triggers a Redis key update, and all workers pick it up immediately. No stale keys.

Option 3: Restart the worker (downtime: 2-5 seconds)

docker restart litellm-proxy

Brute force, but guaranteed to work. Use this if you're in a hurry and can afford a brief blip.

How to Detect It Before Users Do

Add this to your monitoring — a simple script that checks whether the key in config matches the key actually being used:

# Check which key is being used for a specific model
curl -s http://localhost:4000/v1/chat/completions \
  -H "Authorization: Bearer $LITELLM_API_KEY" \
  -d '{"model": "openai/gpt-4o", "messages": [{"role": "user", "content": "test"}], "max_tokens": 1}' \
  | jq '.usage'

# Compare with the key in config
grep "api_key:" config.yaml | head -1

If the provider's response includes a x-api-key-id header (OpenAI does), you can verify which key was used without guessing.

The Bigger Picture

Key cache invalidation is Pitfall #3 in my production survival map. There are 4 more deployment pitfalls and 3 hidden cost traps that I documented after 6 months of running this stack:

  1. 503 on every request after adding a provider — model name mismatch
  2. Costs 3× higher than expected — fallback chain hits expensive models by default
  3. Keys rotated but old ones still workthis one
  4. Streaming responses cut off mid-token — Nginx/Cloudflare buffering
  5. New API channels show "insufficient quota" with balance > 0 — weight = 0 by default

Each of these took me 1-2 hours to diagnose in production. The full one-page reference card with all 5 pitfalls, 3 cost traps, a failure decision tree, and a pre-launch security checklist is available here:

👉 AI API Gateway Pitfall Map — $9

It's the page you print and pin next to your monitor — because when your gateway goes down at 2 AM, you won't be reading a 40-page guide.