Building Multi-Region LLM Failover Gateways: Hedged Requests, Regional Quota Routing, and Sub-Second Circuit Breakers
Why Naive LLM Retries Break Under Regional Load
If your AI product serves users across US-West, US-East, and APAC, relying on a single inference region or a naive tenacity exponential backoff loop is a recipe for p99 latency spikes > 25 seconds.
When a regional cluster hits 429 Too Many Requests or degrades on TTFT (Time-to-First-Token), retrying the same region 3 times only amplifies the thundering herd.
Three Patterns for Resilient Multi-Region Inference
- TTFT-Based Hedged Requests (Tail-At-Scale): If the primary region hasn't emitted its first SSE token within
650ms(for interactive chat), fire a secondary request to a warm backup region and cancel whichever loses the race. - Sliding-Window Provider Circuit Breakers: Trip a region into
HALF-OPENstate for 30 seconds the moment its 10-second error rate (429/503/timeout) crosses8%. - Header-Driven Quota Steering: Parse
x-ratelimit-remaining-tokensfrom every response into a shared Redis/in-memory score so traffic drains smoothly before hitting hard429walls.
Async Python Hedged Router + Circuit Breaker
pythonimport asyncio import time from dataclasses import dataclass, field @dataclass class RegionEndpoint: name: str base_url: str open_until: float = 0.0 failures: int = 0 total: int = 0 def is_healthy(self) -> bool: return time.monotonic() >= self.open_until def record(self, ok: bool) -> None: self.total += 1 if not ok: self.failures += 1 if self.total >= 20 and (self.failures / self.total) > 0.08: self.open_until = time.monotonic() + 30.0 self.failures = 0 self.total = 0 async def call_with_hedge(primary: RegionEndpoint, backup: RegionEndpoint, call_fn, hedge_ms: float = 0.65): if not primary.is_healthy(): return await call_fn(backup) primary_task = asyncio.create_task(call_fn(primary)) done, _ = await asyncio.wait({primary_task}, timeout=hedge_ms) if primary_task in done: try: res = primary_task.result() primary.record(True) return res except Exception: primary.record(False) return await call_fn(backup) # Primary missed TTFT hedge budget — race against backup backup_task = asyncio.create_task(call_fn(backup)) finished, pending = await asyncio.wait( {primary_task, backup_task}, return_when=asyncio.FIRST_COMPLETED ) for task in pending: task.cancel() winner = next(iter(finished)) return winner.result()
Production Takeaway
Keep hedging disabled for high-cost batch jobs (where 2x token spend on overlap hurts margins) and enable it strictly for interactive user-facing tool calls and chat, where cutting p99 latency from 12s to 1.4s directly protects retention.