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

推荐订阅源

OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
L
LINUX DO - 热门话题
Blog — PlanetScale
Blog — PlanetScale
博客园 - Franky
J
Java Code Geeks
腾讯CDC
博客园 - 聂微东
The Cloudflare Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
博客园 - 司徒正美
Last Week in AI
Last Week in AI
量子位
Stack Overflow Blog
Stack Overflow Blog
Microsoft Security Blog
Microsoft Security Blog
Google DeepMind News
Google DeepMind News
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
S
Schneier on Security
C
CERT Recently Published Vulnerability Notes
Latest news
Latest news
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
有赞技术团队
有赞技术团队
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
S
Securelist
AWS News Blog
AWS News Blog
GbyAI
GbyAI
L
LINUX DO - 最新话题
大猫的无限游戏
大猫的无限游戏
Forbes - Security
Forbes - Security
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Attack and Defense Labs
Attack and Defense Labs
C
CXSECURITY Database RSS Feed - CXSecurity.com
Y
Y Combinator Blog
W
WeLiveSecurity
T
Threatpost
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
P
Proofpoint News Feed
D
DataBreaches.Net
博客园 - 三生石上(FineUI控件)
V
V2EX
N
News and Events Feed by Topic
Google DeepMind News
Google DeepMind News
D
Docker
The Hacker News
The Hacker News
A
About on SuperTechFans
Security Latest
Security Latest
NISL@THU
NISL@THU
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Cisco Talos Blog
Cisco Talos Blog
博客园_首页
H
Hacker News: Front Page

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 Developer's Guide to Trimming AI API Costs Without Crying
swift · 2026-06-27 · via DEV Community

The Developer's Guide to Trimming AI API Costs Without Crying

Last March, I opened our team's LLM billing dashboard on a Monday morning and nearly choked on my coffee. We'd spent $11,400 in a single month. Three times what we'd projected at the start of the quarter. I'm a backend engineer, not a finance person, but even I knew that line on the graph wasn't heading anywhere good.

What followed was three weeks of obsessive cost-cutting, a handful of internal RFCs (RFC 9457 made a cameo, as it always does), and the realization that we'd been doing AI integration the expensive way for almost a year. fwiw, the culprit was the usual: we'd defaulted to GPT-4o for everything because it was the path of least resistance, and nobody had bothered to measure whether the cheaper models would do the job just as well.

That $11,400 bill dropped to $1,830 by the end of the next month. Here's exactly how.

The Lie You've Been Telling Yourself About Model Quality

Before I get into the tactics, I need to address something. If you've been shipping AI features in production for the last year or two, you've probably developed an intuition that "the big model is always better." That's a useful heuristic when you're prototyping and shipping MVPs. It's an expensive lie at scale.

I tested this empirically. I ran 2,000 representative prompts from our production traffic through both GPT-4o and a stack of cheaper alternatives. For the vast majority of tasks — classification, summarization, simple chat, FAQ responses, even most code generation — the quality difference was within the noise floor of human preference. We're talking 85-95% of requests where nobody could tell the difference in a blind test.

The 5-15% where the bigger model does matter? That's where tiered routing comes in. But first, let's talk about the single biggest win.

Strategy 1: Stop Using a $10/M Model to Say "Hello"

The lowest-hanging fruit, and the one that saved us the most money, was just picking the right model for the task. Sounds obvious. Almost embarrassingly obvious once you stare at the numbers.

Here's what we shipped, which lines up roughly with what I see across the industry:

Use Case What We Used What We Use Now Savings
Simple chat GPT-4o ($10/M out) DeepSeek V4 Flash ($0.25/M out) 97.5%
Classification GPT-4o-mini ($0.60/M out) Qwen3-8B ($0.01/M out) 98.3%
Code generation GPT-4o ($10/M out) DeepSeek Coder ($0.25/M out) 97.5%
Summarization GPT-4o ($10/M out) Qwen3-32B ($0.28/M out) 97.2%
Translation GPT-4o ($10/M out) Qwen-MT-Turbo ($0.30/M out) 97%

Just doing this across the board — no clever routing, no caching, nothing fancy — took our spend from $11,400 to about $2,900. That alone was a 75% reduction. imo, if you do nothing else from this article, do this.

Here's the routing logic that lives in our service now:

import httpx
import os

BASE_URL = "https://global-apis.com/v1"
API_KEY = os.environ["GLOBAL_API_KEY"]

MODEL_MAP = {
    "chat": "deepseek-v4-flash",        # $0.25/M
    "code": "deepseek-coder",           # $0.25/M
    "classify": "Qwen/Qwen3-8B",        # $0.01/M
    "summarize": "Qwen/Qwen3-32B",      # $0.28/M
    "translate": "Qwen-MT-Turbo",       # $0.30/M
    "reason": "deepseek-reasoner",      # $2.50/M
}

def route_request(task: str, user_input: str) -> str:
    model = MODEL_MAP[task]

    with httpx.Client(base_url=BASE_URL) as client:
        resp = client.post(
            "/chat/completions",
            headers={"Authorization": f"Bearer {API_KEY}"},
            json={
                "model": model,
                "messages": [{"role": "user", "content": user_input}],
            },
            timeout=30.0,
        )
        resp.raise_for_status()
        return resp.json()["choices"][0]["message"]["content"]

Note the global-apis.com/v1 base URL — we route everything through one provider so we can swap models without renegotiating five different vendor contracts. Under the hood, this is just an OpenAI-compatible endpoint, so the code looks identical to what you'd write against OpenAI directly.

Strategy 2: Tiered Routing — The "Try Cheap, Escalate on Failure" Pattern

After model selection got us most of the way, the next biggest win was a tiered router. The idea is straightforward: send every request to the cheapest model that might work, run a cheap quality check, and escalate to a more expensive model only if the cheap one flunked.

Here's the pattern I ended up with after a few iterations:

def smart_generate(prompt: str, budget_tier: str = "auto") -> str:
    """
    Tier 1: Qwen3-8B at $0.01/M handles ~80% of traffic
    Tier 2: DeepSeek V4 Flash at $0.25/M handles ~15%
    Tier 3: DeepSeek Reasoner at $2.50/M handles ~5%
    """

    # Tier 1 — try ultra-budget first
    tier1 = call_model("Qwen/Qwen3-8B", prompt)
    if quality_check(tier1, prompt) >= 0.8:
        return tier1

    # Tier 2 — standard tier
    tier2 = call_model("deepseek-v4-flash", prompt)
    if quality_check(tier2, prompt) >= 0.9:
        return tier2

    # Tier 3 — premium reasoning
    return call_model("deepseek-reasoner", prompt)

def quality_check(response: str, original_prompt: str) -> float:
    """
    Cheap heuristic: ask a tiny model to rate the response 0-1.
    Yes, this adds latency. It's still cheaper than going straight to Tier 3.
    """
    rating_prompt = (
        f"Rate this answer's quality from 0 to 1:\n\n"
        f"Question: {original_prompt}\n\nAnswer: {response}\n\n"
        f"Reply with only a number."
    )
    score = call_model("Qwen/Qwen3-8B", rating_prompt).strip()
    try:
        return float(score)
    except ValueError:
        return 0.5  # bail-out default

Yes, the quality check itself costs money. Yes, it adds latency. But a 0.01/M call to grade a response is still trivially cheap compared to burning a $2.50/M reasoning call on a "what time does the store open" question.

We deployed this on a customer support chatbot and watched the bill drop from $420/month to $28/month. That's not a typo. The trick was that 85% of incoming queries were straightforward FAQ-style stuff that Qwen3-8B handles flawlessly for roughly the cost of dirt.

Strategy 3: Cache Aggressively, Invalidate Carefully

Caching is the third leg of the stool, and it's the one most teams under-invest in. imo, the reason is that "response caching for LLMs" sounds intellectually suspect — surely every request is unique, surely you can't cache creative output, surely this won't help much.

Then you actually look at your traffic patterns and discover that 30-50% of your requests are near-duplicates of ones you've already handled. FAQ queries. Documentation lookups. Standard boilerplate. "Reset my password." "What are your hours." Things that have one canonical answer.

Here's the cache layer I wrote — straightforward, no Redis, just an in-memory dict to start:

import hashlib
import json
import time
from typing import Any

_cache: dict[str, dict[str, Any]] = {}

def cached_chat(
    model: str,
    messages: list[dict],
    ttl: int = 3600,
) -> dict:
    """Cache identical requests for `ttl` seconds. Free responses."""

    key = hashlib.md5(
        json.dumps({"model": model, "messages": messages}, sort_keys=True).encode()
    ).hexdigest()

    now = time.time()

    if key in _cache:
        entry = _cache[key]
        if now - entry["time"] < ttl:
            entry["hits"] += 1
            return entry["response"]  # $0 cost

    response = call_model_raw(model, messages)
    _cache[key] = {
        "response": response,
        "time": now,
        "hits": 1,
    }
    return response

For us, this hit 50-80% on the FAQ-heavy endpoints. The 1-hour TTL was a starting point — for documentation lookups we bumped it to 24 hours; for chat conversations, 5 minutes. Tune it to your actual staleness tolerance.

If you're running multi-instance, swap the dict for Redis. The pattern doesn't change.

Strategy 4: Compress Your Prompts Before You Send Them

This is one nobody talks about, and it's where I found the weirdest savings. Most of us don't think about prompt length as a cost variable — we think of it as a quality variable. But every input token is billed. A 2,000-token system prompt you're shipping 10,000 times a day is real money.

The technique: run your long context through a cheap model first, get a compressed version, ship the compressed version. You lose some nuance, but for the kinds of tasks where you're feeding context into a stronger model to extract something specific, you usually don't need every word.

def compress_prompt(text: str, target_ratio: float = 0.5) -> str:
    """Shrink a long prompt before sending it to a stronger model."""

    if len(text) < 500:
        return text  # already short enough

    target_chars = int(len(text) * target_ratio)

    summary = call_model(
        "Qwen/Qwen3-8B",
        f"Summarize the following in {target_chars} characters or less, "
        f"preserving all key facts and entities:\n\n{text}",
    )
    return summary

Let me put concrete numbers on this. A 2,000-token prompt compressed to 400 tokens saves $0.024 per request on DeepSeek V4 Flash. Sounds trivial. Multiply by 10,000 requests per day. That's $240/day, or $87,600/year, from one prompt template. Once I ran the audit, we had three or four prompt templates that were similarly bloated. The savings compounded.

The 15-30% per-request reduction is real, and it stacks on top of everything else.

Strategy 5: Batch Aggressively

The last technique I want to cover is batching — combining multiple requests into one API call when the tasks are independent. This works especially well for offline/async workloads: nightly report generation, bulk classification, log analysis, that kind of thing.

The win comes from the fact that the system prompt and any shared context are billed once instead of N times. If you have a 500-token system prompt and you batch 50 questions into one call, you've just divided that overhead by 50.

def batch_classify(questions: list[str], categories: list[str]) -> list[str]:
    """Classify many items in one API call instead of N calls."""

    numbered = "\n".join(f"{i}. {q}" for i, q in enumerate(questions))
    prompt = (
        f"Classify each numbered question into one of these categories: "
        f"{', '.join(categories)}.\n\n"
        f"Reply with one category per line, in the same order.\n\n"
        f"{numbered}"
    )

    result = call_model("Qwen/Qwen3-8B", prompt)
    return [line.strip() for line in result.splitlines() if line.strip()]

def classify_individually(questions: list[str]) -> list[str]:
    return [
        call_model("deepseek-v4-flash", f"Classify: {q}")
        for q in questions
    ]

We picked up a 10-20% reduction on our batch ETL pipelines. Nothing dramatic, but free money is free money.

The Combined Effect

Here's what the monthly bill looked like before and after all five strategies, for the same workload:

Metric Before After
Monthly spend $11,400 $1,830
Avg cost per request $0.038 $0.006
Avg latency (p95) 1.8s 1.4s
Quality regression <2% on blind eval

The latency improvement was a nice surprise — when you stop sending everything to the slow