Skip to content
~/series/production-ai-systems/cost-and-reliability
EST: 34mSECTIONS: 11SKILLS: 4

Lesson 2 · Production AI systems

34 min readExpert

Cost and reliability

Caching, model routing, fallbacks, circuit breakers — the production reliability stack.

The request path of a cost and reliability stack

Step 1 of 7

Rate limit

Cap requests per user first, so a traffic spike or a cost attack cannot reach the paid layers.

Each layer is useful alone, and their savings multiply rather than add.

Cost and reliability

Cost and reliability are two sides of production AI. Cost: cache, route to cheap models, compress prompts, set budgets. Reliability: retry, fall back across providers, circuit-break failing services, degrade gracefully. Together they keep your feature affordable AND up.

Why this matters

Without cost controls, an AI feature can 10x its budget overnight (traffic spike, model swap, prompt bloat). Without reliability patterns, a single provider outage takes your product down. Both are mandatory for production. The good news: the patterns compose and compound.

Cost + reliability stack

Request → rate limit → semantic cache (hit = return) → model router (cheap model first) → retry with backoff → fallback chain (provider A → B → C) → circuit breaker per provider → cost budget enforcement → response. Each layer is independently useful and composable.

The full reliability stack

reliability.py
import hashlib, time, random
from dataclasses import dataclass

# --- Semantic cache ---
@dataclass
class CacheEntry:
    response: str
    query_embedding: list[float]
    model: str
    created_at: float

cache: dict[str, CacheEntry] = {}

def cache_get(messages, model, threshold=0.95):
    q = str(messages)
    qhash = hashlib.sha256(q.encode()).hexdigest()
    if qhash in cache:
        return cache[qhash].response
    # Semantic check
    qemb = embed(q)
    for entry in cache.values():
        if entry.model != model: continue
        if cosine(qemb, entry.query_embedding) >= threshold:
            return entry.response
    return None

def cache_set(messages, response, model):
    q = str(messages)
    qhash = hashlib.sha256(q.encode()).hexdigest()
    cache[qhash] = CacheEntry(response, embed(q), model, time.time())

# --- Model router ---
# Model names change often; check your provider's model list (as of 28 October 2026). Replace <...-model> with a model id from that list.
SMALL, LARGE, OTHER_PROVIDER = "<small-model>", "<large-model>", "<other-provider-model>"

def route_model(query: str) -> str:
    if len(query) < 80 and "compare" not in query.lower():
        return SMALL
    return LARGE

# --- Fallback chain with retry + circuit breaker ---
RETRYABLE = {429, 500, 502, 503, 504}
FALLBACK = [LARGE, OTHER_PROVIDER, SMALL]
breaker_failures = {m: 0 for m in FALLBACK}

def call_with_full_stack(messages):
    # Cache
    model = route_model(messages[0]["content"])
    if cached := cache_get(messages, model):
        return cached, "cache"

    for model in [model] + [m for m in FALLBACK if m != model]:
        if breaker_failures[model] >= 5:
            continue  # circuit open
        for attempt in range(3):
            try:
                resp = call_llm(messages, model=model)
                cache_set(messages, resp, model)
                breaker_failures[model] = 0
                return resp, model
            except Exception as e:
                status = getattr(e, "status_code", None)
                if status not in RETRYABLE and status is not None:
                    breaker_failures[model] += 1
                    break  # non-retryable, try next model
                delay = min(30, 0.5 * (2 ** attempt))
                time.sleep(random.uniform(0, delay))
                breaker_failures[model] += 1
    raise RuntimeError("All models failed")

Experiment: cost + reliability levers

Toggle each lever. See cumulative effect on cost and uptime. The cost savings follow from the stated hit rate and routing share; the uptime figures are invented for illustration.

Enable/disable levers. See combined effect on cost and uptime.

What to observe

The levers compound. Cache alone is -60% cost. Routing alone is -75%. Together: -90% (they multiply, not add). In this illustration, fallback + breaker take uptime from 99% to 99.95%; measure your own provider's availability. Stack all four for production-grade cost AND reliability. None is sufficient alone.

Production cost + reliability

Production stack: semantic cache (tune similarity threshold), model routing (monitor accuracy), fallback chain (pin versions, normalise response shapes), circuit breaker (tune threshold + cooldown), per-user cost budgets, cost dashboard, anomaly alerts, and graceful degradation (return cached/partial on failure).

Challenge

Your feature costs about $200/day in week 1 and about $35,800/day in week 2. Traffic 3x'd, cost about 178x'd. Three things changed — identify all three and how much each multiplies cost. (See the 'Cost blowup after launch' challenge.)

Production checklist

Production checklist

0 of 9 checked

Knowledge check

Cache reduces cost 60%, routing reduces 75%. Both on. Total reduction?

Complete

You can now build the cost + reliability stack. Next: security and guardrails — the final production layer.

Mark this chapter as complete

Track your progress and unlock the next chapter.

Email updates

Get the next chapter and new guides by email.

One email per substantial release. Unsubscribe in one click.

We will email you a confirmation link first, then send an occasional email when a substantial guide is published. Unsubscribe in one click. Stored with our email provider (Resend), never sold or shared. See the privacy policy.

Continue learning