LLM cost optimisation
Cutting LLM costs with caching, model routing, batching and budgets.
Bookmark
Bookmarks are kept in your free account, so you can find this page again on any device.
Sign in to bookmarkOverview
Model bills grow with tokens, and tokens grow quietly: longer prompts, bigger retrieved context, agents that loop, retries after failures. Cost optimisation starts with measuring cost per request and per feature, then working through the levers in order of effort. Trim what you send, cache the parts of the prompt that repeat, send easy requests to a smaller model, and move work that can wait to a batch API.
Every saving needs a check that quality held, so cost work sits on top of evals and observability. A gateway such as LiteLLM gives you per-team keys, spend tracking and hard budgets, and local or open models are an option once volume is high and the task is narrow. The goal is a predictable cost per task you can explain, not the cheapest possible call.
What you will practise
- Attribute token spend and cost to each feature and user action
- Structure prompts so stable prefixes hit the provider's prompt cache
- Route simple requests to a smaller model and prove quality held with an eval
- Move non-urgent jobs to a batch API and compare the bill
- Set per-key or per-team budgets and alerts in a gateway
- Cap agent steps and tokens so one task cannot run away
Workflow recipes
- Move an app to a new model without regressions
LLM model migration without surprises: freeze a golden set, run old and new models side by side, fix the prompts, compare cost, then roll out behind a flag.
Articles
- How to choose an LLM for a task without trusting leaderboards
A method for choosing an LLM: build 20 test cases from your own work, then compare accuracy, cost per correct answer, latency and data terms. No rankings.
Tools for this skill
DeepSeek
Chat app and API for DeepSeek's open-weight models.
Groq
Fast inference API on Groq's own LPU chips.
Helicone
AI gateway and observability layer that logs requests, cost and latency.
LiteLLM
Open-source SDK and AI gateway that calls 100+ providers through one OpenAI-style API.
llama.cpp
C/C++ inference engine for GGUF models on CPUs and GPUs; many local tools use it.
OpenRouter
One API and bill for models from many providers, with routing and fallbacks.
Portkey AI Gateway
Open-source gateway for routing model requests with retries, fallbacks and guardrails.
vLLM
High-throughput inference and serving engine for LLMs with an OpenAI-compatible server.
Key terms
Words you will meet while learning this, defined in the glossary.
- AI gateway
A proxy between apps and model providers that gives one API for many models and adds keys, logging, budgets, caching and fallbacks.
- Batch API
A provider endpoint that takes many requests at once and returns results later, typically within 24 hours, at a lower price than live requests.
- Extended thinking
A model setting that gives it a budget of reasoning tokens to use before writing its final answer, trading time and cost for accuracy.
- Latency
The time from sending a request to a model to getting the response, often split into time to first token and total generation time.
- Max tokens
A request setting that caps how many tokens a model may generate in its reply; the model can stop earlier but never goes past the cap.
- Model routing
Choosing which model handles each request, often sending easy requests to a small, cheap model and hard ones to a larger model.
- Prompt caching
Reusing the processed form of a repeated prompt prefix, such as a long system prompt, across requests to reduce latency and cost.
- Reasoning effort
An API setting that controls how much a reasoning model thinks before answering, trading cost and latency against accuracy.
- Semantic caching
Reusing a stored model response when a new request means the same as an earlier one, judged by embedding similarity rather than exact text.
- Test-time compute
Computation a model spends while answering, such as longer reasoning or multiple samples, used to improve answers without retraining.
- Token
The unit of text a model reads and writes, often a word or part of a word; in English a token averages about four characters of text.
- Tokenizer
The algorithm and vocabulary that split text into tokens and map each one to the number a model processes. Each model family has its own.