LLM Inference Infrastructure: A Guide to Cost Planning and Troubleshooting
Deploying a large language model requires more than trained weights. This guide explains how to estimate recurring inference costs, define latency and availability targets, and plan GPU memory and autoscaling capacity. It includes worked calculations, monitoring checklists, and example alert thresholds to help teams identify production bottlenecks.
Once a large language model has been trained, its capabilities are established—but users experience the performance of the inference service. Training is largely an upfront investment, while inference costs accumulate with every request. Many teams discover only after deployment that responses are too slow or operating costs are far higher than expected.
This guide covers inference cost estimation, service-level targets, and infrastructure constraints, with practical calculations and checks for production planning and troubleshooting.
1. Cost Estimation: Upfront Investment vs. Recurring Consumption
Training costs primarily come from compute clusters, data cleaning, and experimental iterations. If the released model is not retrained, those costs can be amortized over its useful life. If the model requires ongoing fine-tuning or training on newly collected data, training remains a recurring expense and should be budgeted separately.
Inference costs grow with request volume. Every request and every generated token consumes GPU resources. A basic monthly estimate is:
Monthly inference cost ≈ Operating days × Average daily requests × Cost per request
The per-request calculation should distinguish between input tokens that hit the prefix cache and those that do not. Cached input tokens require less computation:
Cost per request =[Average input tokens × (1 − h) × a+ Average input tokens × h × c+ Average output tokens × b] / 1,000
Where:
ais the uncached input price per 1,000 tokens.cis the cached input price per 1,000 tokens.bis the output price per 1,000 tokens.his the prefix-cache hit rate, measured as the proportion of input tokens served from cache.
Track input and output tokens separately because their computational requirements and unit prices usually differ.
Compute cost per token can be estimated as:
Compute cost per token =GPU hourly price / Effective throughput in tokens per hour
Use throughput measured in load tests, not a vendor’s theoretical peak. Batching can increase throughput and reduce unit costs, but batch settings remain constrained by latency targets: larger batches or longer batching windows can increase user wait times.
Worked Cost Example
The following figures are hypothetical and illustrate the calculation method. All prices are in CNY; replace them with your own billing and load-test data.
Assume:
- GPU price: CNY 30 per hour.
- Effective throughput measured in load tests: 1 million tokens per hour.
- Compute cost: CNY 0.03 per 1,000 tokens, used as the uncached input rate,
a. - Output rate,
b: CNY 0.06 per 1,000 tokens, reflecting the longer GPU time required for token-by-token generation. - Cached input rate,
c: CNY 0.01 per 1,000 tokens. - Average daily requests: 50,000.
- Average input length: 800 tokens.
- Average output length: 300 tokens.
- Prefix-cache hit rate,
h: 50%.
The calculation is:
- Uncached input: 400 tokens × 0.03 / 1,000 = CNY 0.012.
- Cached input: 400 tokens × 0.01 / 1,000 = CNY 0.004.
- Output: 300 tokens × 0.06 / 1,000 = CNY 0.018.
- Total cost per request: CNY 0.034.
- Daily cost: 50,000 × 0.034 = CNY 1,700.
- Monthly cost over 30 days: approximately CNY 51,000.
Without prefix caching (h = 0), daily cost would be:
50,000 × (800 × 0.03 + 300 × 0.06) / 1,000= CNY 2,100
In this example, caching saves CNY 400 per day, or approximately 19%.
Plan for Peak Traffic, Not Just Daily Averages
At 50,000 requests per day, the average request rate is:
50,000 / 86,400 ≈ 0.58 requests per second
Assuming peak traffic is three times the average, the peak rate is approximately 1.7 requests per second.
A rough concurrency estimate is:
Peak concurrent requests ≈ Peak requests per second × Average response time
With an average response time of eight seconds, peak concurrency is approximately 14 requests. Determine both the peak multiplier and response time from actual traffic patterns and load tests.
Cost and Capacity Planning Worksheet
| Field | Description | Data source |
|---|---|---|
| Average daily requests | Track separately by endpoint and model version | Gateway or application logs |
| Peak requests per second | Highest request rate measured in hourly or minute-level windows | Gateway monitoring |
| Peak concurrent requests | Peak requests per second multiplied by average response time | Inference framework metrics |
| Average input tokens | Include system prompts and conversation history | Inference framework metering or sampled logs |
| Average output tokens | Actual generated length | Inference framework metering |
| Prefix-cache hit rate | Proportion of input tokens served from cache | Inference framework cache metrics |
| Compute cost per token | GPU hourly price divided by effective hourly throughput | Cloud bills and load-test results |
| GPU memory usage | Combined memory used by weights and the KV cache | GPU monitoring |
| Batching settings | Maximum batch size and batching wait window | Inference service configuration |
Protect User Data When Collecting Metrics
Retain only the fields needed for analysis, such as request length, duration, and status code. Do not store complete prompts or user-facing outputs in routine metrics logs.
If content sampling is necessary, redact sensitive information first, restrict access, and maintain access audit logs. Define a retention period and delete logs automatically when it expires.
Share only aggregated data in planning worksheets. Do not copy raw logs into spreadsheets or shared documents.
2. SLAs and User Experience: Latency and Errors Both Matter
Two metrics strongly shape the user experience:
- Time to first token (TTFT): The time from submitting a request to receiving the first token. It influences whether users are willing to wait.
- Generation speed: The output rate, usually measured in tokens per second. It affects how long the full response takes to arrive.
Timeouts and errors also directly affect user retention.
Averages can hide long-tail latency. Most requests may return quickly while a smaller group waits more than ten seconds, yet the overall average can still appear healthy. Monitor P95 or P99 latency, segmented by time period, endpoint, and model version.
Example SLA Targets and Response Actions
The following targets are illustrative. Set actual thresholds using your application requirements and load-test results.
| Metric | Definition | Example target | Alert threshold | Mitigation |
|---|---|---|---|---|
| Time to first token | Time from request submission to the first returned token | P95 ≤ 1.5 seconds | P95 exceeds 2 seconds for 5 consecutive minutes | Switch to a smaller model or limit input length |
| Generation speed | Output tokens generated per second | P50 ≥ 30 tokens/second | P50 stays below 20 tokens/second for 5 consecutive minutes | Reduce batch size or add replicas |
| Timeout rate | Percentage of requests exceeding the server-side timeout | ≤ 0.5% | Above 1% for 3 consecutive 5-minute windows | Return a queueing notice and pause low-priority requests |
| Error rate | Percentage of responses with a 5xx status | ≤ 0.1% | Above 0.5% for 3 consecutive 5-minute windows | Isolate unhealthy replicas with a circuit breaker and switch to a standby cluster |
| Availability | Monthly service uptime | ≥ 99.9% | A single outage lasts longer than 10 minutes | Activate the incident response plan and notify the on-call team |
Collect metrics through gateway or inference framework instrumentation. Calculate request-level measurements first, then aggregate them.
Five-minute reporting windows are a useful starting point. For window-based alerts, require repeated threshold breaches where appropriate to avoid false alarms caused by brief fluctuations.
3. Technical Constraints: GPU Memory and Autoscaling
Account for Weights, KV Cache, and Runtime Overhead
Inference GPU memory usage has three components:
- Model weights.
- KV cache.
- Runtime overhead.
Weight memory can be estimated as:
Weight memory ≈ Parameter count × Bytes per parameter
For a 7-billion-parameter model:
- FP16: approximately 14 GB, calculated as 7 × 10⁹ × 2 bytes.
- INT8: approximately 7 GB after quantization.
KV cache usage grows linearly with concurrent request count and context length. Long contexts combined with high concurrency often exhaust GPU memory before compute capacity is fully used.
Plan maximum context length, maximum batch size, and GPU memory limits together, leaving headroom rather than allocating all available memory.
Scale on Queueing and Latency, Not GPU Utilization Alone
Prioritize queue wait time and TTFT as scale-out signals instead of relying only on GPU utilization. By the time utilization is high, requests may already be backing up.
Cold starts can take several minutes because new instances must pull container images and load model weights. Maintain a minimum replica count or use prewarmed instances to account for that delay.
Configure scaling cooldown periods to prevent frequent scale-up and scale-down oscillation.
| Constraint | Impact | Planning action |
|---|---|---|
| GPU memory limit | Excessive concurrency or context length can cause out-of-memory errors | Set maximum context length and concurrency, and reserve memory for the KV cache |
