Cost Accounting and Quota Design Practices for LLM Inference Services

    This article explores the engineering principles behind cost estimation, deployment choices, and quota design to build a sustainable LLM inference service. It covers unifying cost metrics to cost per million tokens, selecting appropriate deployment strategies, applying general inference optimization techniques like continuous batching and KV cache management, and implementing tiered rate limiting using token bucket algorithms.

    1. Understanding Cost Structures Through Quota Changes

    Providing different usage quotas for the same service at different stages is essentially a judgment of the relationship between unit compute cost, capacity, and demand. When a quota for a specific user category is increased, it usually indicates one or more of the following: a decrease in unit inference cost, an improvement in resource utilization, or an expansion of available capacity. Understanding these three points is necessary to determine whether a quota adjustment is sustainable and to design a reasonable tiered strategy in your own system.

    This article does not discuss pricing details of any specific service. Instead, it breaks the problem down into general engineering tasks: how to estimate the cost of large model inference, how to choose a deployment plan, how to design quota and rate limiting mechanisms, and how to string these components together into a continuously running service.

    2. Unifying the Metric: Cost Per Million Tokens

    LLM services typically use tokens as the billing and cost accounting unit. The core of cost accounting is converting the hourly cost of hardware into the cost per unit token:

    Unit token cost = Hourly cost per GPU ÷ (Effective throughput tokens/sec × 3600)

    “Effective throughput” is not equivalent to peak compute power. It depends on the following factors:

    • Batching strategy: Handling more requests simultaneously generally means higher throughput per GPU, but the latency for individual requests will also increase.
    • Input and output length ratio: Long contexts occupy more GPU memory, which lowers the concurrency ceiling.
    • Memory headroom: Model weights and KV cache share GPU memory. The cache space determines how many concurrent sessions can be maintained.
    • Utilization: Every hour a GPU sits idle must be factored into the cost.

    Below is an illustrative script for estimating costs. The parameters are hypothetical and are only used to demonstrate the calculation structure:

    1. def cost_per_million_tokens(hourly_cost: float,
    2. throughput_tps: float,
    3. utilization: float) -> float:
    4. """
    5. hourly_cost: Total cost per GPU per hour (hardware depreciation + electricity + data center + O&M)
    6. throughput_tps: Number of output tokens per second at full load (measured value)
    7. utilization: Average utilization rate, ranging from 0 to 1
    8. """
    9. effective_tps = throughput_tps * utilization
    10. tokens_per_hour = effective_tps * 3600
    11. return hourly_cost / tokens_per_hour * 1_000_000
    12. # Example: Hypothetical values, for demonstration only
    13. for u in (0.3, 0.6, 0.9):
    14. cost = cost_per_million_tokens(hourly_cost=20.0,
    15. throughput_tps=800,
    16. utilization=u)
    17. print(f"Utilization {u:.0%}: Cost per million tokens is approximately ${cost:.2f}")

    This code illustrates a fact: with the same hardware and throughput, increasing utilization from 30% to 90% can reduce the unit cost to about one-third of the original. Therefore, cost optimization for most services comes first from queuing, scheduling, batching, and load balancing, rather than simply upgrading hardware.

    3. Choosing a Deployment Plan

    Deployment methods are generally divided into three categories:

    1. Directly calling third-party hosted model APIs. The advantage is no need to maintain hardware; the disadvantages are a fixed unit price, difficulty in deep optimization for specific business needs, and data having to pass through external links.
    2. Self-hosted inference clusters. The advantages are controllable costs, data remaining in your own environment, and freely adjustable inference parameters. The disadvantage is taking full responsibility for hardware procurement, operations, and capacity planning.
    3. Hybrid deployment. Core, stable traffic is routed to self-hosted clusters, while burst traffic or niche models go through hosted APIs. As teams scale up, this is a common choice.

    When selecting hardware, it is recommended to evaluate item by item across the following dimensions:

    • VRAM capacity: Determines the model size that can be loaded and the number of concurrent sessions supported.
    • Memory bandwidth: Greatly affects the speed during the decoding phase, as autoregressive generation is limited by memory bandwidth.
    • Inter-GPU interconnect: During multi-GPU parallelism, communication bandwidth directly dictates the efficiency of tensor parallelism.
    • Software ecosystem: Whether the inference framework, operator libraries, and compilation toolchain are mature directly determines the labor cost of migration and tuning.
    • Supply and delivery cycle: Whether hardware can arrive on time often impacts project progress more than theoretical performance.

    For accelerated hardware using different technological routes, migration costs cannot be judged solely by the purchase price. You also need to evaluate the workload of model conversion, operator compatibility, precision verification, and performance regression testing. It is generally recommended to do a canary test on a small scale of traffic first, and then gradually expand the ratio.

    4. General Inference Optimization Techniques

    Once the hardware is determined, software-level optimization typically yields more direct benefits:

    • Quantization: Converting weights from high precision to low-bit representations reduces memory usage and increases throughput. Precision loss must be validated on target tasks; you cannot look at throughput numbers alone.
    • Continuous batching: Dynamically splicing newly arrived requests into a running batch prevents short requests from waiting for long requests to finish. This is a key mechanism for improving utilization.
    • KV cache management: Using paged cache allocation reduces memory fragmentation and reuses caches for requests with the same prefix.
    • Speculative decoding: Using a small model to quickly generate candidate tokens, which are then verified in parallel by a large model. This is suitable for latency-sensitive scenarios.

    These techniques influence each other. For example, the memory freed up by quantization can be used to expand the cache, thereby supporting more concurrency. During optimization, change only one variable at a time and simultaneously record three metrics: throughput, time to first token (TTFT), and output quality.

    5. Tiered Quotas and Rate Limiting Mechanisms

    When a service needs to provide different quotas to different users, the rate limiter is the core component. The token bucket is a common implementation: each user has a bucket that is replenished with tokens at a fixed rate. Each request consumes a corresponding number of tokens, and if there are not enough tokens in the bucket, the request is rejected or queued.

    1. import time
    2. import threading
    3. class TokenBucket:
    4. def __init__(self, rate_per_sec: float, capacity: int):
    5. self.rate = rate_per_sec # Number of tokens replenished per second
    6. self.capacity = capacity # Maximum bucket capacity
    7. self.tokens = capacity
    8. self.updated = time.monotonic()
    9. self.