How to Calculate the Cost per Token for Self-Hosted Large Language Models
A practical guide to calculating the per-token cost of self-hosted LLM inference. It explains how to account for hardware, power, operations, software, redundancy, throughput, utilization, and input/output pricing, then compares the result with usage-based API costs using break-even analysis.
1. Why Measure Self-Hosted Deployment by Cost per Token
The key question in a self-hosted deployment is whether the cost per unit of output can be lower than that of a usage-based service after the team takes on hardware and operational responsibilities.
The right basis for comparison is the unit cost—not the purchase price of a server or its monthly lease fee. Monthly infrastructure cost represents only the total expense. Cost per token is what allows a direct comparison with usage-based pricing.
2. What Makes Up the Cost?
1. Hardware
Purchased hardware is a one-time expense and should be amortized over its useful life, commonly three to five years, and allocated to each month. Leased hardware is charged by the month or by a specific usage period. These two models use different accounting methods, so both should be converted to a consistent monthly cost before comparison.
2. Power and Data Center Costs
Accelerators typically consume significant power. Electricity and cooling costs also vary with system load. Estimates should use actual power consumption multiplied by operating time and should include colocation or data center fees where applicable.
3. Operations Staff
Deployment, monitoring, version upgrades, and incident handling all require engineering resources. These costs should be converted into a monthly amount based on engineer-days or equivalent staffing costs.
4. Software and Redundancy
Inference services, monitoring, alerting, and other infrastructure components have ongoing maintenance costs. Production environments also commonly require standby or redundant nodes. Redundancy increases total cost and can reduce the effective service time available for serving requests.
3. Throughput Determines Unit Cost
Large-model inference involves two metrics that are easy to confuse.
The first is per-request speed, measured in tokens per second. This is the generation speed experienced by an individual user.
The second is total system throughput, measured as the total number of output tokens generated by the server per second. Online inference services often use scheduling techniques such as continuous batching to process multiple requests together. As concurrency increases, the speed of each individual request may decrease while total system throughput rises.
For unit-cost calculations, use the total throughput under stable operating conditions rather than the peak speed of a single request. Estimating with per-request speed can significantly understate costs. Using the theoretical maximum system throughput can overstate actual capacity.
A practical approach is to run load tests and record total throughput and time to first token at different concurrency levels. Then select the concurrency range that meets the target latency requirements and use its measured throughput as the basis for the calculation.
4. Formula for Calculating Cost per Token
The basic formula is:
Cost per unit (CNY per million tokens)= Total monthly cost (CNY) ÷ Effective monthly output (million tokens)
Effective monthly output is calculated as:
Effective monthly output= Average total throughput (tokens/s)× Seconds per month× Average utilization
Using a 30-day month:
Seconds per month = 2,592,000
The following example uses hypothetical figures for illustration only.
Assume:
- Total monthly cost: CNY 300,000
- Average total throughput: 600 tokens/s
Full utilization: 100%
Monthly output is approximately 1.555 billion tokens, or 1,555 million tokens.
Cost per million tokens= 300,000 ÷ 1,555≈ CNY 193
Average utilization: 60%
Monthly output is approximately 933 million tokens, or 933 million tokens.
Cost per million tokens= 300,000 ÷ 933≈ CNY 321
With the same hardware, reducing utilization from 100% to 60% increases the unit cost by approximately 66%. Utilization is one of the most frequently overlooked variables, yet it has one of the greatest effects on the result.
5. Break-Even Analysis Against Usage-Based Pricing
Let:
P= usage-based price in CNY per million tokensC= monthly self-hosting cost in CNY
The break-even monthly usage is:
Break-even monthly usage (million tokens)= C ÷ P
For example, if the monthly self-hosting cost is CNY 300,000 and the usage-based price is CNY 100 per million tokens:
Break-even monthly usage= 300,000 ÷ 100= 3,000 million tokens
That equals 3 billion tokens per month.
If actual monthly usage is below this level, usage-based inference is generally more economical. If usage exceeds it, self-hosting may provide a cost advantage.
Many services price input and output tokens separately, and prices may vary by plan. The calculation should therefore track input and output usage independently and use the prices that actually apply to the selected pricing plan.
6. Common Mistakes
1. Incorrect Unit Conversion
One hundred million tokens equals 100 million-token units, not 10,000 million-token units. For example, 2 billion tokens should be converted to 2,000 million tokens.
A unit conversion error can change the result by a factor of 100 and is one of the most common calculation mistakes.
2. Counting Only Output Tokens
Input tokens also consume compute resources and memory. If the pricing model charges for input, input and output usage must both be included in the calculation.
3. Assuming Full Utilization
Real-world traffic usually has peaks and valleys. Assuming 100% utilization will underestimate the unit cost. Use the average utilization derived from historical traffic instead.
4. Ignoring Redundancy and Downtime
Standby nodes, maintenance windows, and recovery time after failures all reduce the effective service time. These factors should be deducted when calculating the available production capacity.
5. Ignoring Depreciation and Technology Refresh Cycles
The residual value of purchased equipment is affected by the pace of technology changes. Setting the depreciation period too long will underestimate monthly costs, while setting it too short will overestimate them.
7. Example Calculation Code
The following language-independent example illustrates the calculation steps:
monthly_cost = 300000 # Total monthly cost in CNYthroughput = 600 # Average total throughput in tokens/sutilization = 0.6 # Average utilizationseconds = 30 * 24 * 3600 # Seconds per monthtokens_m = throughput * seconds * utilization / 1000000# Monthly output in million tokensunit_cost = monthly_cost / tokens_m# Cost per million tokens in CNYOutput: monthly output tokens_m million tokens,unit cost unit_cost CNY per million tokens
For an actual estimate, replace throughput and utilization with load-test and monitoring data. The monthly cost should also be broken down into hardware depreciation or leasing, power, operations, software, and redundancy.
8. Conclusion and Selection Guidance
Self-hosting provides greater control over data and allows deeper customization. However, its unit cost is likely to be lower than usage-based inference only when demand is stable, utilization is relatively high, and the team has the operational capability to run the infrastructure.
A practical evaluation process is:
- Use real business traffic patterns to run load tests and measure total throughput at different concurrency levels.
- Calculate historical average utilization.
- Apply the measured throughput and utilization to the unit-cost formula.
- Compare the result with usage-based pricing item by item.
- Verify all units and pricing rules before drawing a conclusion.
