Skip to article content
FrontendXLesson 19 / 20

Reliability: SLOs, budgets and fallbacks

Build LLM-backed product features end to end against a deterministic local mock: form well-formed chat requests, structure prompts and validated JSON outputs, stream and retry safely, ground answers in retrieved sources, call tools inside bounded agent loops, defend against prompt injection, and evaluate quality with reproducible checks instead of one-off demos. No API keys and no network calls are used anywhere in the course.

advanced · about 50 minutes · 4 exercises

  • Predict
  • Explain
  • Implement

What you will be able to do

#
  • Define an SLO for an AI feature in terms of measurable indicators and compute its error budget in minutes.
  • Model a circuit breaker as a pure function of failures, threshold and cooldown, and explain the open, half-open and closed states.
  • Trace a deterministic fallback ladder that returns a degraded but valid tier instead of an error.

Overview

#

Reliability is a target, not a hope

Lesson ai-engineering-errors-retries-and-timeouts made a single call resilient. Lesson ai-engineering-observability-and-logging made it visible. This lesson sets the target those mechanisms protect: a service-level objective (SLO). An SLO is a measurable promise such as "99.5 percent of chat requests return a valid answer within two seconds, measured over 30 days". It is defined on service-level indicators (SLIs): success rate, latency percentiles and cost per request. Without a stated SLO, "the feature is reliable" is an opinion.

Error budgets turn a target into a decision

An SLO implies an error budget: the amount of unreliability the target allows. For a 99.5 percent success target over a window, the budget is 0.5 percent of that window. In minutes over a 30-day window (43200 minutes) that is 216 minutes. The budget is a decision tool, not a report: while budget remains, ship features; when it is exhausted, freeze risky changes and spend effort on reliability. Burn rate, how fast the budget is being consumed, drives alerts so you learn about a problem before the budget is gone.

Degrade in tiers, do not just fail

When the primary path is unhealthy, a resilient feature falls back through tiers instead of returning an error: a fresh cache entry (lesson ai-engineering-caching-and-latency), then a smaller or cheaper model (lesson ai-engineering-model-routing-and-cost), then a retrieval-only answer, then a static message. Each tier returns something valid, so the user gets a degraded answer rather than a dead end. Fail closed only where a wrong answer is worse than no answer, such as a payment action; fail open where availability matters more, such as a suggestion.

Circuit breakers stop the bleeding

A circuit breaker watches consecutive failures. Past a threshold it opens and rejects calls immediately, giving the failing dependency room to recover. After a cooldown it moves to half-open and lets a probe through; success closes it, failure reopens it. The breaker converts a slow cascade of timeouts into a fast, predictable failure that the fallback ladder can handle.

Budgets and ladders as deterministic mocks

The exercises keep everything local. errorBudgetMinutes is arithmetic on a window and a target. circuitState is a pure function of failure count, threshold and cooldown. The fallback ladder is a pure function of three booleans, so you can trace exactly which tier serves any health state.

Exercises

#

Reference answers and expected output are authored, not executed in this browser. Nothing you type is run, and there is no Run or Pass control on this page.

  1. Exercise 1Implement

    Implement errorBudgetMinutes(windowMinutes, sloTarget). Return the number of minutes of allowed unreliability: windowMinutes multiplied by (1 - sloTarget). sloTarget is a fraction such as 0.995.

    starter.javascript
    function errorBudgetMinutes(windowMinutes, sloTarget) {
      // windowMinutes * (1 - sloTarget)
    }

    Reveal the reference answer
    reference.javascript
    function errorBudgetMinutes(windowMinutes, sloTarget) {
      return windowMinutes * (1 - sloTarget);
    }
    
    console.log(String(errorBudgetMinutes(43200, 0.995)));

    Authored expected output: 216

    43200 minutes (30 days) times 0.005 equals 216 minutes. The budget is the amount of failure the target tolerates; when it is exhausted, reliability work takes priority over new features.

  2. Exercise 2Implement

    Implement circuitState(failures, threshold, cooldownElapsed). Return "closed" when failures is below threshold, "open" when failures is at least threshold and cooldown has not elapsed, and "half-open" when failures is at least threshold and cooldown has elapsed.

    starter.javascript
    function circuitState(failures, threshold, cooldownElapsed) {
      // below threshold -> "closed"
      // at or above threshold and no cooldown -> "open"
      // at or above threshold and cooldown elapsed -> "half-open"
    }

    Reveal the reference answer
    reference.javascript
    function circuitState(failures, threshold, cooldownElapsed) {
      if (failures < threshold) return "closed";
      return cooldownElapsed ? "half-open" : "open";
    }
    
    console.log(circuitState(4, 3, true));

    Authored expected output: half-open

    Four failures meet the threshold of three, and the cooldown has elapsed, so the breaker admits a probe: half-open. A successful probe closes it; a failed probe reopens it.

  3. Exercise 3Predict

    Using decideFallback, predict the JSON array logged for the four health states. The ladder is primary, then a fresh cache entry, then the secondary model, then a static message.

    starter.javascript
    function decideFallback(primaryOk, cacheFresh, secondaryOk) {
      if (primaryOk) return "primary";
      if (cacheFresh) return "cache";
      if (secondaryOk) return "secondary";
      return "static";
    }
    
    const states = [
      { primaryOk: true, cacheFresh: false, secondaryOk: true },
      { primaryOk: false, cacheFresh: true, secondaryOk: true },
      { primaryOk: false, cacheFresh: false, secondaryOk: true },
      { primaryOk: false, cacheFresh: false, secondaryOk: false }
    ];
    console.log(JSON.stringify(states.map((s) => decideFallback(s.primaryOk, s.cacheFresh, s.secondaryOk))));

    Reveal the reference answer
    reference.javascript
    // Predicted output:
    //
    // ["primary","cache","secondary","static"]
    //
    // Trace: state 1 has a healthy primary -> "primary". State 2 falls to a fresh
    // cache entry -> "cache". State 3 has no cache but a healthy secondary ->
    // "secondary". State 4 has nothing healthy -> "static".

    Authored expected output: ["primary","cache","secondary","static"]

    Each state resolves to the first healthy tier in order. A ladder like this turns a dependency outage into a degraded answer instead of an error, and every tier returns something the caller can render.

  4. Exercise 4Explain

    Knowledge check: what does the error budget implied by an SLO represent?

    starter.text
    // Choose the correct option.

    Reveal the reference answer
    reference.text
    The amount of unreliability the SLO target allows over its measurement window, expressed as a fraction of that window. It is a decision tool: while budget remains, ship; when it is spent, prioritize reliability.

    Authored expected output: The allowed amount of unreliability implied by the target

    The budget is derived from the target, not chosen separately. The other options describe cost, retries or token limits, which are different numbers.

Common misconceptions

#
  • Myth: an SLO is a guarantee that the feature will never fail. Fact: an SLO is a target that defines an allowed error budget; the budget is expected to be spent.
  • Myth: the correct response to any dependency failure is an error page. Fact: a fallback ladder returns a degraded but valid answer, and fail-closed is reserved for cases where a wrong answer is worse than none.
  • Myth: a circuit breaker fixes the failing dependency. Fact: it stops your service from waiting on it and gives it room to recover; the dependency still needs repair.

Follow-up practice

#
  • ai-engineering-production-capstone

Related resources

#

Your progress

#

Answers, drafts and completion marks are stored only in this browser. Nothing is sent to a server and no account is used.

Verification: authored. Answers and outputs on this page are authored reference material, not executed results.

© 2026 FrontendX. All rights reserved.