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

推荐订阅源

WordPress大学
WordPress大学
L
LangChain Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
罗磊的独立博客
J
Java Code Geeks
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
博客园 - 叶小钗
小众软件
小众软件
博客园 - Franky
D
Docker
Google DeepMind News
Google DeepMind News
Microsoft Azure Blog
Microsoft Azure Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
U
Unit 42
宝玉的分享
宝玉的分享
C
Check Point Blog
B
Blog
V
V2EX
博客园 - 三生石上(FineUI控件)
MyScale Blog
MyScale Blog
The Cloudflare Blog
博客园 - 聂微东
博客园_首页
Engineering at Meta
Engineering at Meta

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
How I Cut My AI API Bill by 90% With a Multi-Model Routin...
Sam Chen · 2026-05-11 · via DEV Community

Sam Chen

Last month my Claude API bill was $847. This month it's $73. Same output quality. Here's the system I built.

The Problem

I run multiple AI-powered services — content generation, email classification, SEO optimization, data extraction. Every call was going to Claude Sonnet because "it works." But most of those calls didn't need Sonnet-level intelligence.

Classifying an email as spam? That's a Haiku job. Generating embeddings? Ollama handles that for free. Writing a full article? OK, that's Sonnet. But only 15% of my calls actually needed the expensive model.

The Architecture: Empire Router

I built a routing layer that sits between my application code and the LLM providers. Every request gets classified by complexity, then routed to the cheapest model that can handle it.

from empire_router import router

# Auto-routed based on task complexity
response = router.complete(
    prompt="Classify this email: ...",
    task="classify"  # Routes to Haiku ($0.80/M tokens)
)

response = router.complete(
    prompt="Write a 1500-word article about...",
    task="generate"  # Routes to Sonnet ($3/M tokens)
)

embedding = router.embed("text to embed")  # Routes to Ollama (FREE)

Enter fullscreen mode Exit fullscreen mode

The Routing Decision Tree

Task classification:
├── Binary/classification → Haiku ($0.80/$4 per M tokens)
├── Embeddings → Ollama on VPS (FREE)
├── Simple extraction → DeepSeek ($0.27/M) or Groq (FREE)
├── Content generation → Sonnet ($3/$15 per M tokens)
└── Complex reasoning → Opus ($15/$75) — used <2% of calls

Enter fullscreen mode Exit fullscreen mode

Key Design Decisions

1. Task-type routing, not content-length routing

My first attempt routed by prompt length. Terrible idea. A short prompt like "Is this email spam?" needs a cheap model regardless of length. A short prompt like "Design the architecture for a distributed cache" needs an expensive one.

The task type is what determines model selection, not the token count.

2. Fallback chains, not single-model assignments

ROUTING_CHAINS = {
    "classify": ["ollama/llama3.1:8b", "groq/llama3", "haiku", "sonnet"],
    "generate": ["sonnet", "opus"],
    "embed": ["ollama/nomic-embed-text", "voyage-3"],
}

Enter fullscreen mode Exit fullscreen mode

If the primary model is down or rate-limited, it cascades to the next option. No failed requests, just slightly higher cost on fallback.

3. Quality gates on cheap models

The router doesn't blindly trust cheap model output. For tasks where accuracy matters, it runs a quality check:

  • Send to cheap model first
  • Score the response (confidence, format validity, coherence)
  • If score < threshold → retry on next model in chain
  • Log the escalation for future routing optimization

In practice, Haiku handles 94% of classification tasks without escalation.

4. Prompt caching for repeated patterns

System prompts that exceed 500 characters get cached. For classification tasks that run the same system prompt thousands of times, this cuts input costs by 90% after the first call.

Results After 30 Days

Metric Before After Change
Monthly cost $847 $73 -91%
Avg latency 2.1s 0.8s -62%
Failed requests 12/day 0.3/day -97%
Quality (human eval) 4.2/5 4.1/5 -2%

The quality dip is within noise. The latency improvement comes from Haiku being faster than Sonnet, plus Ollama embeddings having no network round-trip.

Self-Hosted vs. API: Where the Line Is

I run Ollama on a Contabo VPS (CPU-only, $15/mo). It handles:

  • All embeddings (nomic-embed-text)
  • Simple classification fallback (llama3.1:8b)
  • Data extraction on non-sensitive content

Everything that needs quality or handles sensitive data goes to API providers. The VPS pays for itself in 2 days of avoided API calls.

Try It

The routing pattern works with any LLM provider combination. The key insight: treat model selection as a runtime decision, not a deployment decision.

I write about practical AI cost optimization and infrastructure at wealthfromai.com. The full router is open for anyone building similar multi-model systems — DM me if you want the architecture details.