LLM Guardrails: Input and Output Filtering, PII and Moderation
Designing LLM guardrails: input and output classifiers, content moderation, PII redaction, topic and schema checks, grounding checks and fallbacks.
LLM guardrails are the checks around a model that keep an application's behaviour inside acceptable bounds: blocking harmful or off-topic requests, preventing personal data from leaking, making sure outputs are grounded and well-formed, and handing over to a human or a safe default when something looks wrong. The model's own safety training is one layer; production systems add their own, because only you know your policies, your data and your users.
Where guardrails sit
Input guardrails
| Check | How | Action |
|---|---|---|
| Harmful content | Moderation classifier (a provider's moderation endpoint or an open safety model) | Refuse with a safe message; log |
| Off-topic / out of scope | Lightweight intent classifier or embedding similarity to allowed topics | Redirect politely |
| Personal data | PII detectors (patterns plus NER) on user input | Redact before sending to the model or logs |
| Prompt injection attempts | Heuristics and classifiers on user input and retrieved content | Flag, strip, or lower trust; never the only defence (see prompt injection) |
| Abuse and cost attacks | Rate limits, input length caps, per-user budgets | Throttle or reject |
Output guardrails
| Check | How | Action |
|---|---|---|
| Harmful or policy-violating output | Moderation classifier on the response | Block and replace with a refusal or regenerate |
| Data leakage | PII and secret detectors; checks for system prompt content | Redact or block |
| Format | Schema validation | Repair or retry (see structured outputs) |
| Groundedness | Check claims against retrieved sources (judge model or NLI) | Regenerate, add caveat, or say "I don't know" |
| Business rules | Code checks: prices quoted match the catalogue, no promises outside policy | Block or correct |
| Action safety | Permission and limit checks before tool execution | Require approval or deny |
Latency and cost of guardrails
Every check adds latency, and running a large model as a guard on every request can double cost. Practical patterns:
- Cheap first: regex and small classifiers (milliseconds) before any LLM-based check.
- Run in parallel: input moderation can run alongside retrieval; it only needs to finish before the model call.
- Stream with care: when streaming responses, check chunks as they're produced and cut the stream if a check fails, or buffer the first sentences for high-risk products.
- Risk-based depth: a public consumer chatbot needs more checks than an internal tool for engineers.
Designing refusals and fallbacks
- Refusals should be brief, explain what the assistant can help with, and avoid lecturing.
- Over-blocking is a real cost: measure false refusals on legitimate requests, not only missed harms.
- Escalate to a human for high-stakes domains (account security, medical, legal) rather than guessing.
- Fail safe: if the guardrail service itself is down, decide in advance whether to block (high-risk products) or allow with logging (low-risk internal tools).
Measuring guardrails
- Block rate by category, and the share later judged a false positive.
- Missed harms found by sampled review and user reports.
- Added latency per check (p50, p95).
- A red-team test set (jailbreaks, PII extraction, off-topic baits) in the evaluation suite, run on every release.
Where it’s used
Input and output moderation, PII redaction and refusals tuned to minimise both harms and over-blocking.
Data-leak prevention, permission checks and grounding requirements matter more than toxicity.
Policy checks and approval gates before tools run; see AI agents.
In an interview
- Place checks before and after the model and say what each one blocks.
- Order checks cheap to expensive and run some in parallel to protect latency.
- Handle streaming, fallbacks and the guardrail service's own failure.
- Measure false refusals as well as missed harms.
Frequently asked questions
What are LLM guardrails?
Checks around a model that keep an application within acceptable bounds: input checks for harmful, off-topic or injected content and personal data, and output checks for safety, data leakage, format, grounding and business rules, with fallbacks when a check fails.
Do guardrails slow down LLM apps?
They add latency, so run cheap checks first, run input checks in parallel with retrieval, check streamed output in chunks, and match the depth of checks to the product's risk.
Further reading
- Llama Guard: LLM-based Input-Output Safeguard for Human-AI ConversationsInan et al., Meta, 2023
- AI Risk Management FrameworkNIST