Learning SequenceVol. 18 · Part XVIII
0 / 0

Trails / Learning Sequence

LS·210

Phase 14 — AI Production

The fourteenth and last phase: AI APIs, model routing, inference, caching, evaluation, security, tracing, cost, deployment and GPU awareness, ending with an AI platform integrated with your frontend, Java backend, data stores, queues and observability stack; the order and why, an entry check, step-by-step exit checks mapped to Volumes 14B, 16 and 17, the project with milestones and definitions of done, traps, a time budget, and a lab that routes questions between a cheap path and a large model and measures accuracy, cost and latency.

16 topics · ~8 min read · 16-question quiz
Updated Suggest a correction

Start here: Phase 14 in one page

A RAG demo and an AI feature in production differ the way a script and a service do. Phase 14 adds what production needs: choosing and routing between models, serving them efficiently, caching, continuous evaluation, defending against prompt injection and data leaks, tracing every call, controlling cost, deploying safely and understanding the hardware underneath. The project connects the AI work to everything you built in Phases 1–11, which is the bridge to the capstone.

The sequence

Text
AI APIs → model routing → inference → caching → evaluation → security → tracing → cost → deployment → GPU awareness

Start where most teams start, with hosted model APIs; then use several models wisely (routing), understand what inference costs and why (tokens, batching, KV cache), and avoid repeating work (caching). Then the controls that keep quality and safety from drifting (evaluation, security), the visibility that makes problems findable (tracing), the bill (cost), safe rollout (deployment) and finally the hardware view (GPU awareness) for when you self-host.

Routing, measured

The lab built a two-tier system on the Phase 13 retrieval data: a cheap path (BM25 retrieval, answering from the top passage) and a large model, with an escalation rule based on the cheap path's confidence. The large model's accuracy (95%), price and latency are assumptions for illustration; the cheap path's numbers are measured:

Escalate when confidence is below Escalated Accuracy Cost per 1,000 Mean latency
never (cheap path only) 0% 0.457 $0.20 0.05 s
0.05 30.4% 0.640 $3.85 0.51 s
0.10 51.3% 0.758 $6.36 0.82 s
0.20 76.1% 0.881 $9.33 1.19 s
0.30 89.9% 0.933 $10.98 1.40 s
always (large model only) 100% 0.950 $12.20 1.55 s

Routing works only as well as its confidence signal. Here the signal was weak: to reach 93% accuracy the router had to escalate 90% of requests, saving just 10% of the cost. A better signal (a reranker score, a classifier trained on past outcomes) is worth more than any threshold tuning.

How deep to go in Phase 14

Volume 17 defines six mastery levels; Phase 14's exit targets are (L5–L6 on the AI track):

Topic Exit level What that means here
AI APIs and model routing L4 provider abstraction, fallbacks, routing by measured signals
inference and caching L3–L4 tokens, batching, KV cache, quantisation; exact and semantic caches
evaluation L4 offline and online evaluation, regression gates
security L3–L4 prompt injection defences, data protection, tool permissions
tracing and cost L4 per-request traces and cost attribution, budgets
deployment L4 versioned prompts and models, canaries, rollback
GPU awareness L2–L3 memory, throughput, batching and when self-hosting pays

The sequence, step by step

Step Learn Read Exit check
1 AI production architecture Vol. 14b, ch. 180 a diagram of your platform with every model call
2 serving and inference Vol. 14b, ch. 165–166, 179 measure tokens per second and time to first token for one model
3 routing and caching Vol. 14b, ch. 179–180 a router and a cache with measured savings
4 evaluation in production Vol. 14b, ch. 175, 178 online evaluation with sampled human review
5 AI security Vol. 14b, ch. 176 a prompt-injection test suite that runs in CI
6 AI observability and cost Vol. 14b, ch. 177 traces with cost per request and a budget alert
7 deployment Vol. 14b, ch. 178; Vol. 16, ch. 188, 192 a canary for a prompt or model change with automatic rollback
8 depth at L5/L6 Vol. 17, ch. 196B rate yourself with evidence on each AI topic

Entry check

Phases 12–13, plus the backend, data, messaging and observability work of Phases 3–9.

The phase project: an AI platform integrated with the whole system

Goal. Make the Phase 13 application a first-class part of your system: called from the React frontend, served through the Java backend, using your data stores and queues, and visible in your observability stack.

Milestones. A model gateway in the backend (provider abstraction, timeouts, retries, fallbacks, rate limits) → routing between at least two models with a measured confidence signal → exact and semantic caches with tenant scoping → streaming responses to the frontend → asynchronous jobs (ingestion, long tasks) through the queue from Phase 8 → evaluation gates in CI and online sampling → prompt-injection and data-leak tests → OpenTelemetry traces with tokens and cost per request, dashboards and budget alerts → versioned prompts and models with canary rollout → a capacity and cost model, including a self-hosting comparison on paper.

Done when. A user can use the AI feature end to end in the app; every model call is traced with cost; routing and caching savings are measured; evaluation and security suites gate deploys; a prompt change can be canaried and rolled back; the cost model says what 10× traffic would cost.

Stretch. Serve a small open model yourself (CPU or a rented GPU) and compare cost and latency with the API.

Common traps

  • One model for everything with no fallback.
  • Routing on a weak signal without measuring it.
  • Caches that leak across users.
  • No cost attribution until the bill arrives.
  • Shipping prompt changes without evaluation.

Exit criteria

  • [ ] The project meets its "done when" list.
  • [ ] I can explain inference cost and latency from tokens, batching and hardware.
  • [ ] I can defend an AI feature against prompt injection with layered controls.
  • [ ] I can roll out a model or prompt change safely.
  • [ ] I scored at least 80% on 40 questions from Volume 14B's chapters 176–180, twice.

Time budget

Volume 14B's chapters 176–180 (about 24,000 words) plus the named chapters of Volumes 16 and 17 (about 17,000) are about 3 hours of reading; with practice, about 12 hours, plus 4–6 weeks for the project: about 6–7 weeks at 8 hours a week.

Lab: routing between a cheap path and a large model

Setup. The 879 queries and 5,346 chunks from the Phase 13 lab. The cheap path answers with the chapter of the top BM25 passage; it's correct for 45.7% of queries (measured). Its confidence is the relative margin between the top two passage scores, (s₁ − s₂) / s₁. Below a threshold, the request escalates to a large model.

The large model isn't run; its figures are assumptions chosen to be typical of a hosted frontier model relative to a cheap path, so treat the shape of the results, not the dollar amounts, as the lesson:

Cost per request Latency Accuracy
cheap path $0.0002 0.05 s 0.457 (measured)
large model $0.012 1.5 s 0.95 (assumed)

Escalated requests pay for both.

Results

Threshold Escalated Accuracy Cost per 1,000 Mean latency Cheap-path accuracy on requests it kept
0 (never) 0% 0.457 $0.20 0.05 s 0.457
0.05 30.4% 0.640 $3.85 0.51 s 0.505
0.10 51.3% 0.758 $6.36 0.82 s 0.556
0.20 76.1% 0.881 $9.33 1.19 s 0.662
0.30 89.9% 0.933 $10.98 1.40 s 0.787
always 100% 0.950 $12.20 1.55 s —

Reading the results

  • The last column is the diagnosis. A good confidence signal would leave the cheap path with only the questions it gets right. Here, even after escalating half the traffic, the cheap path was right on only 56% of what it kept. The margin between BM25 scores is a weak predictor of correctness.
  • So the curve is nearly a straight line from cheap-and-wrong to expensive-and-right: no threshold gives "most of the accuracy for a fraction of the cost". With a strong signal, the same table would show high accuracy at low escalation.
  • What to do instead: measure candidate signals (reranker scores, agreement between retrievers, a small classifier trained on logged outcomes) by how well they separate right from wrong (e.g. ROC AUC) before building a router on one.
  • Latency follows escalation: routing is also a latency tool when the cheap path is good.

Try it yourself

  1. Compute the ROC AUC of the margin signal for predicting cheap-path correctness.
  2. Try a different signal (the top score itself, or agreement between BM25 and a dense retriever) and rebuild the table.
  3. Plot accuracy against cost for each signal; the better signal's curve bulges toward the top-left.
  4. Change the assumed prices by 10× either way: which conclusions change, and which don't?

Revision sheet: Phase 14, AI production

Key ideas

  • Sequence: AI APIs → model routing → inference → caching → evaluation → security → tracing → cost → deployment → GPU awareness.
  • Route only on a measured, discriminating confidence signal.
  • Cache with scoping and expiry; trace every call with cost; gate deploys on evaluation and security tests.
  • The project integrates AI with the whole system, the bridge to the capstone.

Numbers to remember

  • Lab: cheap path 0.457; 93% accuracy needed 90% escalation and saved only 10% of cost (assumed large-model prices).
  • Prefill is parallel; decode is one token at a time.
  • Budget: ~12 study hours + 4–6 project weeks ≈ 6–7 weeks at 8 h/week.

Pitfalls

  • Single model, no fallback.
  • Weak routing signals; leaky caches.
  • No cost attribution; unevaluated prompt changes.

Glossary

  • Model gateway: a service that abstracts providers and adds policy.
  • Cascade / routing: send requests to models by difficulty or cost.
  • Semantic cache: reuse answers for similar requests.
  • TTFT: time to first token.
  • KV cache: stored attention keys and values for decoding.