Capstone: ship a reviewed, observable AI feature
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.
What you will be able to do
#- Integrate request building, caching, retrieval, structured output, tool policy and fallbacks into one deterministic mock pipeline.
- Turn a release checklist into a pure gate function that reports exactly which required checks are missing.
- Trace the capstone pipeline for a scripted input and predict its status, source and answer.
Overview
#The capstone is the whole pipeline, not one call
Every earlier lesson isolated one concern. This capstone wires them into a single deterministic mock pipeline and asks the release question: what evidence shows this is ready to ship? The pipeline builds a request, checks a cache, retrieves grounding documents, generates and validates structured output, enforces tool policy, falls back through tiers when a step fails, and emits a structured log event. No key and no network are used; every dependency is a local function or map you can trace.
One pipeline, many concerns
The order matters. A cache check (lesson ai-engineering-caching-and-latency) comes first because it is cheapest. Retrieval (lesson ai-engineering-rag-foundations) grounds the answer and can trigger abstention when nothing is found. Structured output (lesson ai-engineering-structured-output) turns the reply into data and routes parse failures into a fallback. Tool policy (lesson ai-engineering-security-review) gates any side effect. The reliability ladder (lesson ai-engineering-reliability-slo-fallbacks) decides what to serve when a step is unhealthy, and observability (lesson ai-engineering-observability-and-logging) records the outcome as a structured event.
The release checklist is the deliverable
A feature is ready when a checklist of evidence is satisfied, not when a demo looks good. The checklist this lesson uses: keys are server-side; injection through retrieved data and tool results has been reviewed; a deterministic eval set passes; an SLO and fallback ladder are documented; observability is wired; and the cost ceiling is set. releaseGate turns that checklist into a pure function that reports exactly which items are missing, so readiness is a fact rather than a feeling.
Trace it end to end
The exercises trace the capstone by hand. runPipeline returns a status and a source so you can see which path served the request: cache, model, retrieval abstention or static fallback. Because every dependency is deterministic, the same input always produces the same result, which is what makes the pipeline testable without a provider.
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.
Exercise 1Implement
Implement runPipeline(input, deps). Check deps.cache first and return { status: "ok", source: "cache", answer } on a hit. Otherwise call deps.retrieve(input); if it returns no documents, return { status: "abstain", source: "retrieval", answer: deps.staticMessage }. Otherwise call deps.generate(input, docs) and deps.extract on the result; if extraction returns null, return { status: "fallback", source: "static", answer: deps.staticMessage }. On success, store the answer in deps.cache and return { status: "ok", source: "model", answer }.
starter.javascriptfunction runPipeline(input, deps) { // 1. cache hit -> { status: "ok", source: "cache", answer } // 2. no retrieved docs -> { status: "abstain", source: "retrieval", answer: deps.staticMessage } // 3. extract(generate(...)) === null -> { status: "fallback", source: "static", answer: deps.staticMessage } // 4. success -> cache the answer and return { status: "ok", source: "model", answer } }Reveal the reference answer
reference.javascriptfunction runPipeline(input, deps) { const cached = deps.cache.get(input); if (cached !== undefined) { return { status: "ok", source: "cache", answer: cached }; } const docs = deps.retrieve(input); if (docs.length === 0) { return { status: "abstain", source: "retrieval", answer: deps.staticMessage }; } const parsed = deps.extract(deps.generate(input, docs)); if (parsed === null) { return { status: "fallback", source: "static", answer: deps.staticMessage }; } deps.cache.set(input, parsed.answer); return { status: "ok", source: "model", answer: parsed.answer }; } const deps = { cache: new Map(), retrieve: (q) => (q.includes("refund") ? [{ id: "doc-1", text: "Refunds take 5 business days." }] : []), generate: (q, docs) => "{\"answer\":\"" + docs[0].text + "\"}", extract: (text) => { try { return JSON.parse(text); } catch { return null; } }, staticMessage: "Please contact support." }; console.log(JSON.stringify(runPipeline("How long do refunds take?", deps)));Authored expected output: {"status":"ok","source":"model","answer":"Refunds take 5 business days."}
The cache is empty, retrieval finds one document, generation returns a JSON string, extraction parses it, and the answer is cached. The status and source fields make the serving path visible, which is what an observability event would record.
Exercise 2Implement
Implement releaseGate(checks). REQUIRED_CHECKS lists the six required check names. Return { ready: true, missing: [] } when every required check is exactly true, otherwise { ready: false, missing } where missing lists the required names whose value is not true, in the order they appear in REQUIRED_CHECKS.
starter.javascriptconst REQUIRED_CHECKS = [ "keysServerSide", "injectionReviewed", "evalSetPasses", "sloDocumented", "fallbackTested", "observabilityWired" ]; function releaseGate(checks) { // missing = required names whose value is not exactly true // return { ready, missing } }Reveal the reference answer
reference.javascriptconst REQUIRED_CHECKS = [ "keysServerSide", "injectionReviewed", "evalSetPasses", "sloDocumented", "fallbackTested", "observabilityWired" ]; function releaseGate(checks) { const missing = REQUIRED_CHECKS.filter((key) => checks[key] !== true); return { ready: missing.length === 0, missing }; } console.log(JSON.stringify(releaseGate({ keysServerSide: true, injectionReviewed: true, evalSetPasses: true, sloDocumented: true, fallbackTested: false, observabilityWired: true })));Authored expected output: {"ready":false,"missing":["fallbackTested"]}
Five of six checks pass, so readiness is false and the gate reports exactly the missing item. A checklist gate makes release readiness a fact that can be reviewed, not an opinion.
Exercise 3Predict
Using runPipeline and the deps below, predict the JSON array logged. The first input is already in the cache; the second has no matching document and no cache entry.
starter.javascriptconst deps = { cache: new Map([["How long do refunds take?", "Refunds take 5 business days."]]), retrieve: (q) => (q.includes("refund") ? [{ id: "doc-1", text: "Refunds take 5 business days." }] : []), generate: (q, docs) => "{\"answer\":\"" + docs[0].text + "\"}", extract: (text) => { try { return JSON.parse(text); } catch { return null; } }, staticMessage: "Please contact support." }; const results = [ runPipeline("How long do refunds take?", deps), runPipeline("How do I reset my password?", deps) ]; console.log(JSON.stringify(results.map((r) => r.status + ":" + r.source)));Reveal the reference answer
reference.javascript// Predicted output: // // ["ok:cache","abstain:retrieval"] // // Trace: the first input is already a cache key, so runPipeline returns // immediately with source "cache". The second input is not cached, and // retrieve returns an empty array because it does not include "refund", so // the pipeline abstains with source "retrieval".Authored expected output: ["ok:cache","abstain:retrieval"]
The cache hit short-circuits before retrieval, and the miss with no documents abstains instead of guessing. Status and source expose exactly which path served each request, which is the observability contract the pipeline emits.
Exercise 4Explain
Knowledge check: which set of evidence shows an AI feature is ready to ship?
starter.text// Choose the correct option.Reveal the reference answer
reference.textA passing deterministic eval set, a documented SLO with a fallback ladder, a completed security review, server-side keys and wired observability. Readiness is a checklist of evidence, not a single successful demo.Authored expected output: A passing eval set plus a documented SLO, security review and observability
No single signal is sufficient. The other options describe a demo, a model self-report or a single error-free run, none of which is evidence of quality, safety or reliability.
Common misconceptions
#- Myth: a feature that works in a demo is ready to ship. Fact: readiness is a checklist of evidence, including a passing eval set, a documented SLO, a fallback ladder and a security review.
- Myth: the cache, retrieval, output validation and fallback are independent add-ons. Fact: their order is a design decision, and the cheapest, safest step that can answer the request should run first.
- Myth: a passing eval set proves the feature is safe. Fact: an eval set measures task quality on known cases; safety, cost and reliability each need their own evidence.
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.