Mid-level interview questionAsked at Perplexity · interview write-up
Implement a client that spreads LLM calls across several providers, respecting their rate limits and failing over when one is down.
Model answer
Try providers in preference order, and bench one for its retry-after time when it rate-limits or errors.
import random, time
class RateLimited(Exception):
retry_after = None
class Pool:
def __init__(self, providers, cooldown=30.0):
self.ps = providers # in preference order
self.until = {p.name: 0.0 for p in providers}
self.cooldown = cooldown
def call(self, req, rounds=3):
for r in range(rounds):
for p in self.ps:
now = time.monotonic()
if self.until[p.name] > now:
continue
try:
return p.complete(req)
except RateLimited as e:
wait = e.retry_after or self.cooldown
self.until[p.name] = now + wait
except (TimeoutError, ConnectionError):
self.until[p.name] = now + self.cooldown
time.sleep(2 ** r + random.random())
raise RuntimeError("all providers failed")
Interviewers look for jittered backoff, never retrying a bad request, timeouts on every call, and no silent failover mid-stream once tokens reach the user.
Practise the rest
8 Tools & constraints questions, 492 in all, each with a model answer. Free.
More from Perplexity interview questions.
More Tools & constraints interview questions
- In tool calling, who executes the tool, and what does the model need in order to use the result?
- The model keeps calling search_orders for refund questions that another tool handles. What do you change first?
- Explain one step of constrained sampling. What does it guarantee, and what doesn't it?
- Your agent has a delete_records tool. What has to happen between the model emitting a call and the delete running?
- Should you turn on grammar-constrained decoding for every structured output? What would you watch?
- Design an AI chat for a spreadsheet product that turns commands like sum column K or delete row 5 into validated tool calls on the sheet.