Does Self-Hosting LLMs Actually Save Money? Inference Optimization and Cost Calculation
This article explains how to determine whether self-hosting a large language model is more cost-effective than using APIs. It covers a four-point readiness checklist, a practical formula for calculating per-token costs, the impact of GPU utilization and cache hits on the denominator, and inference optimization techniques such as PagedAttention and continuous batching, along with how to validate them on your own traffic.
Run These Four Checks First
Before doing any math, confirm that self-hosting is viable for your situation.
- Traffic volume: Is your daily or peak token throughput stable and large enough to amortize fixed costs?
- Latency targets: Can your business tolerate the time-to-first-token and generation speed of a self-hosted service? Do you need a dedicated low-latency path?
- Ops capability: Does your team have people responsible for GPU drivers, inference server upgrades, incident response, and capacity planning?
- Data compliance: Must your data stay on-premises, and do your audit requirements permit using self-hosted models?
If the answer is “no” to two or more of these, keep using API calls for now and re-evaluate once your traffic and team conditions change.
How to Calculate the Cost
The per-token cost of self-hosting can be written as:
Per-token cost = Total GPU cost for the period (including ops and storage) ÷ Total tokens successfully processed in the period
The variables in the formula are defined below.
| Variable | Meaning | Notes |
|---|---|---|
| GPU cost | Amortized hardware purchase or rental cost | Calculated per accounting period; purchase cost is amortized over the expected lifespan |
| Ops and storage | Facilities, bandwidth, model storage, monitoring systems, and labor | Labor is counted by actual hours invested—machine costs alone are not enough |
| Successful tokens | Total tokens delivered to users in completed responses | Requests that time out, error, or get truncated are excluded |
| Input/output basis | Whether input tokens are included in the count | Must match the API pricing basis exactly |
| Accounting period | The time window over which costs and token counts are measured | Use calendar months and keep the period consistent |
Standardize the accounting basis before you calculate. Some platforms bill for both input and output tokens; others bill only for output. Comparing two numbers measured on different bases is meaningless. The recommended approach is to count input and output tokens separately, price each at its own rate, and then sum them.
The denominator depends on GPU utilization. Assuming throughput scales linearly with utilization, raising a card’s utilization from 30% to 70% lets the same cost be spread across roughly 2.3x as many tokens, cutting per-token cost to about 43% of the original. This is an idealized estimate. In practice, as utilization approaches saturation, queuing delays and memory pressure slow throughput growth, and the gains diminish. Use a measured throughput curve instead of a linear assumption.
Cache hits also affect the value of the denominator. If your workloads contain many repeated system prompts or long shared context prefixes, prefix caching eliminates redundant computation. The billing basis and cost of these requests must be recorded separately, or you will over- or under-estimate the unit price.
How to Optimize Inference—and How to Validate It
Inference engine optimizations fall into three main directions. Every one of them must be validated on your own traffic; you cannot simply reuse someone else’s numbers.
1. Paged KV Cache Management (PagedAttention)
The traditional approach reserves a contiguous block of GPU memory sized for the maximum length of each request, so short requests waste significant space. Paged management splits the KV cache into small blocks allocated on demand, reducing memory fragmentation so the same memory can hold more concurrent requests. The benefit depends on your request length distribution.
Validation steps:
- Export a representative sample of production requests from your logs, including input length, output length, and arrival time.
- On the same machine, start the service twice: once with pre-allocated memory and once with paged allocation. Keep the inference engine version, model weights, and precision identical.
- Replay the requests at their original arrival rate, and record maximum concurrency, memory usage, and request throughput.
- Use “the number of requests supported at the same P95 latency” as the acceptance metric.
2. Continuous Batching
Static batching waits for an entire batch to finish before starting the next one. Short requests complete long before the longest one, leaving the GPU idle. Continuous batching lets new requests join at any decoding step and lets finished requests exit at any time, reducing GPU idle cycles. The greater the variance in input and output lengths, the larger the benefit.
The comparison experiment must be controlled: model, hardware, precision, maximum concurrency, input/output length distribution, and load test duration must all be identical. Only change…
