Halving LLM inference cost without touching answer quality
Prompt caching, cross-provider routing and tracing — the three levers that cut our production LLM bill by 50%.
- LLMOps
- Cost
- Langfuse
The first version of most LLM features is built for correctness. The second version has to be built for cost. On a text-to-MongoDB analytics system I work on, we cut inference spend by 50% with no measurable drop in answer quality. Nothing about it was exotic: we measured, cached and routed.
1. Measure before you optimise
You can’t cut a cost you can’t see. We traced every call with Langfuse: model, prompt version, input and output tokens, latency, and which pipeline stage made the call. Within a week the picture was clear. A few stages produced most of the spend, and most of their input tokens were the same long system prompts and schema descriptions sent over and over.
If you only add one thing this sprint, add tracing. Every later decision depends on it.
2. Cache the parts that don’t change
Prompt caching pays off when a large, stable prefix (instructions, schema, few-shot examples) sits in front of a small, changing suffix (the user question). We restructured prompts so that:
- static content comes first and is byte-for-byte identical between calls,
- per-request content (the question, retrieved rows, dates) comes last,
- prompt versions are pinned, so a stray whitespace change doesn’t quietly break the cache.
3. Route each task to the cheapest model that passes
Not every step needs a frontier model. Intent classification, query validation and summary formatting run well on small, fast models. Generating a complex aggregation pipeline doesn’t. We split the pipeline by task and gave each stage the cheapest model that passed its evaluation set, across more than one provider.
| Stage | Needs | Model tier |
|---|---|---|
| Intent & routing | Speed | Small |
| Query generation | Reasoning | Large |
| Answer formatting | Fluency | Small / mid |
4. Keep an evaluation set as the gate
Every routing or prompt change ran against a fixed set of real questions with known-good answers before it shipped. That gate is what lets you say “no measurable loss in quality” and mean it.
The takeaway
Cost work is ordinary engineering: instrument, find the hot path, remove redundant work, and use evaluations to prove nothing broke.