方寸 Portal

AI Engineer Portal

Your personal operating system for career transition.

Private mode

Exercise

Build an error classification and retry policy engine

Build an Error Classification and Retry Policy Engine

AI service calls fail in distinct categories, and each category calls for a different response. A 429 rate limit error should wait and retry. A 401 authentication error should fail immediately — retrying is pointless. A 500 server error should retry with backoff. A validation error in the response should log and skip, not retry.

Treating all errors the same (either always retry or never retry) wastes time on unrecoverable errors and abandons recoverable ones.

What to build

Implement an error classification and retry policy system:

  1. ErrorCategory enum with values: RATE_LIMITED, SERVER_ERROR, AUTH_ERROR, VALIDATION_ERROR, TIMEOUT, UNKNOWN.
  2. classify_error(error: Exception | dict) -> ErrorCategory — classifies an exception or error dict. Rules:
    • HTTP 429 or message containing "rate limit" → RATE_LIMITED
    • HTTP 4xx (except 429) or "unauthorized"/"forbidden" → AUTH_ERROR
    • HTTP 5xx or "internal server error" → SERVER_ERROR
    • TimeoutError or "timed out" in message → TIMEOUT
    • ValueError or "validation" in message → VALIDATION_ERROR
    • anything else → UNKNOWN
  3. RetryPolicy dataclass with: should_retry: bool, max_attempts: int, base_delay_s: float, max_delay_s: float.
  4. get_retry_policy(category: ErrorCategory) -> RetryPolicy — returns the appropriate policy per category. AUTH_ERROR and VALIDATION_ERROR → never retry. RATE_LIMITED → retry up to 5 times, 60s base delay. SERVER_ERROR and TIMEOUT → retry up to 3 times, 1s base delay. UNKNOWN → retry once, 2s base delay.
  5. execute_with_retry(fn: Callable, *args, **kwargs) -> dict — wraps a callable, catches errors, classifies them, and applies the retry policy with exponential backoff. Returns {"result": ..., "attempts": int, "category": ErrorCategory | None}.

Why this matters

Production AI systems without explicit retry policies either retry too aggressively (hammering a rate-limited API) or not at all (failing on transient 500s). This classification layer makes retry behavior explicit, testable, and easy to tune per error type.

Constraints

  • execute_with_retry must use exponential backoff: delay = min(base_delay * 2^attempt, max_delay).
  • Do not retry on AUTH_ERROR or VALIDATION_ERROR under any circumstances.
  • Use time.sleep (synchronous version).

Data Transformation / hard / Step 12 of 16

Practice stage

Transform and summarize data safely

Hint

Think like an evaluation or trace pipeline: group clearly, preserve the data story, and make edge cases visible instead of clever.

Success criteria
  • - Handles missing or noisy records predictably
  • - Produces summary output that is easy to inspect
  • - Would be safe to rerun in a script workflow
Review checklist
  • - Do grouped metrics stay readable?
  • - Would malformed rows be debuggable?
  • - Did I choose names that explain the transformation?

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.