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

推荐订阅源

云风的 BLOG
云风的 BLOG
Security Archives - TechRepublic
Security Archives - TechRepublic
V
Vulnerabilities – Threatpost
C
CXSECURITY Database RSS Feed - CXSecurity.com
P
Proofpoint News Feed
G
GRAHAM CLULEY
P
Privacy International News Feed
The Hacker News
The Hacker News
Forbes - Security
Forbes - Security
U
Unit 42
N
News and Events Feed by Topic
D
Darknet – Hacking Tools, Hacker News & Cyber Security
C
Cyber Attacks, Cyber Crime and Cyber Security
C
Cisco Blogs
A
About on SuperTechFans
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
D
Docker
I
Intezer
Spread Privacy
Spread Privacy
The Last Watchdog
The Last Watchdog
V2EX - 技术
V2EX - 技术
S
Security @ Cisco Blogs
F
Full Disclosure
S
Secure Thoughts
M
MIT News - Artificial intelligence
Microsoft Security Blog
Microsoft Security Blog
G
Google Developers Blog
aimingoo的专栏
aimingoo的专栏
W
WeLiveSecurity
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Project Zero
Project Zero
Recorded Future
Recorded Future
Cyberwarzone
Cyberwarzone
S
Security Affairs
AWS News Blog
AWS News Blog
H
Help Net Security
The GitHub Blog
The GitHub Blog
Hacker News: Ask HN
Hacker News: Ask HN
Vercel News
Vercel News
P
Proofpoint News Feed
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
The Register - Security
The Register - Security
S
Schneier on Security
F
Fortinet All Blogs
C
CERT Recently Published Vulnerability Notes
L
LINUX DO - 最新话题
T
Tor Project blog
T
The Exploit Database - CXSecurity.com
MongoDB | Blog
MongoDB | Blog
Webroot Blog
Webroot Blog

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 Common SOC 2 Failures (Real World) Stop Vibe-Checking Your AI App: A Practical Guide to Evals How to Use SonarQube and SonarScanner Locally to Level Up Your Code Quality Your Next To-Do App Is Dead — I Replaced Mine with an OpenClaw AI Sign a Nostr event in 60 lines of Python using coincurve — no nostr-sdk, no nbxplorer, no rust toolchain ITGC Audit Explained Like You’re in Big 4 Patch Tuesday abril 2026: Microsoft parcha 163 vulnerabilidades y un zero-day en SharePoint Stop scraping everything: a better way to track competitor price changes Listing on MCPize + the Official MCP Registry while routing payments OUTSIDE the marketplace — how I kept 100% of my x402 revenue Building an AI-Powered Risk Intelligence System Using Serverless Architecture Why We Ripped Function Overloading Out of Our AI Toolchain Testing AI-Generated Code: How to Actually Know If It Works SaaS Churn Is Killing Your Business. Here Is What to Do About It (Without a Support Team) The Speed of AI Is No Longer Linear - And Self-Improving Models Are Why How to Implement RBAC for MCP Tools: A Practical Guide for Engineering Teams From Standard Quote to Persuasive Proposal: AI Automation for Arborists I built a CLI that scaffolds complete multi-tenant SaaS apps Axios CVE-2025–62718: The Silent SSRF Bug That Could Be Hiding in Your Node.js App Right Now The dashboard that ended our friendship Data Pipelines Explained Simply (and How to Build Them with Python) The Hidden Cost of AI Systems Nobody Talks About. undefined vs undeclared, and how typeof behaves Switching from file-based jobs to NATS/Kafka in Rust without changing code io_uring Adventures: Rust Servers That Love Syscalls Why Agentic AI is Killing the Traditional Database The POUR principles of web accessibility for developers and designers Quantum Neural Network 3D — A Deep Dive into Interactive WebGL Visualization How To Install Caveman In Codex On macOS And Windows Automation Pipeline Reliability: Why Your Workflow Breaks When Nobody Is Watching I Built an 'Open World' AI Coding Agent — It Works From ANY Folder From Freelancing to Product: A Tech Service Company's SaaS Transformation China's AI Giants: Adding Tencent Hunyuan & ByteDance Doubao to AI University (74 Providers) On the Vibe Coders and Their Lies clerk: Auto-Summarize Your Claude Code Sessions AI Weekly — 2026/04/10–04/17 | The Model Lockdown Is Here, but the Toolchain Is the Real Battleground AI 週報 — 2026/04/10–2026/04/17 模型封鎖潮來了,但工具鏈才是真戰場 Maybe this is how Open-Source apps are born... 🚀 Fine-Tune LLMs with LoRA and QLoRA: 2026 Guide tRPC v11 + Next.js App Router: End-to-End Type Safety Without the Boilerplate ShadCN UI in 2026: Why I Stopped Installing Component Libraries and Started Owning My Components SaaS Billing in React Server Components: Stripe + Supabase Without a Single `useEffect` Join our DEV Weekend Challenge — $1,000 in Prizes Across TEN winners! Submissions Due April 20 at 6:59 AM UTC. Implementing FSRS Spaced Repetition in Flutter + Supabase — Adding Memory Science to an AI Learning App "I Texted My Localhost From the Train — Claude Code Fixed the Bug Before I Got Home" I Built a Sales Prep AI and It Went Deeper Than Expected Design to Code #2: One JSON, Eleven Outputs Solving the 100M-Row Problem: A Summary Table Pattern for High-Volume Push Notification Logs Flutter Web With Wasm: What Actually Changes For Developers I Built 50 Royalty-Free Soundtracks for My Side Project in a Weekend Using AI Music Generation The Vibe Coding Security Checklist: 7 Things to Check Before You Ship Stop Letting Googlebot Guess Fix Your React App's SEO Right Desconstruindo o Streaming do LinkedIn: Como Criar um Engine de Extração de Vídeo de Alta Performance com HLS e FFmpeg (EDA Part-1) EDA (Exploratory Data Analysis) Explained With Real Life — Why Looking at Your Data Is the Most Important Step in Machine Learning Brand Relationship Management at Scale: Our 4-Touch Outreach System for 200+ Brands Why String.fromEnvironment() Might Return an Empty String in Dart JGuardrails 1.0.0 — Hardening Java LLM Apps Against Jailbreaks, Toxicity, and Prompt Injection Plan and Schedule a Full Week of Threads Content From One Claude Conversation Coding Cat Oran Ep3, Five Tables Changed Everything Updated: BFF Pattern I'm done watching freelancers get buried by 200 proposals. So I'm building the alternative. This is my first post BFS Algorithm in Java Step by Step Tutorial with Examples Tracking LLM Pricing Monthly: An Open Dataset for 22 AI Models How We Measure Content ROI on a Comparison Site: Revenue Attribution Without Perfect Data Introducing Nova AI Ops: The AI-Native Operating System for SRE Teams I built a free desktop video downloader for Windows — Grabbit How Talkie OCR Helps Vision-Impaired & Dyslexic Users Read the World Around Them VRCFaceTracking安装和iPhone面捕配置教程,有bug Even CrowdStrike Can't See Your Agents The Automation Gold Rush: What n8n Workflows and Claude Are Opening Up for Developers Right Now
The 5 Cost Traps That Will Quietly Bleed Your AI API Gateway Dry (And How to Fix Them)
Hanlin Xiang · 2026-06-22 · via DEV Community

In my last post, we talked about key cache invalidation — the silent production killer that turns your gateway into a 502 factory. Today I want to talk about something equally dangerous but far more insidious: cost traps.

These aren't bugs. They're not crashes. Your gateway runs fine. Your users are happy. Then finance sends you a Slack message: "Why did our OpenAI bill jump 4x last month?"

I've been running LiteLLM Proxy in production for multiple teams across three companies. Here are the five cost traps I've personally been burned by — each with the config that would have saved me thousands of dollars.


Trap #1: The Retry Spiral — When num_retries=3 Actually Means 15

The Problem

LiteLLM's retry logic is smart. Too smart. When a request fails, it retries. When the retried request hits a fallback model and that fails, it retries again. If you've configured a fallback chain of 5 models with 3 retries each, a single user request can trigger up to 15 upstream API calls — and you pay for every single one, including the ones that errored out after consuming tokens.

Why It Happens

The default num_retries in LiteLLM is 3. Most teams set it and forget it. But retries multiply across your fallback chain. Here's the math:

Request → Model A fails → Retry A (1) → Retry A (2) → Retry A (3)
       → Fallback to Model B → Fails → Retry B (1) → Retry B (2) → Retry B (3)
       → Fallback to Model C → Fails → Retry C (1) → Retry C (2) → Retry C (3)

Total upstream calls: 9 retries + 3 initial = 12 billable calls for 1 user request

If Model C is GPT-4o and each retry consumes 2K input tokens before timing out, that's 24K tokens on a single failed request.

The Fix

Cap total attempts across the entire chain, not just per-model:

# litellm_config.yaml
litellm_settings:
  num_retries: 2           # per-model retries
  max_fallbacks: 2         # hard cap on fallback chain depth
  retry_after: 5           # respect 429 Retry-After headers
  allowed_fails: 3         # circuit breaker: after 3 fails, stop entirely

model_list:
  - model_name: gpt-4o
    litellm_params:
      model: gpt-4o
      max_retries: 2       # override: fewer retries on expensive models

  - model_name: gpt-4o-fallback
    litellm_params:
      model: gpt-4o-mini   # cheap fallback, not another expensive model

The key insight: your fallback should be cheaper than your primary, not equally expensive. If GPT-4o fails, fall back to GPT-4o-mini, not to Claude Opus.


Trap #2: Fallback Chains That Funnel Money Into Premium Models

The Problem

This is the trap that cost me $2,300 in a single weekend. A well-meaning engineer configured a fallback chain that looked like this:

# THE EXPENSIVE WAY — do not do this
router_settings:
  fallbacks:
    - "gpt-4o-mini": ["gpt-4o"]
    - "gpt-4o": ["claude-3-5-sonnet"]
    - "claude-3-5-sonnet": ["claude-3-opus"]

The logic seemed sound: "If the cheap model fails, try the better one." But here's what actually happened: GPT-4o-mini was rate-limited during a traffic spike (429s everywhere), so every single request fell through to GPT-4o and then to Claude 3.5 Sonnet. For 6 hours, we were running 100% of our traffic on the most expensive models in the chain.

Why It Happens

Rate limits are per-model, not per-gateway. When you hit OpenAI's TPM limit on gpt-4o-mini, LiteLLM dutifully falls back. But if the traffic spike is caused by overall volume (not a model-specific outage), the fallback model gets the same volume that caused the 429 in the first place. You're not solving the problem — you're just paying 10x more to have it on a different model.

The Fix

Structure fallbacks by cost tier, not by capability tier:

# THE SMART WAY — fallback within price tier, not up
router_settings:
  fallbacks:
    # Tier 1: Cheap models (fallback to other cheap models)
    - "gpt-4o-mini": ["gemini-1.5-flash", "claude-3-haiku"]

    # Tier 2: Mid-tier models (fallback to other mid-tier)
    - "gpt-4o": ["claude-3-5-sonnet", "gemini-1.5-pro"]

    # NEVER fall up from cheap to expensive
    # If all cheap models fail, return an error, don't escalate

  # Add a cooldown so the same model isn't retried immediately
  cooldown_time: 60

Also add alerting. If your fallback rate exceeds 5% of total traffic, something is structurally wrong:

# prometheus metric in your LiteLLM custom callback
from litellm.integrations.custom_logger import CustomLogger
import litellm

class FallbackAlertLogger(CustomLogger):
    def log_pre_api_call(self, model, messages, kwargs):
        if kwargs.get("metadata", {}).get("fallback_idx", 0) > 0:
            # This is a fallback call, not the primary
            self.fallback_counter.inc()
            # Alert if fallback rate > 5%
            if self.fallback_counter._value.get() / self.total_counter._value.get() > 0.05:
                self.alert_webhook.send(
                    "⚠️ Fallback rate >5% — check rate limits on primary models"
                )


Trap #3: Zero Caching = Paying for the Same Answer 1,000 Times

The Problem

Most teams don't enable LiteLLM's built-in caching because "our prompts are dynamic." But in practice, a huge percentage of your traffic is near-identical: system prompts are the same, the first 500 tokens of user messages are often boilerplate, and many users ask the exact same questions.

I audited one team's traffic and found that 34% of their requests were exact duplicates of requests made in the last hour. They were paying OpenAI ~$400/day for identical completions.

Why It Happens

LiteLLM has Redis caching built in. But it's disabled by default, and the documentation buries it under "Advanced Settings." Most engineers set up the proxy, test it, ship it, and never circle back.

The Fix

Enable Redis caching with a sensible TTL. This is a 30-second config change that can cut your bill by 30-50%:

litellm_settings:
  cache: true
  cache_params:
    type: "redis"
    host: "your-redis-host"
    port: 6379
    namespace: "litellm_cache"

    # Cache settings
    ttl: 3600              # 1 hour for exact matches
    # For semantic caching (similar but not identical prompts):
    # semantic_cache: true
    # similarity_threshold: 0.8

  # Cache based on messages content, not just the full request
  cache_key_include_models: true   # don't share cache across models

model_list:
  - model_name: gpt-4o
    litellm_params:
      model: gpt-4o
      cache: true           # enable per-model

For even bigger savings, use prompt caching with providers that support it (Claude, GPT-4o). LiteLLM supports this natively:

import litellm

# Enable prompt caching for Claude
response = litellm.completion(
    model="claude-3-5-sonnet",
    messages=[
        {"role": "user", "content": [
            {"type": "text", "text": "<long_system_prompt>", "cache_control": {"type": "ephemeral"}},
            {"type": "text", "text": user_input}
        ]}
    ]
)
# Claude charges 90% less for cached input tokens

Real numbers from my audit: After enabling Redis cache with a 1-hour TTL, that team went from $400/day to $180/day. A 55% reduction for a config change that took less than a minute.


Trap #4: No Per-Key Budget Limits — One Runaway Loop Can Bankrupt You

The Problem

An intern pushes a while True loop to a staging environment. It doesn't crash — it just calls your gateway 4,000 times per minute with a 4K-token prompt. By the time PagerDuty fires, you've spent $847 in 12 minutes.

This isn't hypothetical. This is a Tuesday.

Why It Happens

LiteLLM's default configuration has no budget enforcement. The max_budget field exists but most teams never configure it because they're focused on getting the gateway working, not on constraining it.

The Fix

Set budgets at three levels: per-key, per-team, and global:

# Per-virtual-key budget (when creating keys via /key/generate)
# This is your first line of defense

litellm_settings:
  # Global budget — emergency brake
  max_budget: 500          # $500/day global cap
  budget_duration: "1d"

  # Rate limiting
  rpm_limit: 1000          # requests per minute, global

general_settings:
  master_key: sk-1234
  database_url: "postgresql://..."

  # Enable budget tracking
  alerting: ["slack"]
  alerting_threshold: 0.8  # alert at 80% of budget

When creating virtual keys for teams or individual developers:

# Create a key with a $50 daily budget and 100 RPM
curl -X POST http://localhost:4000/key/generate \
  -H "Authorization: Bearer sk-1234" \
  -H "Content-Type: application/json" \
  -d '{
    "max_budget": 50,
    "budget_duration": "1d",
    "rpm_limit": 100,
    "tpm_limit": 50000,
    "models": ["gpt-4o-mini", "gpt-4o"],
    "metadata": {"team": "frontend"}
  }'

And set up a webhook to catch budget breaches:

# In your LiteLLM proxy config
litellm_settings:
  proxy_budget_respecting_alerting:
    - webhook_url: "https://hooks.slack.com/services/..."
      # This fires BEFORE the request is sent when a key is over budget
      # LiteLLM will return a 429 to the client, not forward to the provider

The intern's loop? With a $50/day key budget and 100 RPM limit, it would have been throttled after 100 calls and blocked entirely after $50. Total damage: about $0.80.


Trap #5: The Streaming Tax — You're Paying for Tokens You Never See

The Problem

Streaming mode (stream=True) is great for UX. Users see tokens appear in real-time. But here's what most teams don't realize: when a streaming request is interrupted mid-stream, you still pay for the entire generation.

User starts a request → GPT-4o begins streaming a 2,000-token response → user navigates away after 50 tokens → the client connection drops → but the upstream API call completes fully → you pay for all 2,000 tokens.

At scale, this is devastating. I've seen teams where 23% of their token spend was on tokens that no user ever saw because the client disconnected early.

Why It Happens

LiteLLM (and most API gateways) doesn't automatically cancel the upstream request when the client disconnects during streaming. The gateway is acting as a proxy — it's happily receiving tokens from OpenAI and trying to forward them, even though nobody's listening.

The Fix

Enable client disconnect detection and upstream cancellation:

litellm_settings:
  # Cancel upstream request when client disconnects during streaming
  stream_options:
    include_usage: true     # get token counts in the final chunk

  # Custom callback to track abandoned streams
  callbacks: stream_cost_logger

router_settings:
  # Close upstream connection when client disconnects
  streaming_client_disconnect: true   # LiteLLM 1.40+

If you're on an older version or need more control, add a custom middleware:

from litellm.proxy.custom_proxy_admin_logic import CustomProxyAdminLogic

class StreamCancellationMiddleware(CustomProxyAdminLogic):
    async def async_pre_call(self, user_api_key_dict, cache, data, call_type):
        if data.get("stream"):
            # Mark the start time
            data["metadata"] = data.get("metadata", {})
            data["metadata"]["stream_start_time"] = time.time()
        return data

    async def async_log_stream_event(self, logging_obj, response, start_time, end_time):
        # Log how many tokens were actually consumed vs delivered
        if hasattr(response, 'usage'):
            total_tokens = response.usage.get('completion_tokens', 0)
            # If stream ended early (client disconnect), log it
            if logging_obj.stream_connection_broken:
                self.metrics.abandoned_stream_tokens.inc(total_tokens)
                self.alert(
                    f"Abandoned stream: {total_tokens} tokens paid but undelivered"
                )

Also, consider setting max_tokens conservatively for streaming endpoints:

model_list:
  - model_name: gpt-4o-stream
    litellm_params:
      model: gpt-4o
      max_tokens: 1000        # cap generation length
      stream: true
      stream_options:
        include_usage: true

After implementing stream cancellation, that 23% wasted spend dropped to under 2%.


The Pattern Behind All Five Traps

Notice the theme: every one of these traps is a sensible default that becomes dangerous at scale. Retries are good — until they multiply across fallbacks. Fallbacks are good — until they funnel traffic to premium models. Caching is optional — until it's costing you 30% of your bill.

The fix is never "disable the feature." It's always "add constraints." Budgets, caps, cooldowns, TTLs. The gateway works for you, not the other way around.

If you're deploying LiteLLM or any AI API gateway, do a quick audit:

  1. What's your effective retry multiplier? (num_retries × fallback_depth)
  2. Does your fallback chain ever fall UP in price?
  3. Is caching enabled? (If you can't answer this in 5 seconds, it's probably not)
  4. Does every API key have a budget cap?
  5. What percentage of your streaming tokens are actually delivered?

If any of these questions made you nervous, you might want to check out the AI API Gateway Pitfall Map — a one-page production survival guide I put together that covers these traps (and a few more) in a format you can print and pin above your desk. It's the checklist I wish I'd had before I learned these lessons the expensive way.


Have you hit any of these traps in production? Or found others I missed? Drop a comment — I'm collecting war stories for a follow-up post.

Tags: #litellm #ai #devops #costoptimization


🎁 Free: AI API Gateway Pre-Deployment Checklist

Before you ship, run through this 43-point checklist covering auth, cost control, caching, fallbacks, security, monitoring, and production readiness. It's free — grab it here:

👉 Free Pre-Deployment Checklist (PDF)

And if you want the full pitfall map with detailed fixes for each trap above, that's here: AI API Gateway Pitfall Map ($9)