Latest post

Cutting LLM Costs Without Cutting Corners: A Practical Playbook for AI Tools

Your AI tool works. Users love it. Then the invoice arrives.

As usage grows, the same frontier model that worked well in the demo can end up handling everything — including requests that don't need frontier-level reasoning. The result is predictable: costs grow with adoption, even when much of the workload could be handled more efficiently.

The answer isn't simply to switch to the cheapest model available. It's to understand what each task actually requires — and then match the model, context, and infrastructure to that need.

Here's a four-step framework for optimizing LLM costs incrementally, so you can see what each change saves and what it costs in quality, latency, and complexity.

LLM Cost Optimization Framework: Measure, Route, Reduce, Own. Step 0 Instrumentation, Step 1 Model Routing, Step 2 Context Optimization, Step 3 Open-Source Tiers. LLM Cost Optimization Framework Measure → Route → Reduce → Own Step 0 Step 1 Step 2 Step 3 Instrumentation Model Routing Context Optimization Open-Source Tiers Measure cost, latency, quality, and task type Match task complexity to the right model tier Send less and reuse more Host high-volume tasks where TCO makes sense
Fig. 1 — The LLM cost optimization framework.

Step 0: Instrumentation

You Can't Optimize What You Can't See

Measure: Establish the baseline Every LLM call Tag by task type Trace the workflow Cost · latency · outcome Build your baseline Cost / completed task SQL generation · Q&A · summarization · knowledge building
Fig. 2 — Establish the baseline.

Before touching the architecture, instrument everything. The single most important metric isn't your monthly API bill — it's cost per completed task, broken down by task type.

An analytical agent, for example, might do SQL generation, follow-up Q&A, summarization, and knowledge building. Once you tag calls by task, a pattern almost always emerges: a small fraction of tasks (hard reasoning, multi-step debugging) consumes most of the budget, while high-volume simple tasks (formatting, rephrasing, routine queries) are often over-provisioned on a frontier model.

Implementation options: Langfuse (open-source, self-hostable — important if your data can't leave your environment) or LangSmith for tracing; LiteLLM for per-call spend tracking; OpenTelemetry for end-to-end workflow visibility.

Output: a baseline — cost/task, p50/p95 latency, and a task taxonomy sorted by complexity. This taxonomy is the blueprint for everything that follows.

Step 1: Model Routing

Route, Don't Default

Route: Match the model to the task Incoming task Assess complexity Router Eval-gated Simple → mini / nano Formatting · classification Medium → mid-tier Standard SQL · analysis Hard → frontier Reasoning · novel debugging
Fig. 3 — Match the model to the task.

The first real optimization is conceptually simple: stop sending every request to your most expensive model.

Dynamic routing sends each task to the cheapest model tier that can handle it:

  • Simple (reformatting, classification, canned follow-ups) → mini/nano-tier models
  • Medium (standard SQL generation, routine analysis) → mid-tier models
  • Hard (multi-path reasoning, novel debugging) → frontier models

Implementation options: RouteLLM trains a router on your task data to predict the right tier; LiteLLM Proxy sits in front of all providers as a single gateway — routing config, fallbacks, retries, and spend tracking in one place, no app rewrite.

The catch — and the guardrail: routing is only safe with an eval harness. Use promptfoo or Ragas to regression-test each task type against your baseline model before letting a cheaper model serve it. Published studies claim ~85% cost reduction at ~95% quality — verify those numbers on your workload, not theirs.

Step 2: Context Optimization

Send Less, Not Just Cheaper

Trim: Reduce what you send Large context Schemas · history · docs Retrieve + compress Relevant context only Model Smaller input Repeated question Check semantic cache Reusable answer Cache hit → no model call Also reuse: prompt prefixes & semantic memory
Fig. 4 — Reduce what you send.

Routing optimizes where requests go. The next lever — often the bigger one for context-heavy agents — is optimizing what you send.

  • Prompt caching: Stable context (system prompts, database schemas, knowledge bases) gets cached. Anthropic offers ~90% off cached input tokens. Lowest effort, immediate win.
  • RAG done right: Replace "stuff everything into context" with retrieving only what's relevant — past analyses, table docs, few-shot examples. pgvector works if you're already on Postgres; Qdrant or Weaviate for dedicated stores.
  • Semantic memory: Tools like Mem0 or LangMem let agents build and reuse knowledge as retrievable memories instead of re-deriving it every session.
  • Prompt compression: LLMLingua shrinks retrieved context and conversation history before it hits the model.
  • Semantic response caching: GPTCache answers repeat or near-identical questions without a model call — particularly valuable for scheduled workloads that ask the same questions daily.

Each of these reduces token volume. Measured together, it's not unusual to see 50–80% of input tokens eliminated — savings that multiply the routing gains from Step 1.

Step 3: Open-Source Tiers

Own the Simple Stuff Where It Fits

Own: Host only where it wins LiteLLM gateway Route by task tier YOUR VPC · eligible high-volume tasks Open model + vLLM / SGLang Infrastructure + operations cost Provider API / serverless Use where quality and total cost fit
Fig. 5 — Host only where it wins.

Once you've routed and trimmed, the remaining question: should any of those cheap tiers be yours?

Self-hosted open models (Llama, Qwen, DeepSeek, Mistral) make sense for high-volume, simple tasks — especially when data privacy is part of the equation. Tasks served inside your VPC never leave your environment, which can matter more than the dollar savings for enterprise clients.

Implementation options: vLLM or SGLang serve open models behind an OpenAI-compatible API — they plug straight into the LiteLLM gateway from Step 1. Ollama is perfect for quick local benchmarking. For fine-tuning (only where a specialized task justifies it), Axolotl or Unsloth with LoRA — trained on the successful generations you logged in Step 0. If you don't want GPU ops, serverless providers like Together, Fireworks, or Modal are a middle path.

The trade-off: open source trades API bills for infrastructure bills and operational burden — model management, upgrades, GPU capacity, on-call. That's why this is Step 3, not Step 1. Run the TCO math per task tier, and only migrate where it wins.

The Framework in One Picture

Step Optimization What you save What it costs
0 Instrumentation Nothing yet — but enables everything ~2-3 weeks of engineering
1 Model routing 50–85% on routed task types Eval harness + routing layer
2 Context optimization 50–80% of token volume RAG infra, cache management
3 Open-source tiers API spend → predictable infra spend GPU ops, model maintenance

Ranges and timing above are estimates from the original article, not guaranteed results. Validate savings, pricing, quality, and implementation effort on your workload.

Each step builds on the last and stands on its own — you can stop after routing and keep the savings.

The Real Insight

Cost optimization isn't a model-shopping exercise. It's a measurement discipline: know what each task costs, match each task to the cheapest resource that preserves quality, and gate every change behind evals.

Do that, and "reduce the LLM bill" stops being a scary refactor and becomes a series of small, reversible, measurable decisions — which is exactly how production systems should evolve.

The value of an AI tool isn't the model behind it — it's the quality and reliability of the outcome. Protect that, then optimize everything around it.