方寸 Portal

AI Engineer Portal

Your personal operating system for career transition.

Private mode

Exercise

Build an async model fallback chain with latency budget

Build an async model fallback chain with latency budget

Provider reliability is imperfect. A fallback chain means one provider failing does not make your feature fail. A latency budget means a slow provider gets cut off before it degrades user experience.

What you are building

Implement call_with_fallback — an async function that tries models in a priority order and returns the first successful result within the overall latency budget.

Function signature:

async def call_with_fallback(
    request_fn: Callable[[str], Awaitable[dict]],
    models: list[str],
    latency_budget_s: float = 10.0,
    per_call_timeout_s: float = 5.0,
) -> tuple[dict, str]:
    ...
    # Returns: (result_dict, model_name_used)

Behavior:

  1. Try each model in models order
  2. Each call is wrapped with asyncio.wait_for(..., timeout=per_call_timeout_s)
  3. If the overall elapsed time exceeds latency_budget_s, stop and raise TimeoutError
  4. On any exception (timeout or provider error), log the failure and try the next model
  5. If all models fail, raise RuntimeError("All models in fallback chain exhausted")
  6. Return (result, model_name) where model_name is the model that succeeded

Notes

  • request_fn takes a model name string and returns a dict with at least {"text": str}
  • Track elapsed time with time.monotonic()
  • Simulate logging with print(f"[fallback] model={model} error={exc}")

Api Async / hard / Step 22 of 23

Practice stage

Async and provider control

Hint

Make waiting behavior explicit. Timeouts, retries, and concurrency limits matter more than squeezing everything into one helper.

Success criteria
  • - Uses async boundaries coherently
  • - Makes timeout and retry decisions legible
  • - Would be maintainable under provider instability
Review checklist
  • - Is timeout behavior explicit?
  • - Is retryable failure separate from terminal failure?
  • - Would logs reveal what actually timed out?

Practice

Generate a variation

Generate a new exercise variation to deepen understanding or practice a related concept.

Attempt history

Recent submissions

Before you submit, decide what a strong answer should make obvious to the reviewer.

No attempts yet.