LLM API Pricing Explained: Token Costs and Context Caching for Cost Optimization
This article breaks down how large language model API billing works and how to control inference costs in production. It explains why input and output tokens are priced separately, why output tokens typically cost several times more than input, and how to estimate per-request cost with a simple formula. It then covers context caching—cache writes, cache hits, and TTL tiers—and shows how restructuring prompts into a stable prefix and a dynamic suffix can unlock order-of-magnitude discounts. Finally, it explains tiered service pricing (standard vs. high-speed) and how to route traffic by SLA, closing with a practical cost-reduction checklist for engineering teams.
1. Why You Need to Understand LLM API Billing
As large language models are increasingly embedded in search, coding assistants, customer support, data analysis, and other business scenarios, inference cost has become an operating expense that engineering teams can no longer ignore. Unlike traditional per-call or per-CPU-hour billing, LLM services generally use per-token metering, which means cost is tied directly to prompt length, context window usage, and output length. Without a clear understanding of the billing rules, developers can easily find themselves with a bill several times over budget within a week of launch.
So before integrating any LLM service, understanding three key dimensions of its billing model—base token pricing, context caching, and tiered service pricing—is the prerequisite for cost control. Each is covered below.
2. Base Token Pricing: Why Input and Output Are Billed Separately
2.1 The Basic Unit of Measurement
A token is the smallest unit a large model uses to process text. A single Chinese character, an English word, or a stretch of punctuation may each correspond to one or more tokens. Almost all mainstream inference services split a request into input tokens and output tokens and meter them separately:
- Input tokens: everything sent to the model, including the system prompt, conversation history, the user’s question, tool-call results, and so on.
- Output tokens: the content the model generates and returns to the caller.
2.2 Output Is Usually Priced Much Higher Than Input
The industry-standard practice is to price output tokens higher than input tokens, typically 3–5 times the input price. The reason lies in the different compute paths: the input portion can be processed in parallel through a single forward pass, whereas the output must be generated autoregressively token by token, with each token requiring a full decode computation. Both compute consumption and response time on the output side are far higher than on the input side.
Consider a typical industry pricing structure as an example (values are illustrative only):
| Model version | Input | Output | Cache hit | Cache write (5 min) | Cache write (1 h) |
|---|---|---|---|---|---|
| Standard model | ¥13.00 / MTok | ¥68.00 / MTok | ¥1.30 / MTok | ¥13.00 / MTok | ¥26.00 / MTok |
| High-speed model | ¥26.00 / MTok | ¥136.00 / MTok | ¥1.30 / MTok | ¥26.00 / MTok | ¥52.00 / MTok |
| Previous-gen model | ¥20.00 / MTok | ¥100.00 / MTok | ¥2.00 / MTok | ¥20.00 / MTok | ¥40.00 / MTok |
Two cost-sensitive conclusions follow from this example:
- Model upgrades do not necessarily raise prices. Even with stronger capabilities, a newer generation can have a lower unit price than the previous one—a dividend of inference-efficiency improvements such as better architecture and more efficient inference engines. Upgrading the model version is itself one of the most direct cost-reduction levers.
- Output is the main cost driver. For the same 1M tokens, output cost is roughly 5 times input cost. Controlling output length within what the business allows (constraining the answer format, setting a maximum number of generated tokens) has the greatest leverage on total cost.
2.3 A Simple Cost Formula
The cost of a single request can be estimated as follows:
cost per request = input tokens × input price+ output tokens × output price+ cache-hit tokens × cache-hit price+ cache-write tokens × write price for the corresponding TTL
For example, a typical RAG (retrieval-augmented generation) call with 80K tokens of retrieved documents in the input and 2K output tokens, calculated roughly at the standard-model prices above:
cost ≈ 80K × 13/1M + 2K × 68/1M≈ 1.04 + 0.136≈ 1.18
If daily volume reaches the order of a hundred thousand calls, monthly cost can climb into the hundreds of thousands. This is exactly the real-world backdrop that makes the caching optimization below so important.
3. Context Caching: From Repeated Payment to Hit Discounts
3.1 The Basic Logic of Cache Billing
Multi-turn conversations, agent loops, and batch document Q&A share a common trait: a large amount of prefix content is identical across many requests—for instance, a fixed system prompt, few-shot examples, or long knowledge-base documents. Without any optimization, this repeated content is billed at full input price every time, wasting both cost and prefill compute on the inference side.
Context caching is designed precisely for this pain point. Its billing is typically split into three parts:
- Cache write: the fee charged the first time a prefix is written into the cache, usually on par with or slightly higher than the normal input price (in the table above, the 5-minute-TTL write price equals the input price, while the 1-hour TTL is 2 times the input price).
- Cache hit: when a later request reuses a cached prefix, it is billed at a steep discount—only one-tenth of the normal input price in the example (¥1.30 vs. ¥13.00).
- Cache TTL: the cache is tiered by time-to-live, such as a 5-minute tier and a 1-hour tier; the longer the TTL, the higher the write price.
3.2 Why Longer TTL Costs More to Write but Less to Hit
There is a typical “infrastructure cost pass-through” logic here: cached content occupies high-bandwidth GPU memory or dedicated storage on the inference cluster, and the longer it is retained, the higher the occupancy cost—so the write price for a longer TTL rises accordingly. Once a hit occurs, however, the inference service skips the repeated prefill computation for that prefix, and the saved compute is returned to the caller in the form of a discount.
3.3 Practical Advice: Make the Prefix “Cacheable”
To maximize the cache hit rate, the order in which you organize the prompt is critical. The core principle is to put stable, unchanging content at the very front of the prefix and content that changes every time at the end:
[system prompt (stable)] → [few-shot examples (stable)] → [retrieved documents (relatively stable per knowledge-base partition)] → [current user question (changes every time)]
A counter-example: inserting volatile content such as timestamps or random IDs in the middle of the prefix causes a “break” in the cache, after which all subsequent content must be billed again at full price:
❌ [system prompt] → [current timestamp] → [few-shot examples] → [user question]
Also note:
- Multi-turn conversations: history messages naturally form a growing prefix; keep them concatenated verbatim without rewriting so you can hit the cache written in the previous turn.
- TTL choice: for low-frequency interactions (a user asking follow-ups a few minutes apart), a short TTL is enough; for high-frequency batch tasks (the same document queried hundreds or thousands of times), a long TTL is worth it to amortize the write cost.
- Hits beat prompt-length trimming: when the cache-hit discount reaches the 10x level, even writing a longer system prompt can lower total cost, as long as it hits reliably.
4. Standard vs. High-Speed: How to Choose Between Tiers
4.1 What Tiered Pricing Means
In the example, the high-speed tier is priced at 2 times the standard tier. This is a common industry tiering model: the high-speed tier usually corresponds to a higher resource-guarantee level, such as more stable low latency, a lower probability of queuing, and higher concurrency quotas. The standard tier serves latency-insensitive workloads at a lower unit price.
4.2 Route by Business SLA
The rational approach is not to pick one tier globally but to route by call scenario:
| Scenario | Recommended tier | Reason |
|---|---|---|
| Online consumer-facing chat, real-time coding assistants | High-speed | First-token latency directly affects user experience |
| Offline batch summarization, data labeling, log analysis | Standard | Throughput first, high latency tolerance |
| Scheduled jobs, overnight batch runs | Standard | Can run off-peak, cost-sensitive |
| Real-time agent tool-call chains | High-speed | Multi-hop chains amplify latency at each step |
Implementing this routing at the integration layer is not complex—you only need to distinguish the model-version parameter in the call configuration. Notably, in the example both versions share the same cache-hit price (¥1.30 / MTok), which means that for cache-intensive workloads, the cost premium of the high-speed tier is partly diluted, because the dominant repeated prefix is billed at the same discounted price. This leaves room for a combined strategy of running online traffic on the high-speed tier while still enjoying the cache discount.
5. A Systematic Cost-Reduction Checklist
Bringing the mechanisms above together, here is a directly actionable cost-reduction checklist:
- Upgrade to the newer model version first: when it is both more capable and cheaper per unit, migration is nearly a lossless cost cut.
- Control output length: specify the answer format and length constraints in the prompt, and set a reasonable maximum number of generated tokens.
- Restructure prompts into a “stable prefix + dynamic suffix” layout to maximize the cache hit rate.
- Keep history messages concatenated verbatim in multi-turn conversations to avoid needless cache invalidation.
- Tier calls by SLA: use the high-speed tier for online traffic and the standard tier for offline tasks.
- Set up token-usage monitoring: log the input, output, and cache-hit volume of each request at the application layer, aggregate cost dashboards using the formula above, and catch abnormal consumption early.
- Trim ineffective context: periodically clear redundant history and stale retrieved snippets to avoid “context bloat” driving up input cost.
6. Summary
Cost governance for LLM APIs is essentially a systematic engineering effort over “metering rules + request structure + service tiering.” Understanding differentiated token pricing lets you target output, the main cost driver; understanding the billing split between cache writes and hits lets you obtain order-of-magnitude discounts through prompt restructuring; and understanding the difference in positioning between the standard and high-speed tiers lets you make rational trade-offs between experience and cost. Teams are advised to establish token-usage measurement and cost-review mechanisms early in the integration phase, so that every model call remains observable and optimizable.
