LLM API Pricing Evolution and Cost Optimization Practices
This article explains how LLM API billing has shifted from limited-time discounts to long-term and time-of-use pricing, and what that means for engineering budgets. It breaks down the three structural dimensions of API cost—asymmetric input/output pricing, cache-hit pricing, and peak/off-peak rates—then shows how to build a session-level cost model, avoid four common budgeting mistakes, and implement routing, prefix caching, scheduling, and budget guardrails. It also covers connecting coding assistants through standard API styles and composing the workflow on Baidu AI Cloud.
1. Ask the Right Question: Not “What Discount?” but “What Is the Total Cost?”
When teams evaluate LLM API spending, they usually focus on discount depth: what the current discount is, when the promotion ends, and whether prices return to normal afterward. This view works during promotional periods, but once billing moves from “limited-time discounts” to “long-term pricing plus time-of-use pricing,” the discount is no longer the main variable. Three structural factors actually determine the bill: the per-token price, how requests are distributed across the timeline, and the effective token discount from caching and context reuse.
So the right question about a price change is not “did it go up or down,” but: under the new pricing structure, how much does the unit cost of my typical workload change, and which controllable variables can push that number back down? The rest of this article follows that line of thinking.
2. Three Basic Dimensions of Billing Structure
Mainstream LLM API billing can be abstracted into three dimensions, and understanding them matters more than memorizing any specific number.
The first is asymmetric pricing for input and output. Generation is computationally much heavier than prefill, so the output unit price is typically several times the input unit price. This means “making the model write less” affects the bill more than “making the model read less.”
The second is cache-hit pricing. When a request prefix exactly matches a previous request, the server can reuse the already-computed intermediate state, and those tokens are billed separately at a price often only a fraction of the normal input price. Stable content such as system prompts, tool definitions, and repository structure notes is the best candidate for caching.
The third is time-of-use pricing. Some platforms divide peak and off-peak periods by UTC time, with peak prices reaching roughly twice the off-peak price, and peak windows often overlapping with daytime in Europe and the Americas and working hours in Asia. Weekends and public holidays are usually billed at off-peak rates all day. This dimension turns “cost” from a purely technical problem into a scheduling problem.
3. How to Estimate the Cost of a Typical Coding Session
Rather than arguing about unit prices, build your own estimation model. Let a session’s new input tokens be Tin, cache-hit tokens be Tcache, and output tokens be Tout, with corresponding unit prices Pin, Pcache, and Pout. The cost of a single session is:
Cost = Tin * Pin + Tcache * Pcache + Tout * Pout
Take a medium-sized code refactoring session as an example: about 50,000 new input tokens, about 200,000 cache-hit tokens, and about 30,000 output tokens. During off-peak hours, the cost is roughly within one Chinese yuan; during peak hours, since both input and output unit prices double, the cost is about twice the off-peak cost; and if you estimate with the earlier “quarter price” mental model, the error can be several times off.
The conclusion is straightforward: any cross-period cost review must re-fetch current prices, and historical unit prices are only valid for historical bills. Make unit prices configurable, read from a configuration center or environment variables, rather than scattered across code and documents.
# Example of externalized pricing (use official pricing pages for values; never hardcode)PRICING = {"off_peak": {"input": 0.0, "cache_hit": 0.0, "output": 0.0},"peak": {"input": 0.0, "cache_hit": 0.0, "output": 0.0},}def estimate_cost(tin, tcache, tout, is_peak):p = PRICING["peak" if is_peak else "off_peak"]return tin * p["input"] + tcache * p["cache_hit"] + tout * p["output"]
4. Four Common Pitfalls and Their Fixes
Pitfall 1: Budgeting with old unit prices. Promotional prices, permanent price cuts, and time-of-use prices may coexist at different stages of the same year, so cross-stage comparison is meaningless. Record each price change as a versioned entry and match by time range when reconciling bills.
Pitfall 2: Ignoring output cost. The output unit price is several times the input price, and lengthy explanations, repeated code echoes, and unnecessary summaries all amplify spending linearly. In engineering terms, constrain output explicitly: require only the changed snippets and necessary notes, forbid full-file echoes, and turn large generations into structured patches.
Pitfall 3: Resending context every turn. If every request carries the full system prompt, tool list, and repository notes, the cache hit rate will be low. Stable content should stay fixed at the request prefix in a consistent order, with dynamic content appended after; once the prefix changes, the cache is invalidated.
Pitfall 4: Mixing heavy and light work on one model. Tasks such as completion, renaming, formatting, and writing test skeletons have limited demands on reasoning depth, so a smaller, faster model tier is enough; switch back to a more capable tier for architecture trade-offs, complex defect localization, and cross-module refactoring. Tiered routing typically yields several-fold cost differences.
5. Engineering Implementation: Routing, Caching, and Budget Guardrails
A practical cost governance plan has four layers.
The access layer provides a unified abstraction that wraps different capability tiers behind the same interface, so the business side only declares a task level and the routing layer decides the actual model, making it easy to switch models without changing business code.
The context layer handles prefix stability and trimming. Fix system instructions, coding standards, and directory structure in the prefix; apply a sliding window or summary compression to conversation history; truncate retrieval context by relevance to avoid unbounded growth.
The scheduling layer takes advantage of time-of-use pricing by placing batch tasks (large-scale refactoring, bulk commenting, regression test generation, documentation completion) in off-peak hours and weekends; keep interactive tasks real-time, but control length through output constraints.
The guardrail layer establishes budgets and observability: tag and report token usage and cost by project, environment, and task type; set daily/monthly budget thresholds and alerts; and enforce hard limits on per-request token caps, concurrency caps, timeouts, and retry counts to prevent abnormal loops from blowing up the bill.
Request -> Task classification -> Routing (light/heavy) -> Prefix assembly (stable first)-> Budget check -> Call -> Usage tracking (tags: project/task/time window) -> Alert
6. Common Practice for Connecting Coding Assistants
Mainstream coding assistants usually support two interface styles: a general Chat Completions style and a Messages style. If the tool supports a custom service address, connecting usually requires only three settings—the protocol base URL, the model name, and an access token—without changing local plugin logic. Two additional engineering practices are recommended: configure different model names for different task levels to enable automatic fallback, and keep tokens in a secret manager or environment variables rather than repository config files to avoid leaks.
7. If You Use Baidu AI Cloud Capabilities
When implementing the above on Baidu AI Cloud, you can compose a complete pipeline from general product categories: use a container platform to host routing and gateway services, object storage to hold prompt templates and evaluation sets, a message queue to smooth peaks and fill valleys for time-of-use scheduling, logging and monitoring with alerting to build token and cost observability, and a key management service to hold access credentials. Keep the model capability itself behind an interface abstraction so business code stays decoupled from specific model tiers.
8. Conclusion: Turn Price Volatility into a Controllable Variable
The shift from discounts to long-term and time-of-use pricing is an inevitable result of maturing compute supply-demand and business models. For developers, complaining about unit prices has limited value; what you can do is break cost into engineering variables you can optimize: control output length, stabilize cacheable prefixes, select models by task difficulty, move batch workloads to off-peak hours, and build budget guardrails and a usage dashboard. Do these five things, and even if unit prices change again, the bill stays predictable, explainable, and optimizable.
