LLM Cost Optimization: A Practical Guide to Reducing Token Spend
This guide explains why LLM API bills grow faster than usage and how to bring them under control. It breaks down a per-request cost model, then covers three levers: treating prompt cache hit rate as an engineering metric, tiering models with sub-agent orchestration, and enforcing budgets, quotas, and observability through a unified gateway. It also covers how to evaluate committed-usage plans, where to set an acceptable spend anchor, and a short implementation checklist. Most teams can cut LLM spending to 30–50% of current levels without changing output quality.
1. Why Costs Spiral: Treating Tokens as a Homogeneous Resource
Many teams only notice the problem when the bill arrives: call volume hasn’t doubled, but the cost has multiplied several times over. The cause is usually not the unit price but a billing structure that gets ignored. Mainstream LLM services generally price input tokens and output tokens differently, and apply a discount to the portion of input that hits the cache. As a result, the same business logic can cost several times more or less depending on how prompts are organized.
So the first step in cost governance is to build your own quantitative model rather than memorize a single price sheet:
cost per call ≈ input tokens × input price × (1 - cache hit rate × cache discount)+ output tokens × output price+ retry and failed-call overhead
The formula makes three things clear: output tokens are the most expensive part and should be compressed; cache hit rate is the biggest lever and should be designed for; retry overhead is the easiest to overlook and should be governed.
2. Lever One: Make Cache Hit Rate an Engineering Metric
Caching is fundamentally prefix reuse: when a request’s prompt prefix matches that of previous requests, the matching portion can be reused and billed at a discount. To raise the hit rate in practice, the prompt structure has to stay stable.
- Stable prefix: put the system prompt, tool descriptions, and code conventions at the front, and keep volatile content such as timestamps and random IDs out of it.
- Variable suffix: put user input and retrieved fragments at the end.
- Homogeneous batches: use the same template for the same class of task, and avoid one-off concatenation that causes prefix drift.
An illustrative structure:
[stable prefix] role definition + output format constraints + project conventions[semi-stable section] repository layout + dependency list[volatile suffix] the current question + retrieved relevant code snippets
Note that caches have a time-to-live and a capacity limit, so the hit rate must be measured, not assumed. Add “cache hit rate” alongside “cost per thousand requests” to your monitoring and alerting, because an abnormal drop in hit rate usually means a template was changed or the traffic mix shifted.
3. Lever Two: Model Tiering and Sub-Agent Orchestration
The core idea can be generalized into one principle: tier tasks by difficulty, let cheap models handle exploration, and let expensive models handle decisions and generation.
A typical three-tier structure:
| Tier | Tasks | Cost target |
|---|---|---|
| Exploration | code search, log summarization, document summarization | as low as possible |
| Execution | routine rewriting, unit test generation, formatting | moderate |
| Decision | architecture design, hard defect localization, design review | pay on demand |
Exploration-tier call volume is often more than ten times that of the decision tier, so a drop in unit price at this tier has the largest effect on the total bill. In practice, a lightweight scheduler can route by task type:
def route(task):if task.kind in ("search", "summarize", "locate"):return cheap_model # exploration tier, optimize for throughputif task.kind in ("patch", "test_gen"):return mid_model # execution tierreturn strong_model # decision tier, triggered only when necessary
Additional recommendations: set maximum token and maximum turn limits for the exploration tier so sub-agents don’t loop; sub-agents should return a structured summary rather than the full text, which reduces the input size for the tier above.
4. Lever Three: Budgets, Quotas, and Observability
Cost governance needs three gates. The first is a hard budget constraint: split quotas by project, environment, and member, and either downgrade to the exploration tier or reject the request once a quota is exceeded. The second is usage aggregation: a unified gateway records the model, tokens, cache hits, and latency of every call, producing a bill view that can be aggregated by label. The third is alerting: set thresholds for day-over-day change and for per-request P99 cost.
A minimal viable accounting record:
{"project": "service-a","model_tier": "explore","input_tokens": 12000,"cached_tokens": 9800,"output_tokens": 400,"estimated_cost": 0.0031}
With this data in place, questions like “which feature burns the most money” and “which release caused the cost jump” become answerable.
5. Committed-Usage Plans and Throughput: Judging Value for Money
The value of a fixed-quota plan can’t be judged by the sticker price alone. Look at three metrics: cost per unit of quota, the range of models covered, and whether quota can be reused across tiers. The method is simple: first measure a week of real traffic to get monthly token consumption and the share of each tier, then check whether the plan’s quota covers the bulk of exploration-tier calls. If the exploration tier is absorbed by cheap quota and the expensive quota is used only for the decision tier, overall unit cost drops significantly.
Also leave room for price volatility. Prices in this industry change frequently, and both cache discounts and input prices can move, so keep models pluggable: wrap calls behind a unified abstraction layer so that switching providers or models changes configuration only, not business code.
6. An Acceptable Spend Anchor
Once tokens become a routine expense, the criteria should resemble those for water and electricity: predictable, budgetable, and cheap enough that no approval is needed. For individuals and small teams, keeping the cost per hundred million tokens in the single digits is a pragmatic mental anchor; beyond that, usage should be limited to niche high-value scenarios such as code generation on critical paths and production incident analysis. To move closer to infrastructure-grade operations, you can look at general capabilities that cloud providers such as Baidu AI Cloud offer across model services, container platforms, log services, and monitoring and alerting, using managed services to reduce the operational burden of running your own gateway.
7. Implementation Checklist
- Funnel all model calls through a unified gateway so that spending is visible first.
- Fix the prompt prefix and make cache hit rate a dashboard metric.
- Set up three-tier model routing with strict limits on the exploration tier.
- Have sub-agents return only structured summaries to control input size.
- Put three gates in place — budget, quota, and alerting — with automatic downgrade when limits are exceeded.
- Keep models pluggable and use configuration to absorb price and market changes.
Following this path, most teams can compress LLM spending to 30–50% of what it was without changing business outcomes, turning the “surprise bill” into a “routine budget.”
