Self-Hosting LLM Inference: A Practical Path from Quota Limits to Cost Control

    This article explains how quota limits shape what a business can build on LLM APIs. It walks through a usage and cost model for comparing pay-as-you-go, subscription, and self-hosted inference. It also covers the main technical factors in self-hosting: VRAM planning, throughput techniques such as quantization and dynamic batching, and service packaging with rate limiting.

    1. The Starting Point: Quota Limits Determine What Your Business Can Do

    Many developers have run into this situation. Free or low-cost quotas are enough during the early stage of a project. Once the business grows, the quota is no longer enough. The cost, validity period, and scope of each pricing tier are not always clear. The quota problem is essentially a cost problem. It determines how many requests can be handled per unit of time, how much context a single task can consume, and whether features can launch and run stably.

    To understand this, break “quota” into three measurable metrics:

    1. Request volume and concurrency: how many requests can be accepted per unit of time.
    2. Token throughput: how many input and output tokens can be processed per unit of time.
    3. Per-request latency: the time from when a user sends a request to when they receive the complete result.

    These three metrics constrain each other. Higher concurrency usually increases latency per request. Longer context lengths reduce overall throughput. Therefore, when discussing quotas, first identify which metric the business values most.

    2. Do the Math First: Build a Usage and Cost Model

    Before deciding whether to self-host, estimate your actual usage and compare the costs of different options. You can estimate the monthly cost of pay-as-you-go calls with the following formula:

    Monthly cost = monthly requests × average input tokens × input price + monthly requests × average output tokens × output price

    Self-hosting costs include hardware depreciation or rental fees, electricity or data center fees, operations staff, and business losses caused by failures. The example below uses hypothetical prices and fees to demonstrate the calculation method. Use the official pricing and your real bills for the service you use when working with actual figures.

    1. from dataclasses import dataclass
    2. @dataclass
    3. class UsageProfile:
    4. requests_per_day: int # Average daily requests
    5. avg_input_tokens: int # Average input tokens per request
    6. avg_output_tokens: int # Average output tokens per request
    7. def monthly_tokens(profile: UsageProfile, days: int = 30) -> int:
    8. per_request = profile.avg_input_tokens + profile.avg_output_tokens
    9. return profile.requests_per_day * days * per_request
    10. def api_monthly_cost(profile: UsageProfile, price_per_million_tokens: float) -> float:
    11. # Assume input and output use the same price for simplicity
    12. return monthly_tokens(profile) / 1_000_000 * price_per_million_tokens
    13. def self_host_monthly_cost(hardware: float, ops: float, power: float) -> float:
    14. return hardware + ops + power
    15. def break_even_tokens(self_host_cost: float, price_per_million_tokens: float) -> float:
    16. """Monthly token volume at which self-hosting cost equals pay-as-you-go cost."""
    17. return self_host_cost / price_per_million_tokens * 1_000_000
    18. if __name__ == "__main__":
    19. profile = UsageProfile(requests_per_day=5000, avg_input_tokens=800, avg_output_tokens=400)
    20. price = 10.0 # Hypothetical value, 10 units per million tokens
    21. self_cost = self_host_monthly_cost(hardware=6000, ops=2000, power=500)
    22. print("Total monthly tokens:", monthly_tokens(profile))
    23. print("Pay-as-you-go monthly cost:", round(api_monthly_cost(profile, price), 2))
    24. print("Self-hosted monthly cost:", self_cost)
    25. print("Break-even monthly tokens:", round(break_even_tokens(self_cost, price)))

    Under the assumptions above, the total monthly token volume is about 180 million, and pay-as-you-go cost is lower than self-hosting cost. The break-even point is about 850 million tokens per month. This result shows that self-hosting only pays off when usage is high enough and hardware utilization stays stable. For most early-stage projects, pay-as-you-go calls are usually cheaper.

    3. Three Ways to Get Compute Capacity

    Dimension Pay-as-you-go Subscription or Quota Self-Hosted Inference
    Upfront cost Low Low High
    Cost predictability Varies with usage Depends on plan terms Mostly fixed spending
    Control Low Low High
    Operations burden Small Small Large
    Data retention Depends on service terms Depends on service terms Fully controllable
    Suitable stage Prototypes and small to medium scale Teams with stable usage High throughput, strong privacy, or custom needs

    The right choice depends on your usage curve and business constraints. It should not depend on the short-term reaction to a single quota change.

    4. Key Stages of a Self-Hosted Inference Service

    4.1 VRAM Determines the Model Size You Can Deploy

    You can roughly estimate the VRAM that model weights require as: parameter count × bytes per parameter. For a 7-billion-parameter model, FP16 uses 2 bytes per parameter, so the weights take about 14 GB. INT8 takes about 7 GB, and INT4 takes about 3.5 GB.

    Actual deployment must also reserve space for the KV cache, activations, and runtime overhead. Higher concurrency and longer context increase KV cache usage. Therefore, hardware selection must consider model size, target concurrency, and maximum context length together.

    4.2 Common Methods for Improving Throughput

    • Quantization: lower weight precision to reduce VRAM usage and improve speed. Before launch, use your business evaluation set to confirm that the quality loss is acceptable.
    • Dynamic batching: combine multiple requests into one computation to improve hardware utilization. The trade-off is that latency for individual requests may increase, so balance throughput against latency.
    • Limiting maximum context length: prevent a few very long requests from filling the cache and affecting other requests.
    • Prefix caching: reuse previously computed results for repeated system prompts to reduce redundant computation.

    4.3 Service Packaging

    Run the inference process in a container, and configure health checks and automatic restarts to keep the service available. Standardize the external interface on a common HTTP chat API format. This way, when clients switch backends, they only need to change the address and authentication information. They do not need to modify business logic.

    Below is a simple token bucket rate limiter. You can use it to control the request rate for a single client or a single project:

    1. import threading
    2. import time
    3. class TokenBucket:
    4. def __init__(self, rate_per_sec: float, capacity: int):
    5. self.rate = rate_per_sec # Tokens added per second
    6. self.capacity = capacity # Maximum bucket capacity, i.e., the number of burst requests allowed
    7. self.tokens = float(capacity)
    8. self.updated = time.monotonic()
    9. self.lock = threading.Lock()
    10. def allow(self) -> bool:
    11. with self.lock:
    12. now = time.monotonic()
    13. elapsed = now - self.updated
    14. self.tokens = min(self.capacity, self.tokens + elapsed * self.rate)
    15. self.updated = now
    16. if self.tokens >= 1:
    17. self.tokens -= 1
    18. return True
    19. return False
    20. limiter = TokenBucket(rate_per_sec=5, capacity=10)
    21. for i in range