LLM API Cost and Model Selection: A Practical Guide to Lowering Cost Per Task
This article explains why your API bill can rise even after token prices drop, and shows that the real metric is cost per task rather than price per token. It walks through a reusable cost estimation model, six optimization layers spanning prompt and context compression, prefix caching, batch processing, output length control, tiered model routing, and observability, plus guidance on balancing quality against cost and avoiding vendor lock-in. A final checklist helps teams cut cost per task while keeping quality roughly flat.
1. Why the Bill Goes Up Even When Unit Prices Go Down
In recent discussions about the latest generation of ultra-large-scale parameter model APIs, two complaints come up most often: that they are “expensive,” and questions about whether they still represent the top tier of domestic models. Looked at together, both point to the same engineering problem: cost per task, not price per token.
Unit price is only one factor in the bill. The real bill can be approximated as:
cost per task ≈ (input tokens × input price + output tokens × output price) × number of calls
Context length, retry count, how often the system prompt is repeated, and whether cache hits occur all amplify each term in the formula above.
So when a new-generation model cuts its input price from 20 CNY per million tokens to a lower range, the bill can still go up. There are usually four reasons:
- Longer context: To get better results, engineering practice tends to pack more retrieved snippets, conversation history, and tool return values into the context, and input tokens grow several times over.
- Longer output: Reasoning models generate a long chain of thought before giving a conclusion, so output tokens are often an order of magnitude higher than expected, and the output price is usually higher than the input price.
- Retries and multiple candidates: Using multiple samples, voting, or self-verification to improve stability multiplies the number of calls.
- Capability overkill: Handing simple tasks to the most powerful model is a classic case of resource misallocation.
The conclusion is clear: judging whether a model API is “expensive” has to come back to task-level cost and the ratio of cost to quality gained, not to staring at the price list.
2. The Cost Model: Do the Math Before Optimizing
Teams should build a reusable cost estimation sheet before integration, with at least the following fields:
| Field | Meaning | Common mistake |
|---|---|---|
| Average input tokens | Includes system prompt, retrieved snippets, history | Forgetting that the system prompt is billed repeatedly |
| Average output tokens | Includes the reasoning process and structured results | Estimating only the length of the final answer |
| Calls per task | Includes retries, multiple candidates, second-pass verification | Assuming a single call |
| Cache hit rate | Discount from prefix reuse | Assuming 100% hits |
| Failure rate | Timeouts, rate limiting, parse failures | Leaving retry costs out of the estimate |
An illustrative estimation function:
def estimate_cost(n_tasks, in_tok, out_tok,p_in, p_out, calls=1, cache_hit=0.0, cache_discount=0.5):eff_in = in_tok * (1 - cache_hit * cache_discount)per_task = (eff_in * p_in + out_tok * p_out) * callsreturn per_task * n_tasks
Once this sheet is filled in, most arguments that “feel expensive” turn into a numbers problem you can actually discuss.
3. Six Layers of Optimization: From Prompts to Routing
3.1 Prompt and Context Compression
The system prompt is a fixed cost paid on every request. Trim it to three parts: role, constraints, and output format, and move lengthy background into on-demand retrieval. Deduplicate, rerank, and truncate retrieved snippets so only passages strongly relevant to the current question remain. In practice, context compression is the fastest win with the lowest risk.
3.2 Prefix Cache Reuse
Put the stable, unchanging system prompt, tool definitions, and knowledge notes at the very front of the request so the invariant prefix can be reused and earn a billing discount. The engineering key is stable ordering: once the order of the prefix content changes, reuse breaks.
[stable prefix: role/rules/tool schema] → [semi-stable: knowledge snippets] → [volatile: user question]
3.3 Batching and Asynchrony
For offline summarization, bulk labeling, and log classification, prefer asynchronous batch APIs. Trading throughput for latency usually gets you a better billing tier.
3.4 Output Length Governance
Constrain the output structure explicitly (field lists, word limits, enumerated values) instead of letting the model improvise. For reasoning-style output, switch from “analyze first, conclude later” to “conclusion first, with only the necessary evidence,” which can compress output tokens significantly.
3.5 Tiered Model Routing
This is the most effective architectural lever for cost control: use a lightweight model or rules for intent recognition, format conversion, and simple Q&A, and route only high-value tasks such as complex reasoning, long-form writing, and code refactoring to the most powerful model.
def route(req):if req.task_type in {"classify", "extract", "format"}:return "small"if req.estimated_difficulty < 0.4:return "mid"return "large"
3.6 An Observability Loop
Feed token usage, cache hits, failed retries, and cost per task into your logging service and monitoring alerts, and set cost threshold alarms. Without measurement there is no optimization, so cost governance has to be an ongoing practice.
4. Balancing Quality and Cost: Don’t Pay Full Price for “the Strongest”
The value of an ultra-large-scale parameter model lies in the ceiling it sets for complex tasks, not in the floor it sets for every task. A simple rule for deciding whether you need the strongest model: is the business loss from one task failure higher than the cost difference relative to a lighter model? If the answer is no, drop to the more economical tier.
You should also maintain a small evaluation set covering typical intents and boundary cases, and run a regression after every change to prompts, routing thresholds, or model tiers, so you don’t “save money and lose quality.” Evaluation metrics should record both task success rate and cost per task, forming a two-dimensional view rather than looking at a single dimension.
5. A Rational View on “Regaining Past Glory”
Parameter scale and leaderboard placement are only the entry ticket. What really determines a developer’s opinion is three things: stable rate limits and availability, clear and predictable billing, and a supporting engineering ecosystem (the coordination of general cloud product capabilities such as object storage, message queues, container platforms, logging services, and monitoring alerts). For users, rather than arguing over whether a single model represents the top tier, it is better to make model selection a replaceable abstraction layer: wrap the calling interface uniformly, keep the ability to hot-swap between multiple models, and keep the business from being locked into a single vendor.
6. Implementation Checklist
- Build a cost estimation sheet and do the math before selecting a model.
- Trim the system prompt and compress the retrieved context.
- Keep prefix ordering stable and turn on cache reuse.
- Constrain output structure and govern output length.
- Implement tiered routing and reserve the strong model for high-value tasks.
- Wire up monitoring and alerts to form a two-dimensional cost-quality regression.
Following this path, most teams can cut cost per task by a meaningful margin while keeping quality roughly flat. Cost is never the “original sin” of any particular model; it is a health check on your architecture and engineering discipline.
