The Model Router

Difficulty: MEDIUMID: model-fallback-strategy

The Scenario

You rely on Claude Opus 4.5 for your production app. On Tuesday at 2pm, Anthropic has an outage (500 Internal Server Error). Your app crashes. Users are angry.

The Problem

Your code calls the primary model directly.

return client.chat.completions.create(model="claude-opus-4-5-20251101", ...)

If this raises an exception, the request fails.

The Goal

Implement a Model Router with fallback logic:

  1. Attempt to call Primary Model (Opus 4.5).
  2. If it fails (APIError, Timeout), catch the exception.
  3. Log the error.
  4. Call Secondary Model (e.g., Gemini 3 Pro or GPT-5.1) as backup.

Requirements:

  • Wrap the API call in try/except
  • Fallback to backup model on error
  • Re-raise exception if both fail
solution.py
Loading...
⚠️ Do not include PII or secrets in your code.
SYSTEM_LOGS
5/5
// Waiting for execution trigger...
PREVIEW MODE — SOLVE PREVIOUS MISSIONS TO UNLOCK