LLM guardrails
Guardrails assume the model will sometimes be wrong, manipulated, or
both, and make the system safe anyway. The architecture is defense
in depth: filter what enters, validate what leaves, privilege-limit
what the model can touch, and route the irreducible risk to humans.
Method
- Treat all model-visible content as untrusted. User
messages, retrieved documents, tool results, and web
content can all carry injected instructions
("ignore your instructions and..."): the defense is
structural, not politeness: delimit data from
instructions in the prompt, strip/flag
instruction-shaped content in retrieved data, and
never let model output directly trigger privileged
actions (see tool-use-design's boundary: server-side
authz decides, the model only requests).
- Bound the blast radius by construction. The
agent's credentials scope to the task (see
least-privilege, iam-design); destructive tools
require confirmation or policy approval (see
automation-guardrails); spending, sending, and
deleting have hard caps and rate limits independent
of model judgment (see rate-limiting). Assume
injection succeeds and ask what the attacker then
controls: that inventory is your real exposure
(see threat-modeling).
- Filter inputs proportionate to the surface.
Moderation classifiers for abuse categories on public
surfaces; jailbreak-pattern detection where stakes
justify it; size/rate limits always (see
request-validation): tuned against false positives,
because over-blocking legitimate users is its own
failure (measure both directions: see
llm-eval-design's should-not-refuse sets).
- Validate outputs before they act or ship.
Structured outputs against schema (see
structured-output), citations against sources
(groundedness checks for RAG: see rag-pipeline),
policy checks on generated content (PII leakage:
see pii-handling; unsafe categories), and
plausibility bounds on extracted values: with a
defined action per failure (regenerate, redact,
escalate: see api-error-responses' decision
discipline).
- Design the human escalation path. Confidence
thresholds, sensitive-topic triggers, user requests
for a human, and repeated failure loops route to
people: with context handed over (conversation
summary, what was attempted: see
agent-handoff-protocol's contract, human edition)
and honest UI about the transition (see
conversation-design). Systems without an escape
hatch convert edge cases into incidents.
- Red-team and monitor continuously. Adversarial
testing before launch and on a cadence (injection
attempts through every input channel including
documents and tool results: see
penetration-test-prep's mindset); production
monitoring for guardrail trigger rates, novel
attack patterns, and refusal drift (see
llm-observability, security-incident-response for
when one lands); every confirmed bypass becomes an
eval case (see llm-eval-design's regression loop).
Boundaries
- Guardrails reduce and contain risk; they do not
produce a safe-by-construction system: capability
decisions (what the system can do at all) dominate
any filter stack, and some deployments are wrong at
the capability level.
- Over-restriction is a real cost: measured
false-refusal rates and user friction belong in the
same review as bypass rates (see
product-metrics' guardrail symmetry).
- Compliance regimes (privacy, sector rules) set
floors that engineering judgment cannot waive;
involve the owning functions early (see
pii-handling, data-retention).
1---2name: llm-guardrails3description: Layer input filtering, output validation, injection defense, and human escalation around LLM features. Use when an LLM system faces untrusted input or its outputs carry real-world consequences.4---56# LLM guardrails78Guardrails assume the model will sometimes be wrong, manipulated, or9both, and make the *system* safe anyway. The architecture is defense10in depth: filter what enters, validate what leaves, privilege-limit11what the model can touch, and route the irreducible risk to humans.1213## Method14151. **Treat all model-visible content as untrusted.** User16 messages, retrieved documents, tool results, and web17 content can all carry injected instructions18 ("ignore your instructions and..."): the defense is19 structural, not politeness: delimit data from20 instructions in the prompt, strip/flag21 instruction-shaped content in retrieved data, and22 never let model output *directly* trigger privileged23 actions (see tool-use-design's boundary: server-side24 authz decides, the model only requests).252. **Bound the blast radius by construction.** The26 agent's credentials scope to the task (see27 least-privilege, iam-design); destructive tools28 require confirmation or policy approval (see29 automation-guardrails); spending, sending, and30 deleting have hard caps and rate limits independent31 of model judgment (see rate-limiting). Assume32 injection *succeeds* and ask what the attacker then33 controls: that inventory is your real exposure34 (see threat-modeling).353. **Filter inputs proportionate to the surface.**36 Moderation classifiers for abuse categories on public37 surfaces; jailbreak-pattern detection where stakes38 justify it; size/rate limits always (see39 request-validation): tuned against false positives,40 because over-blocking legitimate users is its own41 failure (measure both directions: see42 llm-eval-design's should-not-refuse sets).434. **Validate outputs before they act or ship.**44 Structured outputs against schema (see45 structured-output), citations against sources46 (groundedness checks for RAG: see rag-pipeline),47 policy checks on generated content (PII leakage:48 see pii-handling; unsafe categories), and49 plausibility bounds on extracted values: with a50 defined action per failure (regenerate, redact,51 escalate: see api-error-responses' decision52 discipline).535. **Design the human escalation path.** Confidence54 thresholds, sensitive-topic triggers, user requests55 for a human, and repeated failure loops route to56 people: with context handed over (conversation57 summary, what was attempted: see58 agent-handoff-protocol's contract, human edition)59 and honest UI about the transition (see60 conversation-design). Systems without an escape61 hatch convert edge cases into incidents.626. **Red-team and monitor continuously.** Adversarial63 testing before launch and on a cadence (injection64 attempts through every input channel including65 documents and tool results: see66 penetration-test-prep's mindset); production67 monitoring for guardrail trigger rates, novel68 attack patterns, and refusal drift (see69 llm-observability, security-incident-response for70 when one lands); every confirmed bypass becomes an71 eval case (see llm-eval-design's regression loop).7273## Boundaries7475- Guardrails reduce and contain risk; they do not76 produce a safe-by-construction system: capability77 decisions (what the system can do at all) dominate78 any filter stack, and some deployments are wrong at79 the capability level.80- Over-restriction is a real cost: measured81 false-refusal rates and user friction belong in the82 same review as bypass rates (see83 product-metrics' guardrail symmetry).84- Compliance regimes (privacy, sector rules) set85 floors that engineering judgment cannot waive;86 involve the owning functions early (see87 pii-handling, data-retention).