KatInterview

Fault-Tolerant Router for Multiple LLM API Vendors

System DesignModel answer
HardModel answerSystem Designcircuit-breakerrate-limitingload-balancing

Last updated

Design a client library that spreads LLM calls across several vendors, enforcing limits, isolating failures, and choosing providers by cost and latency while staying thread-safe and observable.

Your team calls several third-party LLM vendors, and a single vendor outage or throttling event should not take the product down. You are asked to design and sketch the implementation of an in-process client library that sends each request to the best available vendor.

The library must cover:

  • Vendor registration and capabilities: adding and removing vendors at runtime, and mapping each vendor's models to a common model family, including context window size and feature support (tools, streaming) and pricing.
  • Governance: per-vendor and per-key rate limits (requests and tokens per minute) and spend quotas.
  • Resilience: health checks, per-attempt and overall timeouts, and circuit breakers.
  • Routing: load balancing that accounts for cost and latency, plus retry and fallback policies when a vendor is degraded or down.
  • Observability: metrics, logs, and tracing.
  • Security: safe handling of API keys.
  • Concurrency: correct behavior with many threads sharing the library.

State your assumptions about scale (vendors, request rate, token sizes, latency targets, whether streaming is required). Then describe the public interfaces, the core data structures, the lifecycle of a single request, and how each failure mode is handled. Include a code sketch for at least one non-trivial component, such as the circuit breaker or the rate limiter.

Hints

Hint 1

Separate vendor-specific details (request formats, error codes) behind an adapter, so the router only deals with a canonical model family and a small set of error classes: retryable, non-retryable, and auth.

Hint 2

Think about what happens when every instance of your service picks the same 'best' vendor at the same moment. Consider randomized selection among the top candidates rather than a strict minimum score.

Hint 3

Streaming changes retry rules: once any output has been sent to the caller, a transparent retry on another vendor can duplicate or corrupt output. Decide where the retry boundary is.

Hint 4

Rate limits are enforced by the vendor per key, but your fleet has many instances. Compare static per-instance shares, a central Redis bucket, and leasing batches of tokens.