← Questions

LLM provider pool with failover

0
PerplexityCodingLast asked Aug 2025

A coding interview question asked in Perplexity's technical screen or assessment, last asked Aug 2025. It tests llm, failover and http clients.

Write a client library that sends completion requests to a pool of external LLM providers. Callers make one call; the library picks a provider that serves the requested model, respects each provider's rate limits, and when a provider slows down or fails, moves traffic to another without the caller noticing. It must be safe to call from many threads at once.

Interface

Unless you agree on a different interface with the interviewer, assume:

  • register(provider: Provider, models: dict[str, int], rate_limit_rps: float, cost_per_1k_tokens: float) -> None — adds a provider with the models it serves and their context lengths.
  • Provider.complete(model: str, prompt: str, timeout_s: float) -> str — the provider's own call; it may raise or hang.
  • complete(model: str, prompt: str, deadline_s: float = 30) -> str — routes, retries and falls back within the deadline; raises NoProviderAvailable when every eligible provider is exhausted.

Example

register(A, {"llama-70b": 8192}, rate_limit_rps=10, cost_per_1k_tokens=0.6)
register(B, {"llama-70b": 32768}, rate_limit_rps=5, cost_per_1k_tokens=0.9)

complete("llama-70b", "hi")          -> served by A (cheaper)
A fails 5 times in a row             -> A's circuit opens; calls go to B
30 s later                           -> one trial call to A; success closes the circuit
complete("llama-70b", <20k tokens>)  -> only B fits the context; served by B

Constraints

  • Retries and fallbacks never exceed the caller's deadline.
  • No API key appears in logs or error messages.

Clarifying questions

  • Should routing favor cost, latency, or a mix? Is there a quality ranking between providers?
  • Which errors are retryable (timeouts, 429, 5xx) and which are not (400, content refusals)?
  • Are requests streaming? Fallback after the first streamed token is a different problem.
  • Is it acceptable to send a request to a second provider before the first has timed out (hedging)?
  • What metrics and traces does the team already collect?

What a strong answer covers

  • A provider record with capabilities, a token-bucket rate limiter, and health state; the router filters by model and context length, then picks by cost and recent latency.
  • A circuit breaker per provider (closed, open, half-open) driven by consecutive failures or error rate, and a probe that brings it back.
  • Retries with jittered backoff, a per-attempt timeout under the overall deadline, and fallback to the next provider only for retryable errors.
  • Thread safety: locks or atomic counters around limiter and breaker state, no lock held during a network call.
  • Observability: per-provider latency, errors, breaker transitions, a trace span per attempt; keys loaded from a secret store.
  • Tests with fake providers that fail, hang or rate-limit on cue, and a fake clock for breaker timing.

Follow-ups

  • Add hedged requests for tail latency without doubling cost.
  • Support streaming responses with failover.
  • Enforce a monthly spend quota per provider across many processes.

Where it's asked

  • Perplexity · Technical screen or assessment · Last asked Aug 2025Online assessment or live coding (often CoderPad); problems come in parts, each adding a requirement to your code · Assume no AI unless the recruiter says otherwise

What it tests

llm · failover · http clients

Write and test your solution herePython in your browser, the question's tests level by level, every submission kept for you.Sign in to start coding