0) Problem Restatement
Anthropic asked: design the safety and reliability layer around an LLM-powered production feature (e.g., a support assistant). Models can receive malicious inputs (prompt injection), produce harmful or wrong outputs, return invalid formats, be slow, or fail entirely. Design guardrails (checks before and after the model) and fallbacks (what to do when something fails), without making the product slow or annoying.
1) The Request Pipeline
Architecture Diagram
flowchart LR
U["User input"] --> IN["Input guardrails - PII, injection, policy"]
IN -->|"blocked"| REF["Safe refusal / help message"]
IN --> CTX["Build prompt - system rules + retrieved data"]
CTX --> M1["Primary model - timeout"]
M1 -->|"error / timeout"| FB["Fallback: retry, smaller model, cached answer"]
M1 --> OUT["Output guardrails - schema, safety, grounding"]
FB --> OUT
OUT -->|"fails"| REPAIR["Repair / regenerate / safe template"]
OUT --> RESP["Response to user"]
OUT --> LOG[("Logs + metrics")]2) Input Guardrails
- Validation: length limits, allowed languages, rate limits per user.
- PII handling: detect and mask personal data (card numbers, IDs) before sending to the model or logs, when not needed.
- Prompt injection detection: classifiers and heuristics for "ignore previous instructions", and hidden instructions in retrieved documents. Treat retrieved content as data (clearly delimited), never as instructions.
- Policy checks: disallowed topics for this product, with a friendly refusal.
3) Output Guardrails
- Format validation: if the app expects JSON, validate it against a schema. If invalid, try a repair (ask the model to fix it, or use constrained decoding), then fall back.
- Safety filters: toxicity, self-harm, and leaked secrets or PII in the output.
- Grounding checks (for RAG): does the answer cite retrieved sources? Are claimed facts supported? If not, answer "I'm not sure" or show sources only.
- Business rules: never promise refunds, never quote prices not in the data, and so on.
4) Fallbacks
- Timeouts: a strict timeout per call. Retry once with backoff for transient errors (5xx, rate limits).
- Model fallback chain: primary model → a smaller or faster model → a cached answer for common questions → a templated response ("I can't answer right now, here's a help article") → human handoff.
- Circuit breaker: if the primary model's error rate spikes, route traffic to the fallback for a while, instead of hammering it.
- Streaming with care: output checks on a streamed answer run on chunks (or a buffer of a few sentences before display) so harmful text isn't shown and then retracted.
5) Measuring and Improving
- Metrics: guardrail hit rates per check, false positive rate (from user appeals and reviews), fallback rate, latency added per stage, and incidents.
- Evaluation sets: red-team prompts (injection, jailbreaks), normal prompts (to catch over-blocking), and format tests. Run them on every change.
- Safe rollout: new guardrail versions run in shadow mode first (log what they would block), then gradually enforce.
- Ownership: policies are config (versioned), owned by trust and safety plus product, and applied consistently across features.
6) Trade-offs
- Stricter checks → safer but more false refusals and added latency. Tune per product and risk level.
- Checking with another LLM is accurate but costly and slow. Use cheap classifiers first, and escalate only uncertain cases.
7) Wrap-Up
Wrap the model in a pipeline: input guardrails (validation, PII masking, injection detection, policy), a carefully built prompt that treats retrieved text as data, and output guardrails (schema validation with repair, safety filters, grounding and business rules). Handle failures with timeouts, one retry, a fallback chain (smaller model, cache, template, human) and circuit breakers, and keep it trustworthy with red-team evals, shadow-mode rollouts and metrics on blocks, false positives and fallbacks.