LLM Cost Optimization

Reduce LLM costs without breaking quality

Optimization is not about squeezing tokens — it is about using the cheapest model that still passes the task. Here are the techniques that actually move the number.

Answer

LLM cost optimization means tuning model choice, caching, batching, and retry behavior so you pay the least for a result that still meets your quality bar. The biggest lever is replacing an expensive model with the cheapest one that passes the task — not chasing the lowest token price.

Evidence
  • Model price varies widely by capability tier — compare on LLM Pricing.
  • Candidate models must be measured on your tasks, not generic scores — see Benchmarks.
  • Cheapest tokens can be most expensive per success — track cost per successful task.

Token cost vs outcome cost

Token cost is the unit providers bill on, but it is not the unit you should optimize. The outcome you care about — a correct answer, a completed task, a usable draft — has its own price once you include failures and retries.

Token cost

Price of input and output tokens on a single call. Easy to read, easy to compare, but blind to quality.

Outcome cost

Total spend divided by tasks that succeeded, including failed runs and retries. This is the number that belongs in a budget.

When you optimize token cost alone, you can pick a model that fails often and quietly raises outcome cost. Cost per successful task keeps the target honest.

Model replacement (the biggest lever)

Most production traffic was originally built against a flagship model and never revisited. High-volume, well-scoped calls — classification, extraction, summarization, routing — rarely need the most capable model available.

  • Audit by task, not by app. One application usually contains several task types with very different difficulty.
  • Benchmark candidates on your data. Use Model Compare to score cheaper models against your prompts and a held-out set.
  • Swap where quality holds. Replace the model only where the cheaper candidate meets your acceptance bar, and keep the premium model for the hard tail.

Harpd ModelSwitch runs this search for you: it tests cheaper models on your real tasks and reports which ones pass, so the downgrade is backed by evidence.

Prompt caching

If your prompt contains a stable prefix — a system prompt, a long set of instructions, or retrieved context that does not change per request — cache it. The first request pays full price to write the cache; subsequent requests read from it at a much lower rate.

  • Cache the static parts. Put instructions and repeated context first so they form a cacheable prefix.
  • Mind the TTL. Caches expire; design so the hot prefix is reused within the window.
  • Best for RAG and agents. Retrieved documents and tool schemas are ideal cacheable prefixes.

Batch processing

When a task is not user-facing and can tolerate delay, batching often lowers effective cost. Instead of sending requests one at a time with synchronous latency expectations, submit them as a batch that the provider processes offline.

Use batch for backfill, bulk summarization, dataset labeling, and nightly enrichment. Donot use it for interactive chat or anything in a live request path.

Prompt & output reduction

Smaller inputs and outputs cost less, but the goal is to remove waste, not to starve the model of what it needs.

  • Trim the system prompt. Delete instructions that are never exercised by the task.
  • Limit retrieved context. Retrieve only what the call needs; irrelevant chunks inflate input tokens on every request.
  • Stop asking for discarded output. If you parse out only the JSON, do not also ask for prose explanations.
  • Set a sane max_tokens. A reasonable ceiling prevents a single runaway generation from dominating cost.

Measure success rate before and after any reduction — if quality drops, you removed too much.

Retries and fallbacks

Retries are necessary but expensive. An unbounded retry loop multiplies cost on the exact inputs that already failed once.

  • Cap retries. A small fixed limit (often one or two) is usually enough.
  • Fail over, do not repeat. On repeated failure, switch to a different model or a safe fallback rather than re-calling the same one.
  • Count retry spend. Retries are part of cost per successful task — they should show up in your numbers, not hide in a log.

Cost per successful task

Tie it all together: the metric that matters is total spend divided by tasks that succeeded. It automatically accounts for model price, cache hits, batch discounts, and the cost of failures and retries. A change that lowers token price but raises failure rate will show up as worse here — which is exactly the signal you want.

Measure it with Cost per Successful Task and validate model swaps against it rather than against token price alone.

Harpd's point of view

Don't optimize for cheapest tokens. Optimize for the cheapest model that still passes the task.

Frequently asked questions

What is the cheapest way to reduce LLM cost?
The single biggest lever is model replacement: route each workload to the cheapest model that still passes it. After that, prompt caching (for stable prefixes) and reducing unnecessary retries usually return more than micro-optimizing token counts. Harpd measures candidate models on your own tasks so the swap is evidence-based, not a guess.
Is cheaper per token always cheaper overall?
No. A model with low token price can be more expensive once you include failures and retries, because you pay for the work that did not succeed. Optimize for cost per successful task, which divides total spend by tasks that actually passed.
How much can prompt caching save?
It depends on how much of your prompt is a stable prefix. Cached input tokens typically cost a fraction of the full input price (often around a tenth on many providers), so workloads with large, repeated system prompts or retrieved context can see meaningful reductions. Treat cached reads as "pay once, read many."
Should I batch my LLM requests?
If latency is not critical, batching can lower effective cost because some providers price async/batch traffic lower than synchronous requests. Batch is best for offline jobs (summarization, classification, enrichment) where a delay of minutes or hours is acceptable.
Does reducing output tokens hurt quality?
It can, if you force brevity on tasks that need reasoning or detail. The safe approach is to tighten instructions and remove redundant generation (e.g. stop asking for explanations you discard) rather than hard-capping length on every call. Measure success rate before and after any output reduction.

Find your cheapest passing model

Let ModelSwitch test cheaper models against your real tasks and report which ones hold up.