Building Resilient Applications Under LLM API Rate Limits
This article explains why rate limits occur on shared public LLM services and how free interaction quotas differ from paid API channels in terms of stability. It then presents a practical resilience design approach covering client-side and gateway-side rate limiting, semantic caching, tiered model routing, streaming output with queue feedback, fallback strategies, and token bucket-based capacity estimation and load testing. Illustrative code and a pre-launch checklist are included to help developers turn unpredictable rate limiting into a predictable user experience.
When a shared public large model service starts rate-limiting requests, many applications suddenly become “unavailable” during peak hours. This article first explains why rate limiting happens and the fundamental stability differences between free interaction quotas and paid API channels. It then provides a practical resilience design methodology, including client-side and gateway-side rate limiting, semantic caching, tiered model routing, streaming output with queue feedback, fallback strategies, and capacity estimation and load testing based on token buckets. At the end, illustrative code and a launch checklist are provided to help developers turn “uncontrollable rate limiting” into a “predictable experience”.
Why Rate Limiting Happens
Public LLM services are shared infrastructure with finite capacity. When aggregate demand exceeds available throughput, the provider enforces limits to protect overall availability. Understanding this is the first step toward designing around it.
Free Quotas vs. Paid Channels
Free interaction quotas and paid API channels differ fundamentally in stability. Free tiers are typically subject to stricter and more variable limits, while paid channels generally offer more predictable throughput. Applications that depend on free quotas should expect sudden unavailability during traffic spikes.
A Practical Resilience Design Approach
Client-Side and Gateway-Side Rate Limiting
Apply rate limiting on both the client and the gateway. Client-side limiting prevents a single caller from exhausting its own quota, while gateway-side limiting enforces global fairness across all callers.
Semantic Caching
Use semantic caching to serve repeated or similar requests from cache instead of calling the model again, reducing both latency and quota consumption.
Tiered Model Routing
Route requests across model tiers so that lower-cost or higher-availability models absorb traffic when the primary model is constrained.
Streaming Output and Queue Feedback
Use streaming output combined with queue feedback so users receive incremental responses and clear waiting signals rather than a hard failure.
Fallback Strategies
Define fallback strategies that activate when the primary service is rate-limited, preserving core functionality under degraded conditions.
Capacity Estimation and Load Testing with Token Buckets
Estimate capacity using a token bucket model and validate it with load testing before launch.
Illustrative Code
import timeimport threadingclass TokenBucket:def __init__(self, capacity, refill_rate):self.capacity = capacityself.tokens = capacityself.refill_rate = refill_rateself.last = time.time()self.lock = threading.Lock()def consume(self, tokens=1):with self.lock:now = time.time()self.tokens = min(self.capacity,self.tokens + (now - self.last) * self.refill_rate)self.last = nowif self.tokens >= tokens:self.tokens -= tokensreturn Truereturn False# Example: capacity of 10 requests, refilled at 2 requests per secondbucket = TokenBucket(capacity=10, refill_rate=2)def call_llm(request):if not bucket.consume():return "rate limited, please retry later"# Place your model API call herereturn "model response"
Pre-Launch Checklist
- Client-side and gateway-side rate limiting are both configured.
- Semantic caching is enabled for repeated requests.
- Tiered model routing is defined with fallback models.
- Streaming output and queue feedback are implemented.
- Fallback strategies cover core functionality.
- Capacity has been estimated with a token bucket model.
- Load testing has validated behavior under peak traffic.
