Token Cost Optimization for LLM Selection: A Practical Guide
As model capabilities converge, the real cost driver is how you call them, not which one you pick. This article treats token spend as a first-class engineering metric alongside accuracy, covering six levers: context reduction, model routing, caching and reuse, batch processing, output-side controls, and observability. It includes a per-call logging schema, a context-building example, and a routing rule example to help teams cut quota spend without sacrificing quality.
Many teams run into the same problem after integrating large language models: quality targets are met, but the bill is higher than expected. The source material shows a typical scenario in which several models produce comparable output quality on the same table-generation task, yet their token consumption differs significantly. This article strips out the specific products and frames the issue as a general problem of LLM invocation cost governance. It covers six areas — task definition, context reduction, model routing, cache reuse, batching, and observability — to help developers reduce quota spend substantially at the same quality level.
1. Reframing the Problem: Cost Competition in an Era of Converging Quality
As the capabilities of underlying models gradually converge, the core selection metric is shifting from “can it get this right” to “what does getting it right cost.” In a structured table-generation task, multiple models fully satisfy the field, format, and constraint requirements and receive identical scores. But input prompt length, output redundancy, and retry counts differ, so the final billed quota can vary by several times. This shows that the cost difference does not come from capability, but from how the model is called.
What is needed, therefore, is an engineering method that manages cost as a technical metric on par with accuracy.
2. Measure First: Establish a Comparable Cost Baseline
Optimization must be measurable before it can begin. Log the following fields for every call:
{"task_id": "report_table_001","model_route": "fast | balanced | strong","prompt_tokens": 1820,"completion_tokens": 640,"retry_count": 0,"cache_hit": false,"latency_ms": 4200,"quality_score": 1.0}
With these metrics in place, you can compute cost per unit of quality — cost divided by quality score — which avoids chasing a low price at the expense of output quality.
3. Context Governance: The Biggest Savings Lever
Input tokens are usually where waste is worst. Practices that work in production include:
- Deduplication and trimming: remove repeated examples, stale conversation history, and irrelevant log fragments;
- Structured compression: convert long documents into bullet-point summaries or tables instead of pasting entire passages;
- Retrieval instead of full context: use vector retrieval to pull back only the relevant fragments;
- A fixed system prompt: place stable instructions up front to improve cache hit rates.
Example:
def build_context(query, docs, top_k=5):hits = retrieve(query, docs)[:top_k]body = "\n".join(f"- {h.text.strip()}" for h in hits)return SYSTEM_PROMPT + "\n[Sources]\n" + body + f"\n[Question]\n{query}"
4. Model Routing: Match Compute to Difficulty
Not every request needs the strongest model. Requests can be tiered by task complexity: extraction, classification, and formatting go through the lightweight route; multi-step reasoning and long-form writing go through the high-capability route. Routing rules can be implemented with heuristic conditions such as input length, number of fields, and whether tool calls are required, or with a small model that predicts difficulty in advance.
def route(req):if req.need_reasoning or req.input_len > 8000:return "strong"if req.output_schema and req.steps <= 2:return "fast"return "balanced"
5. Caching and Reuse: Reduce Repeated Payment to Zero
Build a semantic cache for high-frequency, stable requests so that a hit returns the result directly; reuse system prompt caching for requests that share the same prefix; and for tasks that can run offline, use the batch asynchronous API, which is usually more economical than real-time calls.
6. Output-Side Control
Explicitly constrain the output format, field range, and maximum length, and require “return only the result, no explanation,” which significantly reduces wasted completion tokens. At the same time, set a maximum retry count and an automatic fallback policy so that failing requests do not repeatedly burn quota.
7. Observability and Continuous Iteration
Wire token metrics into the monitoring and alerting system, and set budget thresholds per business line. Review the cost-per-unit-of-quality trend weekly, and reinvest the saved quota into inference tasks that are more worthwhile. The ultimate goal of engineering governance is not to drive unit price down at all costs, but to make every unit of quota produce real business value while quality stays unchanged.
