Token Pricing and Cost Control for LLM-Powered Coding Assistants
This article explains how tiered token pricing works for large language model APIs—covering standard input, cached input, and output tokens—provides a reusable cost estimation formula, and outlines engineering practices for controlling token spending in long-context, multi-iteration coding workflows.
Introduction: Whether Your Quota Lasts Depends on How You’re Billed
Recent reports indicate that an intelligent coding tool offers several hundred dollars in dedicated credits for specific large language models. The exact source, validity period, and applicable scope of such credits should be confirmed with official announcements. A few hundred dollars may sound generous, but in scenarios involving long contexts, multi-turn iterations, and large codebase analysis, token consumption often exceeds expectations. How long a quota lasts depends on three things: the unit price of each token type, cache hit rates, and the context length carried by each request.
This article explains the tiered pricing model of LLM APIs, provides a reproducible estimation formula, and lists engineering practices for cost optimization.
1. Three Billing Categories
LLM APIs are typically billed per million tokens. Mainstream pricing models generally distinguish three categories:
| Billing Type | Trigger Condition | Cost Characteristics |
|---|---|---|
| Standard input | Context in the request that does not hit the cache, such as code, prompts, and conversation history | Standard input rate |
| Cached input | The request prefix matches a previous request and hits the KV Cache | Unit price far below standard input |
| Output | Model-generated responses, code, and explanations | Highest unit price |
All rates below are example values for demonstration only and do not represent any vendor’s actual pricing: standard input at $1.40 per million tokens, cached input at $0.26 per million tokens, and output at $4.40 per million tokens. All amounts exclude tax.
The cost of standard input comes from the model parsing and encoding the context. If every request resends identical content and the model cannot reuse previously computed results, this cost recurs repeatedly.
Cached input relies on the KV Cache. In multi-turn conversations or continuous code completion, if a new request’s prefix matches a previous request, the model can directly reuse the computed key-value pairs without recomputing attention. Because the computation load drops, platforms offer a lower unit price. Caching only works for exactly identical prefixes—any change in the prefix can invalidate the cache.
Output tokens carry the highest unit price due to autoregressive generation. The model generates only one token at a time, and each token requires a full forward pass and sampling step. As a result, generating 1,000 tokens requires far more computation than encoding 1,000 input tokens.
2. How to Estimate Quota Consumption
Use the following formula for estimation:
Unit cost (USD per million tokens) = input share × [(1 − cache hit rate) × standard input price + cache hit rate × cached input price] + output share × output price
Usable tokens (millions) = credit amount ÷ unit cost
Here, the input share and output share refer to each type’s proportion of total tokens, and together they sum to 1. The cache hit rate is the proportion of input tokens that hit the cache.
Assume a $300 credit, with output accounting for 10% of total tokens and input for 90%. Plugging these into the formula gives:
| Cache Hit Rate | Unit Cost (USD per million tokens) | Usable Tokens (approx.) |
|---|---|---|
| 0% | 1.70 |
