LLM Cost Optimization Techniques Every Engineer Should Know
The first LLM invoice rarely looks alarming. A few thousand requests, a large model as the default, full conversation history on every call. Then traffic grows, an agent loop retries a tool five times, and the bill stops looking like a prototype cost.
I advise teams to treat LLM cost optimization as a systems problem, not a prompt-tuning hobby. The techniques below are the ones I keep seeing pay off: smaller models where they suffice, less context, caches, structured outputs, and hard limits on agents. Most of them require a decision you can encode in code.
A frequent case looks like this. Imagine a support assistant that uses the same frontier model for “What is your refund policy?” and for “Draft a migration plan for this schema.” Both answers can look fine. Only one of those tasks needed that model. Everything that follows is a variation on that mistake: paying for more capability, more tokens, or more iterations than the job requires.
LLM cost optimization starts with the cheapest path that still works
Start with smaller models. Use the cheapest model that can do the work. Reserve large models for coding, planning, complex reasoning, and agent workflows. FAQs, classification, tagging, and extraction usually hold up on smaller models. The quality gap is real on hard tasks. It is often invisible on routine ones.
Multi-model routing turns that policy into a default. Route by complexity instead of sending every request to one model. Sentiment analysis can go to a small model. SQL generation can go to a medium model. Coding can go to a large one. The router is unglamorous, and it is where a large share of the savings lives.
Query classification belongs in front of that router. Classify the request before you process it: FAQ search, RAG, agent, or coding. Then send it down the matching path. This avoids the expensive failure I see most often: an agent stack waking up for a question that a retrieval hit or a small classifier could have answered. Classification itself should run on a cheap model.
Tool-first architecture goes one step further. Call a deterministic tool before you invoke an LLM. Time comes from a clock API. Weather comes from a weather API. Currency conversion comes from a calculator. A database lookup is SQL. Use the model when reasoning is required, not when a function already has the answer.
These four choices are the first layer of LLM cost optimization: do not buy frontier reasoning for a lookup, a classifier, or a clock.
Send less context, then ask for a shorter answer
Input tokens and output tokens both show up on the bill. Context trimming attacks the input side. Send only relevant context. Keep recent messages, drop irrelevant history, and remove duplicates. Reducing a 20k-token conversation to 2k relevant tokens is a realistic cut on a long thread. The model does not need the whole transcript to answer the last question. It needs the facts that still matter.
Conversation summarization is the longer-term version of the same idea. Replace older chat history with a concise summary. Instead of storing 100 messages, store key facts, preferences, and decisions. The assistant still has memory. You stop paying to re-read the chat.
RAG (retrieval-augmented generation) applies the same discipline to documents. Retrieve only the top matching chunks. Do not send a 100-page manual because one paragraph might be useful. Top-k retrieval is a cost control, not only a quality trick. Therefore, if your RAG pipeline still dumps the whole hit list into the prompt “just in case,” you are buying insurance the model cannot spend well.
On the output side, set output token limits. Cap max tokens and write concise prompts: “Answer in 3 bullet points.” Fewer output tokens mean lower cost. They also reduce rambling. However, a low cap without a matching instruction just truncates a long answer. Pair the limit with the format you actually want.
Structured outputs help for the same reason. Ask for JSON or a fixed schema instead of long paragraphs. You spend fewer tokens, parsing gets easier, and you retry less often because the shape is constrained.
Batch processing reduces call overhead when the task is repetitive. Classify, extract, tag, or enrich many records in one request instead of one call per row. This is a poor fit for interactive chat. It is a good fit for overnight enrichment jobs.
Taken together, trimming, summarizing, retrieving chunks, and constraining the answer are LLM cost optimization on the token path: send less, ask for less.
Cache what you already paid for, then put rails on agents
If the same question keeps arriving, do not keep paying for it. Response caching stores answers for frequent questions and returns the cached result on a repeat. Invalidate when the source of truth changes. A cached refund policy from last quarter is not a saving. It is a support incident.
Prompt caching targets the prefix you send every time: system prompts, documentation, and standing instructions. It is particularly useful for agents, RAG systems, and documentation assistants, where the same large prefix sits in front of a short user question. You still pay for the unique tail. You stop paying full price for the part that never changes.
Semantic caching matches on meaning rather than exact wording. “What is OAuth?” and “Explain OAuth” can share one cached response. Exact-match caches miss those. Semantic caches catch them. In addition, keep a similarity threshold and fall back to a live call when two queries look close but need different answers.
Agents need a different kind of control. Agent guardrails cap max iterations, max tool calls, max tokens, and timeouts. Without those limits, agents become the largest line item. A loop that “tries one more tool” is polite in a demo and expensive in production. I advise setting the caps in the same place you set the model name.
None of this holds without visibility. Cost monitoring dashboards should track cost per user, cost per request, tokens per request, cache hit rate, model usage, and agent cost. Watch daily spend, spikes, expensive prompts, expensive users, and expensive agents. LLM cost optimization without a dashboard is guesswork.
Start LLM cost optimization with these five moves:
- Default to a small model and route up only when classification says the task needs it.
- Trim context and summarize old history before every call, and retrieve chunks instead of whole documents.
- Cache frequent answers, repeated prompt prefixes, and semantically similar questions.
- Put iteration, token, and timeout caps on every agent, and prefer tools over model calls when a function already has the answer.
- Instrument cost per request, cache hit rate, and agent spend before you add more features.
The pattern is consistent. Pay for reasoning when you need reasoning. Pay for retrieval when you need facts. Pay for a large model when the small one fails the task. Everything else is overhead that compounds quietly until someone opens the billing page.
Which of these LLM cost optimization techniques is still missing in your stack: routing, caching, or agent limits?

