Interview question
Implement a model gateway with deadlines, budgets, fallback, and version evidence
Builds a provider-neutral model gateway that owns request deadlines, retry classification, budget enforcement, fallback policy, and versioned telemetry.
TL;DR
Builds a provider-neutral model gateway that owns request deadlines, retry classification, budget enforcement, fallback policy, and versioned telemetry.
Dependency boundaries, timeout budgets, retry safety, fallback compatibility, cost controls, concurrency limits, and operational evidence.
Practice the problem like a real interview: restate, reason, implement, and test.
Implement the execution path of a model gateway used by several product features. Callers specify a capability and a total deadline, not a provider model name. The gateway must choose an approved deployment, enforce token and cost budgets, bound concurrency, retry only safe temporary failures within the remaining deadline, and use a compatible fallback when policy allows. Every result must expose safe version and usage evidence.
Move to the linked follow-up, next path step, prerequisite, or deeper variant.
Move forward in the planned practice sequence.
capability=structured_extract, deadline=8s, max_cost=$0.02
primary returns 429 with retry-after=1s -> retry only if deadline and attempt budget permit
primary times out after 7s -> do not start a fallback that cannot finish inside the remaining second
fallback lacks required schema support -> reject route during configuration validation.
async def execute(command: ModelCommand) -> ModelResult:
route = routes.resolve(command.capability, command.policy)
deadline = clock.monotonic() + command.timeout_seconds
budget = await budgets.reserve(command.tenant_id, command.max_cost)
async with concurrency.slot(command.tenant_id, command.capability):
for target in route.targets:
if not target.supports(command.requirements):
continue
for attempt in range(target.max_attempts):
remaining = deadline - clock.monotonic()
if remaining < target.minimum_attempt_seconds:
raise GatewayDeadlineExceeded()
try:
raw = await target.adapter.generate(
command.payload,
timeout=min(remaining, target.per_attempt_timeout),
max_output_tokens=command.max_output_tokens,
)
result = normalize_and_validate(raw, command.requirements)
await budget.commit(result.usage)
return result.with_route_evidence(target, attempt)
except TemporaryProviderError as error:
if not retry_policy.allows(error, attempt, remaining):
break
await clock.sleep(retry_policy.delay(error, attempt))
except (Refusal, PolicyViolation, InvalidRequest):
raise
raise NoCompatibleModelSucceeded()
Routing is O(number of configured targets and bounded attempts). Provider calls dominate latency, while explicit limits cap simultaneous work, generated tokens, and cost exposure.
Aporeon is shaped by Aleksandar Tomovski, a software developer with experience on both sides of technical interviews. Content is reviewed for accuracy, natural spoken delivery, useful depth, and honest trade-offs.
Help improve the interview library.
Say thanks with a standalone, one-time $5 contribution. No account required.