AI Cost Optimization: How to Cut Your LLM Bill 40-70% Without Losing Quality
AI bills compound fast in production. A team that started with $300/month in API spend can find itself at $30K/month nine months later, without anyone explicitly authorizing the growth. The seven optimization patterns in this guide consistently cut LLM cost 40-70% in production without measurable quality loss. We use them on every Alher Tech engagement.
The Seven Patterns
- 1. Prompt caching: Cache the static parts of your system prompt. Subsequent calls hit cache at 10-25x discount on input tokens. Available on Anthropic, OpenAI, Gemini in 2026. Single biggest cost lever.
- 2. Model routing: Cheap model (Haiku, GPT-5 Mini, Gemini Flash) for simple tasks. Expensive model (Opus, GPT-5, Gemini Pro) for complex reasoning. Routing logic in code, not in user-facing complexity.
- 3. Response caching: If the same prompt + same context produces the same answer, cache the answer. Especially for high-volume agents answering recurring questions.
- 4. Structured outputs: JSON schemas, function calling, regex constraints. Eliminate retries from malformed output. Reduce token waste on verbose explanations.
- 5. Prompt compression: Summarize long contexts before sending. RAG retrieves 50 chunks but model sees only the top 5 most relevant. Use small model to compress before large model reasons.
- 6. Batching: Anthropic and OpenAI offer batch APIs at 50% discount with 24h SLA. For non-real-time workflows (analytics, daily reports, classification jobs), free 50% off.
- 7. Self-hosting threshold: Past $30-50K/month in API spend, self-hosted Llama 4 or Qwen 3 starts winning on cost. Below that, the ops burden isn't worth it.
Cost Levers That Don't Work
- Switching to a cheaper provider just for the sake of it. The migration cost often exceeds the savings.
- Aggressively reducing max_tokens. Cuts cost but causes truncated responses that frustrate users.
- Removing context. Worse answers, more user retries, net cost goes up.
- Asking the model to 'be brief'. Saves a small fraction of output tokens; loses meaningful info.
- Switching from streaming to non-streaming. Doesn't change cost, just UX.
Prompt Caching Deep Dive
Prompt caching is the single highest-ROI optimization in 2026. The pattern that wins:
- Identify your static prefix: system prompt, few-shot examples, RAG retrieval context that doesn't change per request
- Mark it as cacheable in the API call (cache_control: ephemeral on Anthropic, prompt caching on OpenAI)
- First call costs full price + small caching surcharge; subsequent calls within TTL pay 10-25% of normal input price
- TTL is typically 5 minutes (Anthropic ephemeral) or 1 hour (Anthropic 1h cache); match your traffic pattern
- Order matters: put truly static content first, dynamic content last
- Real savings: support agents we've shipped see 40-65% input cost reduction with caching alone
If you're not using prompt caching in 2026 and your system prompt is over 500 tokens, you're overpaying. Period.
Model Routing Strategy
- Tier 1: Cheap (Haiku, GPT-5 Mini, Gemini Flash): Classification, intent detection, simple Q&A from RAG, structured extraction. ~5% of frontier model cost. Use as the default; escalate when necessary.
- Tier 2: Mid (Sonnet 4.6, GPT-5, Gemini Pro): Production agent reasoning, multi-step planning, tool use. The workhorse tier. ~30% of frontier cost.
- Tier 3: Frontier (Opus 4.7, GPT-5.5): Hardest reasoning, code generation, multi-document synthesis. Use selectively. Most production agents only need this for 5-15% of requests.
- Routing rules: Token count, complexity score from a small classifier model, retry-on-low-confidence. Match the model to the request, not the request to the model.
Real Savings Examples
| Workload | Before | After optimization | Savings |
|---|
| Support agent (50K tickets/mo) | $22K/mo | $8K/mo | 63% |
| RAG-powered search (1M queries/mo) | $45K/mo | $18K/mo | 60% |
| Document extraction pipeline | $15K/mo | $4K/mo | 73% |
| Sales qualification agent | $8K/mo | $3K/mo | 62% |
| Code review agent (internal) | $12K/mo | $5K/mo | 58% |
Real numbers from Alher Tech engagements 2024-2026. Each used a combination of patterns; no single optimization carries the day alone.
When to Self-Host
- API spend over $30-50K/month, where the GPU bill starts being competitive
- Predictable, high-volume workloads (steady traffic suits dedicated GPUs)
- Compliance or data residency requirements that rule out vendor APIs
- Open-source model performance is acceptable for your accuracy bar (Llama 4, Qwen 3 are competitive in many tasks in 2026)
- You have or can hire MLOps capacity (vLLM, SGLang and TGI all need someone watching them)
- You can amortize the GPU spend across multiple workloads (don't dedicate 4 H100s to one tiny app)
Below $30K/month, self-hosting saves you nothing once you account for ops time, GPU idle cost and the engineering complexity. Stay on managed APIs.
Optimize Like It's Production Engineering
AI cost optimization is engineering, not procurement. Caching, routing, batching, schemas: these are the same patterns that work for any high-volume API. Treat your AI bill like a database bill: instrument it, optimize it, monitor it.
The teams that don't end up paying 3-5x what they should be paying. The teams that do free up budget to ship more AI features.
Frequently asked questions
How much can I realistically save?
30-50% with prompt caching alone. 40-70% with the full pattern stack. Beyond that, self-hosting becomes the next lever, but only at scale.
Will optimizations hurt quality?
Not if done right. Caching, batching and structured outputs don't change quality. Model routing maintains quality if the routing classifier is accurate. The patterns we use are quality-neutral or quality-positive.
What about Claude Code or coding agents?
Same patterns apply. Code-related workloads especially benefit from prompt caching (the system prompt is huge) and model routing (Haiku for simple edits, Sonnet/Opus for design work).
Should I switch from OpenAI to Anthropic for cost?
Don't switch for cost alone; they're roughly comparable in 2026. Switch for quality fit, ecosystem alignment or specific features. Cost optimization within your chosen provider yields more savings than provider switching.
Is prompt caching worth it for low-volume apps?
Below 1,000 calls/day, the engineering effort isn't worth it. Above 10K calls/day, it pays back in days. In between, depends on average prompt length and TTL hit rate.
Related guides