LLM costs are shaped less by a single model price than by the way an application sends, repeats, stores, and routes work. This guide provides a repeatable method for estimating spend, finding avoidable token usage, and reducing API costs without weakening the user experience or bypassing essential governance controls.
Overview
Cost optimization for an LLM application is a measurement problem first and a model-selection problem second. A cheaper model may reduce the invoice while lowering answer quality, increasing retries, or creating more work for downstream systems. Conversely, a higher-capability model may be justified for a small number of difficult requests but unnecessary for routine classification, extraction, or routing.
The practical goal is to lower the cost of a successful task. Track cost alongside quality, latency, error rate, retry rate, and human review effort. A useful optimization is one that improves the total operating picture, not merely the token line on an invoice.
A reliable workflow has five stages:
- Measure current request volume and token usage.
- Separate input, output, retry, and tool-related work.
- Identify requests that receive more context or model capability than they need.
- Test an intervention against a fixed evaluation set.
- Deploy with limits, monitoring, and a rollback path.
This approach also supports good governance. Cost controls should not encourage unsafe truncation, removal of privacy checks, or silent degradation in high-impact workflows. For related controls, see the AI governance checklist for LLM applications.
How to estimate
Start with a simple monthly estimate for each request type. Do not average all traffic together if your application handles materially different tasks, such as retrieval-augmented answers, short classifications, document summaries, or agentic workflows.
Estimated monthly cost = monthly requests × average cost per request
For a request with separate input and output rates:
Cost per request = (input tokens ÷ billing unit × input rate) + (output tokens ÷ billing unit × output rate)
Then add costs that are not represented by the primary completion call, such as embeddings, reranking, tool calls, storage, retrieval, evaluation runs, and retries. The exact billing unit and rate depend on the provider and model, so enter those values from the pricing information applicable to your account rather than copying a static example from an article.
For a practical spreadsheet, create one row per workflow and include:
- Requests per day or month
- Average and high-percentile input tokens
- Average and high-percentile output tokens
- Model and current input/output rates
- Retry and fallback percentages
- Additional retrieval, embedding, or tool-call costs
- Quality pass rate and human review rate
Use both averages and upper-percentile observations. An average can hide a small number of very large documents that drive cost and latency. If a workflow has a hard context or output limit, record rejected and truncated requests separately; otherwise, an apparent cost reduction may simply reflect failed work.
Inputs and assumptions
Before changing the system, document the assumptions that make the estimate meaningful. These inputs are also the items most likely to change when traffic, prompts, models, or provider pricing changes.
| Input | What to measure | Optimization question |
|---|---|---|
| Request mix | Share of traffic by task and user tier | Can simple requests bypass the most capable model? |
| Input context | System instructions, history, retrieved passages, and metadata | Which content is repeated, stale, or irrelevant? |
| Output length | Requested and actual completion tokens | Can the output schema or limit be made more precise? |
| Retries | Timeouts, validation failures, and manual replays | Are failures creating duplicate calls? |
| Cacheability | Repeated prompts, documents, or responses | Can identical or stable work be reused safely? |
| Quality threshold | Pass criteria, escalation rate, and review effort | What must not regress while reducing spend? |
Context trimming is often a stronger first step than prompt rewriting. Remove duplicated instructions, unused metadata, old conversation turns, and retrieved passages that do not support the current question. In retrieval workflows, test smaller chunks, stricter relevance filters, and a maximum passage budget. Do not trim blindly: preserve the evidence needed for a verifiable answer.
Prompt compression can include shorter instructions, compact schemas, and removal of repeated examples. Keep prompts readable and versioned so that changes remain reviewable. The prompt templates for developers can help teams standardize prompt structure before optimizing it.
Output controls are equally important. Specify the required fields, limit unnecessary explanation, and validate structured output. A short, complete answer is preferable to a severe limit that causes truncation and retries. Store prompt versions and evaluation results; cost optimization without traceability makes regressions difficult to diagnose.
Worked examples
Example 1: Reducing repeated context
Assume a support application sends a long system prompt, the entire conversation history, and several retrieved passages on every request. Its first audit shows that much of the history is irrelevant after the issue has been identified. The team creates a rolling summary for older turns, removes duplicated instructions, and limits retrieval to passages that pass a relevance threshold.
The calculation does not need a guessed provider price to show the effect. If the original request uses 10,000 input tokens and the revised request uses 6,000, input-token consumption falls by 40 percent for that workflow, assuming request volume and output usage remain unchanged. The team should then compare answer quality, citation coverage, latency, and escalation rate before making the change permanent.
Example 2: Routing routine work
Suppose an application performs three tasks: intent classification, field extraction, and complex answer generation. Sending all three to the same high-capability model simplifies implementation but may be inefficient. Create separate evaluation sets and test a routing policy: use a lower-cost model for classification and extraction when it meets the acceptance criteria, while reserving the stronger model for ambiguous or complex cases.
Estimate the blended cost as follows:
Blended cost = (routine request share × routine cost) + (complex request share × complex cost) + retry and fallback cost
Include the cost of misclassification, retries, and escalations. A model that appears cheaper per call may cost more if it produces invalid JSON or requires repeated correction. Use a prompt debugging workflow to distinguish prompt defects from model-capability limits.
Example 3: Caching and batching
Some work is stable or repeated: a policy document may be summarized more than once, or a queue may contain many independent classification requests. Where the provider and application design support it, cache stable results and batch independent tasks. Define cache keys carefully, include the prompt and source-document version, and set an invalidation rule. A stale answer is not a successful optimization.
For asynchronous work, batching may reduce operational overhead, but it can change latency and error handling. Test it only where delayed results are acceptable, and preserve per-item identifiers so a partial failure does not require rerunning the whole batch.
When to recalculate
Recalculate LLM costs whenever a major input changes, not only when the invoice appears unexpectedly. At minimum, review the model rates, context limits, caching terms, and capabilities relevant to your application on a regular schedule. Provider pricing and model behavior can change, so do not treat a historical spreadsheet as a permanent source of truth.
Run an additional audit in these situations:
- Traffic increases or the request mix changes.
- A new model, prompt version, retrieval strategy, or agent tool is introduced.
- Average input or output tokens rise without a planned feature change.
- Retries, fallbacks, latency, or human review increase.
- A provider changes rates, context limits, caching options, or model availability.
- A new privacy, security, or retention requirement changes the architecture.
Use this compact audit checklist:
- Export usage by workflow, model, and date.
- Compare average and high-percentile input and output tokens.
- Identify duplicated context, oversized retrieval results, and unnecessary history.
- Measure retries, fallbacks, invalid outputs, and human escalations.
- Recalculate using current provider inputs and documented assumptions.
- Test one change at a time against quality and safety criteria.
- Set alerts for spend, token volume, error rate, and unexpected routing.
- Record the result, owner, prompt or model version, and next review date.
The best LLM cost optimization program is continuous but controlled. Start with measurement, remove waste before reducing capability, route work according to difficulty, and keep quality and governance in the same dashboard as spend. Revisit the calculation before launches, after traffic changes, and whenever a provider or model changes so that your AI app pricing strategy remains based on current evidence rather than assumptions.