Module 1 · Designing AI systems · Chapter 02 · Free
The reference architecture every AI product shares
- Almost every AI product, whatever the vendor, has the same eight layers: surface, orchestration, model gateway, retrieval, tools, memory, guardrails, and eval with observability.
- The model is one layer out of eight. Most production failures happen in the other seven.
- When something goes wrong, name the layer that owns the fix before you change anything.
The picture: a restaurant. A dining room, a kitchen pass, one supplier door, a pantry, hands that cook, a memory of regulars, house rules, and someone tasting the food.
- The eight layers, each with its job, the failure when it is missing, and one design question.
- One request, "where is my refund?", walked through all eight layers.
- A latency budget splitting 3 seconds across the layers, arithmetic shown.
- A matching lab: eight design problems, each owned by one layer.
BrightCart is a made-up online retailer. Its first support assistant was two layers deep: a chat box wired straight to a model. Launch week, at an illustrative 900 chats a day:
- It quoted a returns policy from training data. The real policy had changed two months earlier, costing an illustrative $4,100 in goodwill credits in week one.
- It told 63 customers their refund "had been issued". No system had been called, so no refund existed.
- When answers got worse after a quiet model update, nobody could tell. No traces, no test set.
Nothing here is a model problem. Each failure is a missing layer.
- Walk one refund request through the picture.
- Layers 1 to 4: surface, orchestration, gateway, retrieval.
- Layers 5 to 8: tools, memory, guardrails, eval.
- Worked example: a 3 second latency budget.
- Lab: match eight problems to the layer that owns the fix.
Next: the picture, and one request walking through it.
Step 1: One request, eight layers
Think of the product as a building with eight floors. A request enters at the top, work happens on each floor, and the answer leaves the way it came in. Press Next and follow a customer asking "where is my refund?"
Where the picture breaks down:
- A real request may loop back to retrieval and tools several times, not visit each floor once.
- Eval is less a floor than the building inspector: it watches every floor, mostly after the fact.
- Floors can merge in one service. That is fine, as long as each job has an owner.
- "Where is my refund?" arrives in the support chat widget. The surface handles typing, streaming and the session, nothing smarter.
- Input checks confirm sign-in and strip an account number the customer pasted, so raw secrets never reach the model.
- Your code starts the loop: identify the order, check its status, then answer. It owns routing, retries and step limits.
- State holds what the chat established: order 7719, bought 12 days ago, so nobody asks again.
- Retrieval pulls the current returns policy and refund timeline, fresh from the help centre, not model memory.
- One door picks the model, applies keys and limits, and logs tokens, so a vendor swap is a config change.
- The model asks for the order system. Your code calls it: refund issued two days ago, landing Friday. A fact, not a guess.
- Output guardrails confirm no other customer data is named. Trace, cost and a quality score are logged for eval.
Next: the first four layers, one by one.
Step 2: Layers 1 to 4, where the request takes shape
1. User surface. Job: where people and programs meet the product: chat, voice, a widget, or an API. Missing it: the design assumes one channel, so adding voice or a partner API later means rebuilding. Ask: which surfaces must this work on in year one, and what does each do to input?
2. Orchestration. Job: the code that runs the loop: routes requests, calls layers in order, retries failures, stops runaway work. Missing it: the prompt becomes the program, and nobody can say why a request took its path. Ask: which steps are fixed in code, and which does the model choose? Basics: agents and workflows.
3. Model gateway. Job: one door to every model: routing, keys, rate limits, timeouts and cost logs. Missing it: keys and model names scatter through the codebase, and switching vendor becomes a rewrite during an outage. Ask: if this model doubled in price tomorrow, what exactly would we change?
4. Retrieval and data. Job: brings in facts the model does not have: documents, indexes and fresh data such as prices. Missing it: the model answers from training memory, stale the day your policy or prices change. Ask: how fresh must each fact be, and who owns updating it? End to end: RAG end to end.
Next: layers 5 to 8, where answers become actions and evidence.
Step 3: Layers 5 to 8, action, memory, safety, proof
5. Tools and actions. Job: the systems the AI can touch: order lookup, refunds, calendars, tickets. Words become real-world effects here. Missing it: the assistant can only describe actions, or worse, claim it did them, like BrightCart's phantom refunds. Ask: which actions are read-only, and which change the world and need limits?
6. Memory and state. Job: holds the conversation, user facts, and task state across steps and visits. Missing it: every message starts from zero, so users repeat order numbers and long tasks lose their place. Ask: what do we remember, for how long, and what must we delete on request?
7. Guardrails and policy. Job: input and output checks, permissions, and human approval for risky actions. Missing it: private data flows to the model, harmful output flows out, and a $40 refund gets the same scrutiny as a $4,000 one. Ask: which actions need a human yes first, and what is blocked outright?
8. Eval and observability. Job: traces of every request, quality scores, golden test sets, and cost meters. Missing it: quality changes are invisible, so a bad model update ships and stays shipped. Ask: how would we know within a day if answers got 10 percent worse? More in Chapter 11, "The eval loop: the pattern that makes every other pattern safer", and evals.
Predict first:
With no golden set and no scored traces, "the model changed" and "our product got worse" look identical, and both are invisible until customers complain. Eval is the layer that turns a silent change into a number.
Next: the same eight layers, priced in milliseconds.
Step 4: Worked example, a 3 second latency budget
Budgets make layers concrete. All figures are illustrative. Target: the refund answer completes in 3,000 ms. Split it before you build (memory reads sit inside orchestration, gateway overhead inside the model call, eval runs after the answer, off the clock):
- Surface, render and stream setup: 100 ms
- Orchestration, routing and state: 150 ms
- Retrieval, policy plus order facts: 400 ms
- Model call, via the gateway: 1,800 ms
- Tools, the order system lookup: 300 ms
- Guardrails, checks in and out: 150 ms
- Slack, kept for slow days: 100 ms
Check: 100 + 150 + 400 + 1,800 + 300 + 150 + 100 = 3,000 ms. The model is 1,800 / 3,000 = 60 percent of the budget, which is why Chapter 3 takes up latency next. More: latency, streaming and cost.
Now the model call overruns to 2,400 ms, 600 ms over. You must find 600 ms or break the target:
- Narrow retrieval to the top 3 passages instead of 8: saves about 150 ms.
- Run the tool lookup in parallel with retrieval instead of after it: saves about 300 ms of wall clock.
- Let the gateway route this simple status question to a smaller, faster model: saves about 150 ms.
150 + 300 + 150 = 600 ms. Budget restored, each cut naming its layer. Notice what you did not cut: guardrails. Speed bought by removing safety is a loan, not a saving.
Next: the lab. Eight problems, eight owners.
Step 5: Hands-on lab, match the problem to its layer
Why this exercise: in design reviews, the fastest skill is naming the owning layer. It turns "the AI is bad" into a fixable ticket.
Do this:
- On paper, sketch the eight layers for a product you know, using the checklist below (illustrative). Mark any layer you cannot describe: that blank is your first design task.
- Then do the matching exercise: eight problems, one per layer.
Layer checklist (illustrative)
[ ] Surface: which channels, year one?
[ ] Orchestration: fixed steps vs model-chosen?
[ ] Gateway: how do we swap models?
[ ] Retrieval: freshest fact, and its owner?
[ ] Tools: read-only vs world-changing?
[ ] Memory: what is kept, what is deletable?
[ ] Guardrails: what needs human approval?
[ ] Eval: golden set, traces, cost meter?
Lab: In design reviews, the fastest skill is naming the owning layer, because it turns a vague complaint like the AI is bad into a fixable ticket. This lab runs in your browser and needs JavaScript enabled.
- None of the eight fixes is "use a better model". Most production pain lives outside the model layer.
- Similar problems split by cause: stale facts go to retrieval, silent drift to eval, invented actions to tools.
In design reviews, a useful habit is drawing these eight boxes on a whiteboard before naming any product. Teams tend to find two or three boxes empty, and those empty boxes predict the first production incidents better than the model choice does. Layer names vary between teams; the jobs do not.
Mix-ups to avoid
- The gateway is the model. No: the gateway is the door. Models sit behind it and can change without the app changing.
- Memory is retrieval. No: memory holds this user and this task. Retrieval fetches shared facts and documents.
- Guardrails are the system prompt. No: a prompt is a request. Guardrails are checks and permissions enforced in code.
- Eval is testing before launch. It starts there, but traces, scores and cost meters run in production too.
- Eight layers means eight services. No: layers are jobs with owners. Small products merge several into one codebase.
Chapter quiz
Five questions. Score 70% or more to mark this chapter complete.
Review this chapter in 5 minutes
Watch the whole chapter recapped: the eight layers, one refund request walked through them all, and a 3 second latency budget split by layer.
- Eight layers: surface, orchestration, model gateway, retrieval, tools, memory, guardrails, eval and observability.
- For each layer, know three things: its job, the failure when it is missing, and one design question.
- One refund request touches all eight: in at the surface, checked, routed, given context and facts, answered via the gateway, verified by a tool, and logged.
- Latency budget, illustrative: 3,000 ms total, model call 1,800 ms (60 percent). A 600 ms overrun is recovered by named cuts in named layers.
- Layers are jobs with owners, not boxes to buy. Merging them in code is fine; leaving a job ownerless is not.
Decision rule: for every failure or feature, name the layer that owns it before discussing solutions. If no layer owns it, that gap is the design problem.
Review
Glossary
- User surface
- Where users meet the product: chat, voice, embedded widget or API. Example: a support chat box.
- Orchestration
- Code that runs the loop: routing, ordering, retries and limits. Example: a status question takes a short path.
- Model gateway
- One door to all models holding keys, routing and limits. Example: swap models by changing one config.
- Retrieval
- Fetching facts and documents at answer time. Example: pulling today's returns policy. See also Chapter 5, "Retrieval patterns: RAG variants and when each one fits".
- Tools and actions
- Systems the AI can call to read or change the world. Example: an order lookup, or issuing a refund.
- Memory and state
- What the product keeps about the conversation, user and task. Example: the order number given two messages ago.
- Guardrails
- Checks and permissions enforced in code, in and out. Example: human approval before a large refund.
- Golden set
- A fixed set of questions with known good answers, scored on every change. Example: 100 support questions run nightly.
- Trace
- A record of one request across all layers: inputs, calls, timing, cost. Example: why one answer took 4 seconds.
- Latency budget
- A total time target split across layers before building. Example: 3,000 ms, with 1,800 ms for the model call.
Next: "Quality attributes: accuracy, latency, cost and safety pull against each other".