Estimating LLM Usage and Costs for Software Development
A practical framework for estimating token usage, cache-related savings, subscription value, time-based pricing, and response latency when using large language models for coding tasks. All figures are illustrative; always refer to the provider’s current pricing rules for actual costs.
When developers use large language models to assist with programming, they often see only a quota percentage and have little visibility into the actual cost of a task. This article presents a reusable estimation method based on usage data. All figures are hypothetical examples for illustration. Actual prices should be based on the provider’s published billing rules.
1. Why Estimate Model Usage for Coding Tasks?
LLM coding tools typically use one of two billing models.
The first is usage-based billing, where you pay according to the number of input and output tokens. The second is a subscription plan that provides a fixed quota for a billing period. Once the quota is exhausted, access may be paused or downgraded. Usage-based bills can be converted directly into costs, while subscription plans often show only a percentage, making the actual cost of an individual task difficult to determine.
When estimating a project budget, developers commonly face three questions:
- How many tokens will a task consume?
- How do caching, time-of-day pricing, and subscription plans affect the final cost?
- How should cost be weighed against execution time?
The following sections address each of these questions.
2. The Three Fundamental Usage Dimensions
Billing generally involves three types of tokens.
Input tokens include system prompts, code context, conversation history, and tool outputs. In a multi-turn conversation, earlier content may be sent again with each request, so input volume can grow rapidly as the conversation becomes longer.
Output tokens include generated code, explanations, and reasoning content. Within the same service, output tokens are usually more expensive than input tokens. As a result, even a relatively small amount of output can represent a significant share of the total cost.
Cache-hit tokens are input tokens whose request prefix matches a recently processed prefix and can therefore reuse results already handled by the service. Cached tokens are usually priced much lower than uncached input tokens. The higher the cache-hit ratio, the lower the effective cost.
3. A Cost Model Based on Usage
Usage-based cost can be expressed with the following formula:
Total cost =Uncached input × input price+ Cached input × cache price+ Output × output price
The cached input amount equals total input multiplied by the cache-hit ratio. Uncached input equals total input minus cached input. Prices are usually quoted per million tokens, so the final result must be divided by 1,000,000.
The following function implements this calculation. All parameters use hypothetical values:
def estimate_cost(input_tokens, output_tokens, cache_hit_ratio,input_price, cache_price, output_price):"""Estimate the cost of one task using prices per million tokens."""cached = input_tokens * cache_hit_ratiouncached = input_tokens - cachedcost = (uncached * input_price+ cached * cache_price+ output_tokens * output_price) / 1_000_000return round(cost, 2)# Hypothetical prices: 10 yuan for input, 1 yuan for cached input,# and 20 yuan for output, per million tokensprint(estimate_cost(input_tokens=20_000_000,output_tokens=200_000,cache_hit_ratio=0.9,input_price=10,cache_price=1,output_price=20,)) # Output: 42.0
The function returns 42.0 yuan. If the cache-hit ratio falls to 50% while all other parameters remain unchanged, the cost rises to 114.0 yuan. This demonstrates that caching strategy can have a greater effect on cost than relatively small price changes.
4. How to Improve the Cache-Hit Ratio
Cache hits depend on keeping the request prefix stable. The following practices can help improve the cache-hit ratio:
- Put stable system prompts and project conventions at the beginning of the request, and avoid changing the opening content unnecessarily.
- Keep context continuous within the same task and avoid inserting unrelated content into the prefix.
- When the conversation history becomes too long or contains excessive irrelevant information, start a new conversation and carry over only a necessary summary.
- For repeated queries against the same codebase, use a consistent context structure whenever possible.
A higher cache-hit ratio is not always better. Retaining large amounts of outdated context in order to improve cache reuse can reduce output quality. The right balance between cost and quality is more important than maximizing cache hits.
5. Converting Subscription Quotas into an Equivalent Cost
Subscription plans often display usage as a percentage. To estimate the equivalent value of a subscription quota:
- Record the quota percentage before and after the task, then calculate the proportion consumed.
- Use the formula from the previous section to calculate the task’s usage-based cost.
- Divide the usage-based cost by the quota proportion consumed to estimate the equivalent value of the full-period quota.
Suppose a task consumes 95% of the quota for a billing period and its usage-based cost is 42 yuan. The equivalent value of the full quota is approximately:
42 ÷ 0.95 ≈ 44.2 yuan
This figure is only an estimate. In practice, quota windows may not be allocated linearly, and consumption rates may vary across different time periods. As a result, the conversion may contain some error. Averaging the results of multiple tasks generally produces a more reliable estimate.
If the subscription is billed monthly, divide the equivalent monthly quota value by the subscription price to calculate the effective discount ratio. This helps determine whether the subscription is more economical than usage-based billing.
6. Time-of-Day Pricing and Cost Fluctuations
Some services adjust prices based on the time of day. The difference between peak and off-peak periods can sometimes be several times the base price. When estimating cost, record when each task runs and apply the appropriate pricing rules for that period.
Practical strategies include:
- Schedule batch generation and document organization tasks during lower-priced periods when they do not require immediate results.
- Run debugging tasks that require instant feedback during peak periods when necessary, while accepting the higher cost.
- Review the provider’s billing documentation regularly because pricing rules may change.
7. Output Speed and Time Cost
Cost is not the only consideration. Waiting time is also part of the overall cost. Output time can be approximated with the following formula:
Output time ≈ Number of output tokens ÷ Output speed in tokens per second
This approximation ignores input processing time and network latency, so it is mainly useful for estimating the order of magnitude.
For example, generating 200,000 output tokens would take approximately:
- At 150 tokens per second: 1,333 seconds, or about 22 minutes.
- At 50 tokens per second: 4,000 seconds, or about 67 minutes.
A lower-priced service does not necessarily save time. Developers should evaluate cost and speed together based on the urgency of the task. For interactive debugging, response speed is often more noticeable than a small difference in unit price.
8. A Complete Estimation Workflow
- Record the task’s start and end times, model type, and usage data.
- Track input, output, and cache-hit tokens separately.
- Calculate usage-based cost using the applicable prices.
- If using a subscription plan, convert the quota percentage into an equivalent cost.
- Adjust the estimate according to time-of-day pricing rules.
- Estimate completion time using the output speed and include time cost in the evaluation.
Keep the following points in mind:
- Multi-turn conversations may resend earlier input repeatedly, so input usage in long sessions can be much higher than expected.
- Tool calls and internal reasoning may generate additional output tokens. Use the provider’s billing records as the authoritative source.
- An estimation model provides only an approximation. Formal budgeting should rely on the platform’s actual billing results.
9. Conclusion
The key to estimating LLM usage is to convert different types of consumption into a common view of cost and time. By understanding how input, output, and cached tokens are calculated—and by incorporating subscription conversion and time-of-day adjustments—developers can make more reliable budget decisions during project planning and identify targeted ways to reduce costs during daily use.
