Lesson 6.1 · 8 min read · about 55 min to master at a very slow pace

Production Architecture

What sits between the user and the model in a real deployment: a backend that holds the keys, streaming, timeouts and retries with backoff, model fallbacks, rate limits, and background queues for long jobs.

Not startedJump to quiz

The shape of a deployment

Browser / Slack / Teams
        │  (streamed response)
        ▼
Your backend API ── auth, rate limits, logging, budgets
   ├── Retrieval: search index + document store
   ├── Tools: client systems (CRM, tickets, ERP)
   ├── LLM gateway: provider(s), retries, fallbacks
   └── Queue + workers: long or batch jobs
        │
        ▼
Traces, metrics, eval samples, feedback

Never call the LLM API directly from the browser: the API key would be public. The backend holds keys, checks who the user is, and enforces limits.

Streaming

Stream tokens to the client (server-sent events or WebSockets) so the user sees text within a second. Remember that streamed output is shown before you can validate all of it; for output that needs checking first (JSON for another system, anything sent to a third party), generate in full, validate, then deliver.

Failures are normal

LLM APIs return rate-limit errors (429), overloads and timeouts every day at scale. Plan for them:

  • Timeouts on every call, sized to the use case.
  • Retries with exponential backoff and jitter for transient errors: wait about 1 s, 2 s, 4 s (plus a random amount) and give up after a few attempts. Do not retry validation errors blindly.
  • Fallback model: if the primary provider fails, route to a second model that has passed the same evals.
  • Graceful degradation: if all else fails, show search results without a generated answer, or "try again in a minute", never a stack trace.

Worked example: retry budget

Backoff waits of 1, 2 and 4 s with three retries add up to 7 s of waiting on top of the attempts. For a chat app with an 8 s budget that is too much: use one quick retry, then fall back to the second model. For a nightly batch job, generous retries are fine.

Long jobs go to a queue

Summarising 500 contracts or running an agent for ten minutes does not belong in an HTTP request. Put the job on a queue, process it with workers, save progress so a crash can resume, and notify the user when it is done. Many providers also offer batch APIs at a discount for work that can wait hours.

Protect the system and the budget

  • Per-user rate limits (requests per minute and per day).
  • Input limits: maximum message length and file size.
  • Spend caps per user, per client and per day, with alerts before they are reached.

Stuck? Common doubts

Questions real learners asked while studying this lesson. They stay closed so they don't spoil your own thinking: open only the ones you need.

First passShould I use one provider or several?

Start with one, behind a thin interface (or an AI gateway) so switching is a configuration change. Add a second when uptime, price, data residency or a specific capability makes it worth evaluating and maintaining two. Every model you route to must pass your evals.

Going deeperServerless or long-running servers?

Either works. Serverless functions suit request–response chat and scale to zero; make sure the platform's timeout allows streaming answers and tool loops. Long-running workers suit queues, agents and batch jobs. Many deployments use both: functions for the API, workers for background jobs.

Cheatsheet

Failure handling

Timeouts, retries with exponential backoff + jitter on transient errors, a tested fallback model, graceful degradation.

Keys stay on the server

Never call the LLM from the browser. The backend holds keys, authenticates users and enforces limits.

Request vs queue

Interactive chat: stream in the request. Long or bulk work: queue + workers, resumable, notify when done.

See every cheatsheet by category →

Check your understanding

Score 75% or more to master this lesson and earn XP. Answer first, then check.

Q1conceptWhy must a web app never call the LLM API directly from the browser?
Q2applyThe provider returns a 429 rate-limit error. What should your backend do?
Q3calculateBackoff waits double from 1 s. How long does a request spend waiting in total over 4 retries?
Q4applyA client wants all 800 of last year's contracts summarised. How should this run?
Q5diagnoseA streamed answer showed JSON to a downstream system before validation, and the system crashed on a malformed field. What was the design error?