Lesson 9.9advanced3 h 20 min

Production: latency, cost, caching, guardrails, security and privacy

What changes when real users and real data arrive

A demo answers one question; a product answers a hundred thousand a day for people who did not write the prompt and may be trying to break it. Production adds a latency budget, a cost model that input tokens dominate, caching at three layers, reliability engineering, guardrails, and two threats specific to RAG: prompt injection arriving through your own documents and permission bypass through retrieval. These concerns multiply when the system gains tools and autonomy in Phase 10 (10.10 Agent safety and security, 10.11 Building production agents).

You'll be able to

  • Decompose request latency into stages and name the lever for each
  • Write a per-request cost model and identify the levers that matter most
  • Apply prompt caching, semantic caching and component caching appropriately
  • Design guardrails, retries, fallbacks and routing for reliability
  • Defend against indirect prompt injection and enforce access control at retrieval time

Key ideas

  1. Latency is a sum of stages; prefill grows with context length and decode with output length — stream, parallelize independent stages, and watch p95.
  2. Per-request cost ≈ (T_in·p_in + T_cached·p_cached + T_out·p_out)/10⁶ plus embedding, reranking and infrastructure; input tokens usually dominate.
  3. Prompt caching rewards stable-prefix-first prompt layout with cheaper, faster cached tokens; semantic caching reuses answers to near-duplicate queries but risks wrong hits; component caches memoize embeddings and retrieval.
Show 4 more key ideas
  1. Reliability basics: backoff with jitter, timeouts, idempotency, circuit breakers, fallback models, graceful degradation, pinned versions gated by evals.
  2. Guardrails are a policy layer on inputs and outputs; measure false positives, because over-blocking is a visible failure.
  3. Indirect prompt injection arrives through retrieved documents; the lethal trifecta (private data + untrusted content + exfiltration channel) is defeated by removing or gating the exfiltration channel and applying least privilege.
  4. Access control is enforced by code at retrieval time; redact logs, honour deletion down to the vectors, and plan embedding-model migrations as dual-index cut-overs.

Suggested path through this lesson