LLM API Token Pricing and Practical Cost Optimization
Learn how input, output, and cached input token pricing affect LLM API costs, and how prompt caching, output limits, context management, model routing, and observability can help keep spending predictable as usage grows.
1. Why Token Pricing Matters for LLM API Costs
In production deployments of large language models (LLMs), API usage can be a major expense, often second only to compute infrastructure. Unlike traditional per-request billing, mainstream LLM APIs generally charge by token.
Tokens are the units into which a model breaks text, whether it is English, Chinese, or code. Providers charge separately for the tokens sent to the model and those generated in its response. Understanding this pricing model is essential for cost management and should be part of architecture planning.
2. Three Token Pricing Categories
Token-based pricing typically distinguishes between three categories:
- Input tokens: Tokens consumed by prompts and context sent to the model. These generally cost less than output tokens.
- Output tokens: Tokens generated in the response. Their unit price is often several times higher than input pricing because autoregressive generation requires computation one token at a time.
- Cached input tokens: Input tokens that qualify for server-side context caching can be billed at a substantially lower rate than regular input tokens.
These price differences create opportunities for optimization. As an illustration—not a quote for any specific product—output tokens might cost five to ten times as much as input tokens, while cached input might cost one-tenth as much as regular input, or less.
Making better use of caching and reducing unnecessary output can therefore lower the bill for the same volume of business activity.
3. Prompt Caching: A Key Cost-Saving Lever
Cached input is cheaper because the server reuses previously computed context states, such as the key-value cache (KV cache), rather than recomputing the same prompt prefix.
To benefit from this, organize requests around a stable prefix. Put content that changes infrequently—such as system instructions, tool definitions, long documents, and knowledge base excerpts—at the beginning of the prompt. Place the user question, which changes with each request, at the end.
# Illustrative cache-friendly prompt structuredef build_request(system_prompt, knowledge_base, user_query):# Stable prefix: eligible for cached input pricing on a cache hitstable_prefix = f"{system_prompt}\n[Knowledge Base]{knowledge_base}"# Variable suffix: billed as regular inputvolatile_suffix = f"\n[Question]{user_query}"return stable_prefix + volatile_suffix
Keeping the prefix byte-for-byte identical across requests helps improve cache reuse. By contrast, putting timestamps, random IDs, or other changing content at the beginning can prevent cache hits and undermine expected savings.
4. Practical Engineering Strategies
Reduce output length. Use explicit output requirements and structured prompts, such as requesting JSON or setting a word limit, to reduce redundant generation. This directly targets the more expensive output tokens.
Trim and summarize context. Maintain rolling summaries of long conversations and retain only essential information to prevent the context from growing indefinitely.
Batch requests and use asynchronous processing. Group non-real-time tasks and stagger requests. Coordinate with server-side rate limits to smooth traffic peaks.
Route tasks by complexity. Send simple tasks to smaller models and escalate complex tasks to larger models, rather than using a large model for every request.
Build observability into the application. Use logging, monitoring, and alerts to track token consumption. Attribute usage to business units and application scenarios so that unusual spending can be identified.
5. Architecture-Level Cost Management
Cloud infrastructure can support these practices through several general-purpose service categories:
- Object storage for archiving long documents and supporting preprocessing.
- Message queues for smoothing demand and enabling asynchronous inference workflows.
- Container platforms for elastic scaling as traffic changes.
- Logging, monitoring, and alerting for real-time dashboards of token usage and cost.
Cloud platforms, including Baidu AI Cloud, offer general-purpose service categories that may serve as a foundation for this architecture, subject to availability in the intended deployment region.
The goal is to move cost management from retrospective bill reconciliation to ongoing visibility and control.
6. Conclusion
The central pricing pattern is straightforward: input tokens are generally cheaper than output tokens, while cached input offers the greatest savings on reused context.
Effective cost optimization is not a single technique. It combines cache reuse, concise output, context management, tiered model routing, and observability. Embedding these practices into everyday development helps keep LLM API spending predictable and sustainable as usage scales.
