How do I reduce LLM API costs?
Measure first, then cut where the tokens are. Most savings come from caching the repeated start of your prompts, sending less context, capping output, moving non-urgent work to batch jobs and routing easy requests to a smaller model. Check each change with evals so quality does not drop.
Last updated
Start by measuring the token mix
Log token counts per feature, split into fresh input, cached input and output. Cost depends on that split, not just the total: an output-heavy feature and an input-heavy one with the same total can cost very different amounts, and the cheapest model for one may be the dearest for the other.
Once you can see which feature and which part of the prompt uses the most tokens, you know which lever to pull first.
The levers, roughly in the order to try them
Each one is independent, so you can combine them.
- 1Prompt caching: put fixed instructions, tool definitions and shared documents at the start of the prompt and changing content after it, so repeated input is billed at the cached rate.
- 2Send less context: retrieve the few relevant passages with RAG instead of whole documents, and trim old chat history.
- 3Cap output: ask for short answers, set a maximum output length and use structured output instead of long prose where code reads the result.
- 4Batch API: move work nobody is waiting on, such as tagging, summaries and nightly reports, to batch jobs, which providers usually price lower.
- 5Model routing: send easy requests to a smaller model and escalate only the hard ones.
- 6Cache whole responses for repeated questions, so identical requests do not hit the model at all.
- 7Cut wasted calls: retries on errors, agents looping without a step limit, and duplicate calls from the front end.
- 8Compare providers on your own token mix and check for off-peak or discounted pricing that suits your workload.
Protect quality while you cut
Every saving that changes the model, the prompt or the context can change answers. Keep a set of evals built from real cases and run it before and after each change. A cheaper setup that fails your evals is not cheaper once support tickets and rework are counted.
Check it for your case.
Common follow-up questions.
Is a smaller model always cheaper?
Per token, usually. Per task, not always: if it needs longer prompts, more retries or more human fixes to reach the same quality, the total can be higher. Compare cost per finished task.
Does prompt caching change the answers?
No. Caching reuses the processed prompt prefix; the model sees the same input. It changes cost and often speed, not the output.
Should we self-host an open model to save money?
Sometimes, at steady high volume. Self-hosting swaps per-token fees for the cost of machines, setup and engineers to run them, so compare the full cost, not only the per-token price.
Related questions and guides.
- Who owns the code when you hire a software agency?Question
- Is my AI system high-risk under the EU AI Act?Question
- Does my app need to be HIPAA compliant?Question
- AI agent vs workflow automationComparison
- RAG vs fine-tuningComparison
- GlossaryEvery term, in plain words.
- LLM API pricingModel prices side by side, with sources.
- Pricing librarySoftware and API prices, each with its source and date.
- AI automationHow OceanLab automates company workflows with AI.