Warmup Guide — GenAI in Production
Zero-to-expert primer for Phase 10: turning RAG demos into systems enterprises can trust — hallucination measurement, prompt-injection defense, structured outputs, refusal policy, and the evaluation harness that gates every change.
Table of Contents
- Chapter 1: Why GenAI Production Is Failure Management
- Chapter 2: The Production RAG Loop
- Chapter 3: Hallucination, Measured
- Chapter 4: Prompt Injection — the Attack Surface
- Chapter 5: Structured Output and the Repair Loop
- Chapter 6: Refusal Policy and Topical Guardrails
- Chapter 7: Evaluation Harnesses and LLM-as-Judge
- Chapter 8: Cost, Latency, and the Cascade Pattern
- Lab Walkthrough Guidance
- Success Criteria
- Interview Q&A
- References
Chapter 1: Why GenAI Production Is Failure Management
A classical ML model fails quantifiably — a wrong class, a bad score, measurable offline. An LLM application fails fluently: confident prose containing fabricated facts, leaked instructions, or malformed JSON that takes down the consumer service. Three engineering consequences define this phase:
- Validation moves to the output side. You cannot test an LLM into correctness ahead of time; you wrap every response in checks (groundedness, schema, policy) and route failures to fallbacks.
- Quality becomes a measured rate, not a property. "Does it hallucinate?" is the wrong question; "what is the unsupported-claim rate on our golden set, and did this prompt change move it?" is the right one (Ch. 3, 7).
- Inputs are adversarial. The prompt is an API that users — and documents — can program. Injection is an attack surface with defense-in-depth, not a quirk (Ch. 4).
The JD's phrase "hallucination analysis … and safety controls" is this chapter, operationalized in the lab.
Chapter 2: The Production RAG Loop
The demo loop is retrieve → stuff → generate. The production loop:
query → [input guards] → retrieve (hybrid, Phase 04) → rerank →
context assembly (token budget, source attribution, freshness) →
generate (schema-constrained where possible) →
[output guards: groundedness, policy, schema] → respond | fallback | escalate
The load-bearing differences from the demo:
- Context assembly is a budgeted ranking problem: which chunks, in what order (LLMs attend better to start/end — "lost in the middle"), with per-source attribution markers so groundedness checking (Ch. 3) and citation are possible. Retrieval quality bounds answer quality — most "hallucination" bugs are retrieval bugs (right answer not in context → model improvises); always check retrieval recall before blaming the generator.
- Fallbacks are designed, not improvised: low retrieval confidence → "I don't have information on this" beats a fluent guess; guard failure → retry with repair, then degrade to extractive answer (quote the chunks), then escalate to a human. The fallback ladder is a product decision encoded in the service.
- Everything is versioned: prompt templates, retrieval config, model version, guard thresholds — because Ch. 7's harness must attribute regressions to a change (the Phase 08 lineage discipline, applied to prompts).
Chapter 3: Hallucination, Measured
For RAG systems the operational definition is groundedness (faithfulness): every factual claim in the answer is supported by the retrieved context. This is measurable:
- Decompose the answer into claims (sentence-level is the practical granularity; clause-level is better and costlier).
- Score support for each claim against the context: token/n-gram overlap (cheap, the lab's method), NLI entailment models (better), LLM-as-judge with a support rubric (best, with Ch. 7's caveats).
- Aggregate: groundedness = supported claims / total claims; the complement — the unsupported-claim rate — is the hallucination metric you trend, gate on, and report.
Two companion metrics complete the triangle: answer relevance (does it address the question?) and context relevance (was the retrieval any good? — the diagnostic that splits retrieval bugs from generation bugs). This triangle is the RAGAS-style evaluation core, and the lab implements its mechanics by hand so the industrial tools are legible.
The honest caveats: overlap-based scoring misses paraphrase (false alarms) and miss-attributes coincidental overlap (false passes); sentence decomposition struggles with compound claims. That's why the lab pairs the scorer with golden cases — hand-labeled supported/unsupported examples that pin the scorer's behavior — and why Extension A measures judge-vs-rule agreement before trusting either.
Chapter 4: Prompt Injection — the Attack Surface
The vulnerability class: LLMs cannot reliably distinguish instructions from data in their context. Anyone who controls text the model reads can attempt to program it.
- Direct injection: the user says "ignore previous instructions and …" — embarrassing but bounded by the user's own privileges.
- Indirect injection (the serious one): instructions hidden in retrieved documents, emails, or web pages — the attacker programs your system through your own corpus, with the system's privileges. Any RAG system over user-contributed or external content has this surface.
Defense in depth (no single layer suffices; the lab builds the testable ones):
- Privilege separation — the architectural defense: the LLM's outputs get least privilege; tool calls are allow-listed and parameter-validated; actions with side effects require confirmation. Assume injection will sometimes succeed; bound what success buys.
- Input/context filtering: detect known injection patterns in user input and retrieved chunks (the lab's detector); strip or quarantine suspicious content; mark untrusted spans in the prompt ("the following is data, not instructions" — weak alone, useful in depth).
- Output filtering: detect signs of successful injection (system-prompt leakage, off-policy actions) before the response leaves.
- Canary regression testing (the lab's signature move): maintain a suite of known attack strings; run them through the full pipeline on every change; the attack success rate is a tracked metric exactly like accuracy. Pattern detectors decay as attacks evolve — the canary suite is how you notice.
Chapter 5: Structured Output and the Repair Loop
Downstream systems need JSON, not prose. The reliability ladder:
- Constrained decoding (where you control inference): grammar/JSON-schema enforced at the token level — malformed output becomes impossible (the llm-inference track covers the mechanics). Use when available; it converts a validation problem into a non-problem.
- Validate-then-repair (the portable pattern, the lab's): validate against the schema (Phase 01's contract library does this verbatim!); on failure, re-prompt with the validation errors included ("your output failed: missing field X; emit corrected JSON only") — one repair round fixes the large majority of failures; bound the loop (1–2 rounds), then fallback.
- Never
json.loads+ pray: every unvalidated LLM output consumed by code is a production incident on a timer.
Design note the lab encodes: the validator returns aggregated, path-specific errors (Phase 01's collect-don't-raise) precisely because those errors become the repair prompt — error-message quality is now system quality.
Chapter 6: Refusal Policy and Topical Guardrails
Enterprise assistants have a charter: topics they must not advise on (medical, legal, competitor comparisons…), data they must not reveal (PII, system prompts), and tones they must keep. Engineering form:
- Policy as data (the lab): rules with scope (input/output), matcher, action (block / safe-completion / escalate), and rationale — reviewable and versionable like any config, because policy changes need the same governance as code (Phase 12).
- Layered enforcement: system-prompt instruction (steers the model) + input classifier (blocks before spending tokens) + output classifier (catches what steering missed). Measure each layer's catch rate separately — that's how you know which layer regressed.
- The refusal-quality bar: a good refusal states what it can't do, why, and what it can do instead — refusal UX is measurable with the same golden-set machinery (over-refusal is also a failure mode: track false-refusal rate on benign prompts, or the guardrails quietly eat the product).
Chapter 7: Evaluation Harnesses and LLM-as-Judge
The harness is to GenAI what the test suite is to code — and this phase's lab plus Phase 12's CI gate make it literal:
- Golden sets: curated (question, context, reference-or-rubric) cases covering the product's real distribution plus its known failure modes; 50 good cases beat 5 000 scraped ones. Versioned, content-hashed (the Phase 09 model-accuracy discipline).
- Canary suites: the adversarial twin — injections, policy probes, format-breakers. Gate = (quality metrics ≥ baseline) AND (attack success rate ≤ threshold) AND (false-refusal rate ≤ threshold).
- LLM-as-judge: scalable scoring of fluency/relevance/support with a rubric prompt. Its measured biases — position (prefers first answer; mitigate by swapping orders), verbosity (prefers longer), self-preference (prefers its own model family) — and the discipline: validate the judge against human labels on a sample (agreement rate), re-validate when the judge model changes. A judge is a model in production; it gets the same skepticism.
- What runs when: rule-based checks on every request (ms, the lab's layer); golden+canary harness on every change (minutes); human-labeled audits monthly (the calibration anchor).
Chapter 8: Cost, Latency, and the Cascade Pattern
The economics that shape GenAI architecture:
- Token cost asymmetry: context tokens are bought on every request — context assembly discipline (Ch. 2) is a cost lever; caching (Phase 09's tiers, plus provider prompt-caching for stable prefixes) routinely cuts 30–70%.
- The cascade: route easy/common queries to a small model (or cache, or extractive path); escalate hard ones to the big model — a router classifier + confidence threshold. Most enterprise traffic is head-heavy; cascades cut cost multiples at equal quality if the router is evaluated with the same rigor as everything else (its errors are silent quality loss).
- Latency shape: TTFT (time to first token) is UX; streaming hides total latency; guards add tail latency — run input guards in parallel with retrieval, output guards on the stream (sentence-buffered) where possible.
- The Phase 09 frontier thinking applies unchanged; only the units (tokens, not requests) are new.
Lab Walkthrough Guidance
Lab 01 — Guardrail & Eval Harness, suggested order:
sentences+token_overlap_support— the groundedness primitives; pin against the hand-built cases first.groundedness_report— claim decomposition, per-claim support, the unsupported-claim rate. The compound-claim test shows the granularity limits; read its comment.InjectionDetector— patterns + the canary suite; note the suite is data (CANARY_ATTACKS), so red-teamers extend it without touching code.PolicyEngine— policy-as-data evaluation, first-match-wins with severity ordering.StructuredOutputGuard— Phase 01's validation shape + the bounded repair loop with the mock LLM.GuardrailHarness.evaluate— compose everything into the gate verdict; the final tests run the full suite both on a healthy mock pipeline and a broken one.
Success Criteria
You are ready for Phase 11 when you can, from memory:
- Draw the production RAG loop with both guard stages and the fallback ladder.
- Define groundedness operationally and compute an unsupported-claim rate by hand on a 3-sentence answer.
- Explain direct vs indirect injection and the four defense layers; say why canaries are the regression mechanism.
- Recite the structured-output ladder and why repair prompts want path-specific errors.
- Name the three LLM-as-judge biases and the validation discipline.
- Sketch the cascade pattern and its router-evaluation requirement.
Interview Q&A
Q: Users report the assistant "makes things up." Walk through your response. Instrument before fixing: build/check the golden set, measure the groundedness triangle — if context relevance is low, it's a retrieval problem (fix Phase 04's stack: hybrid search, reranking, chunking); if context is fine but support is low, it's generation (tighten the prompt's grounding instructions, add citation requirements, lower temperature, add the groundedness output guard with extractive fallback). Then make the metric a CI gate so the fix can't silently regress. The answer's skeleton: measure → localize (retrieval vs generation) → fix the right stage → gate.
Q: Your RAG corpus includes user-uploaded documents. What's your injection story? Indirect injection is the threat model: a poisoned upload programs the assistant for other users. Defense in depth: least-privilege outputs (no tool side effects from document-derived instructions; allow-listed tools with validated params), context filtering on retrieved chunks (detector + quarantine), untrusted-span marking in prompts, output filtering for leakage, and a canary corpus — poisoned test documents whose attack success rate is tracked per release. And the honest statement: filters mitigate, privilege separation is the only defense that holds against novel attacks.
Q: When do you trust LLM-as-judge scores? After validating against human labels on a sample (report agreement, not vibes), with bias mitigations in place (order swapping, length normalization, judge ≠ generator family), pinned judge version, and re-validation on judge change. Use judges for relative comparisons (A/B of prompts) where biases partially cancel, not absolute quality claims. A judge is a model in production — it gets a golden set too.
References
- Lewis et al., Retrieval-Augmented Generation (2020) — arXiv:2005.11401
- Liu et al., Lost in the Middle (2023) — arXiv:2307.03172 — context-position effects
- Es et al., RAGAS: Automated Evaluation of RAG (2023) — arXiv:2309.15217 — the metric triangle
- Greshake et al., Not what you've signed up for: Indirect Prompt Injection (2023) — arXiv:2302.12173
- OWASP Top 10 for LLM Applications — the attack-surface checklist
- Zheng et al., Judging LLM-as-a-Judge (2023) — arXiv:2306.05685 — the bias measurements
- NVIDIA NeMo Guardrails and Guardrails AI — industrial versions of the lab
- llm-inference track Phase 07 — the RAG/agent foundations this phase hardens