Learn AI · Foundations
LLM Cost Optimization
The operational playbook for cutting LLM spend without cutting quality. Model routing, caching, batching, and the metrics that actually matter.
Key takeaways
- 1Model routing is the highest-leverage move: cheap model for easy tasks, frontier only when needed.
- 2Prompt caching cuts input cost 75–90% on repeated system prompts — enable it before anything else.
- 3Shorter prompts beat cheaper models. Trim context, summarize history, drop redundant examples.
- 4Batch APIs (OpenAI, Anthropic) give ~50% off for offline work — use them for backfills and evals.
- 5Track cost-per-successful-task, not raw token spend. A $0.02 call that fails three times costs more than a $0.05 call that works once.
The real problem
Your LLM bill isn't high because models are expensive. It's high because every request hits the most expensive model with the longest possible context. That's a design choice, not a law of physics.
Most production workloads are a mix: 70% classification, extraction, and formatting; 20% moderate reasoning; 10% genuinely hard. If you're sending all of it to Claude Opus or GPT-5 at full context, you're lighting money on fire.
The optimization stack (in order of leverage)
1. Model routing — the 10x move
Route by task difficulty, not by user tier.
Incoming request
→ cheap classifier (Gemini Flash, Haiku, GPT-5 nano)
→ easy? → cheap model handles it
→ hard? → escalate to frontier
→ ambiguous? → cheap model tries first, escalate on low confidence
What counts as "easy":
- Classification and routing
- Structured extraction (name, date, category)
- Summarization under 2K output tokens
- Format conversion (JSON → markdown, etc.)
What needs frontier:
- Multi-step reasoning with tool use
- Code generation with test feedback loops
- Nuanced judgment calls (legal, medical, financial)
A well-tuned router cuts frontier-model calls by 60–80%. That's not a marginal saving — it's a different cost structure.
2. Prompt caching — free money you're leaving on the table
If your system prompt, tool definitions, or RAG context prefix is identical across calls, prompt caching stores it server-side after the first request. Subsequent calls pay 75–90% less on that prefix.
This matters most when:
- Your system prompt is 2K+ tokens (common in agent setups)
- You run RAG with a fixed retrieval template
- You make thousands of similar calls per day
Enable it. Measure the hit rate. If it's below 50%, your prompts aren't structured for cache reuse — fix that before switching models.
3. Context compression — shorter is cheaper
Every token in the context window costs money. Most agent loops accumulate garbage:
- Full chat history when only the last 3 turns matter
- Tool outputs from 5 steps ago that are already reflected in the answer
- Retrieved chunks that weren't used in the previous response
Practical fixes:
- Summarize conversation history every N turns (keep a rolling summary + recent messages)
- Cap RAG retrieval at 5–8 chunks, not 20
- Strip tool outputs from context once their result is incorporated
- Use a smaller model to compress before sending to the frontier model
Context engineering is cost engineering. See our Context Engineering Explained guide for the full playbook.
4. Batch APIs — half price for offline work
OpenAI and Anthropic both offer batch endpoints at ~50% discount. The tradeoff: results return within 24 hours, not seconds.
Perfect for:
- Eval suite runs
- Nightly document processing
- Bulk summarization and tagging
- Embedding generation for index updates
If you're running these synchronously today, you're paying double for no reason.
5. Output control — stop the runaway generation
Unbounded max_tokens is a budget leak. Set explicit limits:
- Classification: 50–100 tokens
- Extraction: 200–500 tokens
- Summarization: 500–1500 tokens
- Only open-ended generation gets 4K+
Combine with structured outputs (JSON mode, tool schemas) to prevent the model from rambling. A 200-token structured response beats a 2K-token prose answer every time — for cost and reliability.
6. Fine-tune small models for narrow tasks
If you're paying a frontier model to do the same narrow task 10,000 times/day (support ticket classification, intent routing, format conversion), fine-tune a small model and cut per-call cost by 90%+.
Fine-tuning shifts behavior, not knowledge — so pair it with RAG for facts. But for stable, repetitive tasks, it's the cheapest long-term play. See Fine-Tuning Explained for when it actually makes sense.
Metrics that matter
Stop tracking raw token spend. Track these instead:
| Metric | Why |
|---|---|
| Cost per successful task | A failed $0.02 call that retries 3x costs $0.06. A $0.05 call that works once costs less. |
| Frontier model % of traffic | Should be under 20% for most apps. Above 30% means your router is too conservative. |
| Cache hit rate | Below 50%? Your prompt structure needs work. |
| P95 context length | If P95 is 50K tokens, you have a context bloat problem, not a model problem. |
| Cost per user per month | The number your CFO cares about. Everything else is engineering detail. |
The anti-patterns (what wastes money)
- One model for everything — frontier on every call because "quality matters." It doesn't for 70% of your traffic.
- Stuffing full docs into context — use RAG retrieval, not paste-the-whole-PDF. See RAG Explained.
- Ignoring caching — paying full input price on identical prefixes thousands of times daily.
- No evals before optimizing — you cut cost and quality together, then can't tell which broke.
- Self-hosting prematurely — GPU ops, cold starts, and model updates cost more than API routing until you're at serious scale.
A realistic savings timeline
| Week | Action | Expected savings |
|---|---|---|
| 1 | Enable prompt caching + set max_tokens | 15–25% |
| 2 | Add cheap classifier router | 30–50% cumulative |
| 3 | Compress context (history summarization, RAG cap) | 40–60% cumulative |
| 4 | Move offline work to batch APIs | 45–70% cumulative |
| Month 2+ | Fine-tune small models for top 3 repetitive tasks | 60–80% cumulative |
These aren't theoretical. They're what teams actually see when they stop treating every LLM call like a research project.
The honest take
Cost optimization isn't about using worse models. It's about using the right model for the right task, with the right amount of context, cached where possible. The teams that do this well spend 1/5th what everyone else spends — and their apps are faster because shorter contexts mean lower latency too.
Start with routing and caching. Measure with evals. Optimize the boring 70% of your traffic first. The hard 10% can keep the frontier model.
Common misconceptions
The wrong-but-common takes worth correcting.
Myth
Switching to a cheaper model is the main cost lever.
Reality
Routing, caching, and prompt compression usually save more than a model downgrade — and without quality loss on hard tasks.
Myth
You need to self-host to get real savings.
Reality
Self-hosting only wins at very high volume with stable workloads. For most teams, API routing + caching beats GPU ops overhead.
Myth
Longer context windows are free because providers advertise big limits.
Reality
You pay per token. Stuffing 200K tokens into every call is the fastest way to 5x your bill.
Real-world use cases
Multi-tier model routing
Classify incoming requests with a cheap model, escalate only ambiguous or high-stakes queries to a frontier model.
Cached system prompts
Long RAG system prompts, tool schemas, and few-shot examples cached across thousands of identical-prefix calls.
Offline batch processing
Nightly summarization, eval runs, and document indexing via batch APIs at half price.
Context compression
Summarize chat history, retrieve only top-k chunks, and drop stale tool outputs before the next turn.
Open
Frequently asked questions
Watch
Hand-picked videos from official + trusted channels. Opens in a new tab.
- OpenAIOfficial
OpenAI prompt caching documentation
Official guide on how caching works and when discounts apply.
- AnthropicOfficial
Anthropic prompt caching
Anthropic's caching model — critical for Claude-heavy stacks.
- Helicone
Helicone cost observability
Practical walkthroughs on tracking and optimizing LLM spend in production.
Related on AIKnowHub
Comparison
Cheapest AI APIs
Per-million-token cost comparison across providers, with notes on where the cheap models are actually good enough.
Concept
Context Engineering Explained
Prompt engineering is one input. Context engineering is everything around it — what you fetch, what you trim, what you remember, what you let the model see.
Concept
Fine-Tuning Explained
When to fine-tune, when not to, and how to do it without burning weekend cycles. The honest version.
Tool Guide
LiteLLM Guide
The definitive guide to LiteLLM for multi-model routing — OpenAI-compatible proxy, fallbacks, cost tracking, and self-host setup for production AI apps.
Comparison
Local Models vs API Models
Should you run LLMs on your own hardware or pay OpenAI and Anthropic? An honest comparison on privacy, cost, latency, quality, and the ops burden nobody talks about upfront.
Interview
An LLM hallucinated a fake API in production code. How do you prevent this systematically?
Tests whether you treat LLM reliability as an engineering problem, not a model problem.