LLM Inference Cost Reduction in Practice: Scheduling, Quantization, and Caching

    This article explains how to cut compute costs for LLM inference in self-hosted or hybrid deployments. It covers unit cost calculation, Kubernetes HPA autoscaling based on GPU metrics, testing quantized models before rollout, and KV cache and prefix caching. It also explains where semantic caching has limits.

    Cheap LLM API pricing can make self-hosting look like a minor expense, but enterprises pay for the compute bill of self-hosted or hybrid deployments. That bill depends on GPU utilization, model precision, and cache hit rate. This article covers only compute cost control in the LLM inference stage. It gives practical steps in four areas: cost accounting, elastic scaling, model quantization, and cache reuse.

    1. Calculate the Unit Cost First

    You need to know where the money goes before you can reduce it. You can estimate the unit inference cost with this formula:

    Unit cost ≈ hourly price per GPU ÷ effective output tokens per hour

    The word “effective” matters. Idle GPUs, timeout retries, and long contexts that fill GPU memory and reduce concurrency all shrink the denominator, which raises the unit cost.

    There are three common ways to obtain compute resources. Each suits different workloads:

    Option Advantages Risks Suitable Workloads
    On-demand instances Flexible, no commitment Highest unit price Highly variable traffic, short-term experiments
    Reserved instances Lower unit price Requires a commitment period, and idle capacity is still billed Services with stable baseline traffic
    Spot instances Lowest unit price May be reclaimed by the provider Interruptible offline batch jobs

    For current prices, refer to the latest price list from your cloud provider. A common approach is to cover baseline traffic with reserved instances, handle peak traffic with on-demand instances, and run offline jobs on spot instances.

    2. Scale with HPA Based on GPU Metrics

    In Kubernetes, you can autoscale based on GPU-related metrics. Do not apply the configuration below as is. First, make sure these prerequisites are met:

    1. The cluster must support autoscaling/v2, which generally means Kubernetes 1.23 or later.
    2. Deploy a GPU metrics exporter such as DCGM Exporter, and have Prometheus scrape it.
    3. Pods-type custom metrics must be served through an implementation of the custom.metrics.k8s.io API, such as Prometheus Adapter, before the HPA can read them.
    4. The adapter needs the appropriate RBAC permissions. Otherwise, the HPA will report that it cannot retrieve the metric.

    After the prerequisites are met, use this configuration:

    1. apiVersion: autoscaling/v2
    2. kind: HorizontalPodAutoscaler
    3. metadata:
    4. name: llm-inference-hpa
    5. spec:
    6. scaleTargetRef:
    7. apiVersion: apps/v1
    8. kind: Deployment
    9. name: llm-inference-service
    10. minReplicas: 2
    11. maxReplicas: 10
    12. metrics:
    13. - type: Pods
    14. pods:
    15. metric:
    16. name: gpu_memory_usage_percentage
    17. target:
    18. type: AverageValue
    19. averageValue: "80"

    Watch out for one common mistake. Pods-type metrics accept only the AverageValue target type. Utilization applies only to Resource-type metrics. If you write Utilization under a Pods metric, the HPA will fail validation.

    Two more points need attention. First, loading model weights takes tens of seconds to several minutes, so scale-out has latency. Set a reasonable scale-up stabilization window in the behavior field, and make scale-down more conservative to avoid frequent oscillation. Second, setting minReplicas to 2 means you keep paying for two GPUs even when there is no traffic. If your service can tolerate cold-start latency, you can evaluate scaling to zero. This approach requires an additional event-driven component, and you must account for requests lost during cold starts.

    3. Test Quantization Accuracy Before Rollout

    INT8 quantization usually reduces GPU memory usage with a small loss in accuracy. INT4 offers a higher compression ratio, but it carries greater accuracy risk. The actual loss depends on the quantization method, model architecture, and task type, so do not switch precision based on experience alone.

    Follow these steps:

    1. Sample a calibration set from real production requests. Cover your main business types, short and long texts, and multilingual samples. If the calibration set uses only general-purpose corpora, the quantized model may degrade noticeably on your business tasks.
    2. Build an evaluation set. Record task metrics, such as classification accuracy and factual question-answering correctness, along with efficiency metrics, such as time to first token, throughput, and GPU memory usage.
    3. Compare the original and quantized models. Proceed to rollout only when the business metrics are acceptable.
    4. Design a fallback mechanism. For high-risk requests, such as finance, medical, or legal questions, you can keep a high-precision model and route low-risk requests to the quantized model.

    Quantization savings are real, but they count only after accuracy meets your target.

    4. KV Cache and Prefix Reuse

    PagedAttention manages the KV cache in pages. This reduces GPU memory fragmentation and lets one GPU hold more concurrent requests. Most inference frameworks enable this by default. When you deploy, focus mainly on these two settings:

    • Maximum context length. A value that is too high makes each request use more GPU memory, which reduces concurrency.
    • Maximum concurrency and GPU memory utilization. Use load testing to find the balance between throughput and latency.

    Prefix caching suits workloads where many requests share the same system prompt or knowledge base prefix. On a cache hit, the repeated prefix does not need to be computed again, which directly reduces compute. The benefit depends on the hit rate, so measure the actual hit rate with monitoring before you invest in changes.

    5. The Limits of Semantic Caching

    Some teams cache answers to identical or highly similar questions and return the stored results directly. This works well for FAQ-style scenarios, but three points need attention. Answers can go stale, so set an expiration time. In multi-tenant environments, isolate cache entries by tenant to prevent data from being shared across tenants. If cached content contains users’ personal information, apply data minimization and follow the data protection requirements of the relevant region.

    6. Pre-Launch Checklist

    • Confirm that GPU metrics can be queried in Prometheus.
    • Confirm that the Custom Metrics API returns correct values. You can verify this with kubectl get --raw "/apis/custom.metrics.k8s.io/v1beta1".
    • Check the HPA describe output for metric read errors.
    • Confirm that the quantized model passed the business evaluation set and that a fallback path exists.
    • Confirm that each inference service has unit cost monitoring configured.

    Cost reduction starts with measurement and then optimization. Track unit cost, scaling, quantization accuracy, and cache hit rate on one dashboard. Only then can you tell whether each optimization actually saves money.