Guardrails and moderation

AI Engineer · Where it goes wrong · 02 / 04 · Intermediate · 26 min

Input and output checks, classifiers and moderation APIs, human review — and an honest account of what guardrails can promise.

Before thisPrompt injectionLLM as judge

Jumping Rivers · 26 min

Guardrails and moderation

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by Jumping RiversReport a problem with this video

What you'll learn

  • A guardrail is a runtime check on the way in or the way out, and it is probabilistic — a filter, not a boundary.
  • Output checks catch what input checks cannot, because the harm is often in what the model produced rather than in what was asked.
  • Every guardrail has a false-positive cost, and a system that blocks legitimate requests is failing its users in a way nobody logs.

What a guardrail is

A check that runs around the model rather than inside it. On the way in, it inspects the incoming request before the model ever sees it. On the way out, it inspects the generated response before it reaches the user, and may block it, redact part of it, trigger a regeneration, or escalate to a human reviewer. The word “guardrail” suggests something close to an impenetrable barrier; the reality is a classifier operating with a tunable threshold, which means there are always two distinct error rates to manage, and the position of the dial between them is a product decision as much as it is a security one.

The categories worth distinguishing

Toxicity and abuse filters catch content that is harmful or policy-violating in its own right, independent of context. PII detectors flag personal information — names, phone numbers, government identifiers — that should not be sent onward to a third-party API or stored longer than necessary. Jailbreak and injection detectors look specifically for patterns associated with attempts to override a system’s intended instructions, as covered in prompt injection. Topic restriction keeps a system from engaging with subjects outside its intended scope, regardless of whether the content would otherwise be considered harmful — a legal-document assistant declining to offer medical advice, for instance. Format validation checks that output actually conforms to what is expected structurally, overlapping with but distinct from the schema enforcement covered in structured output, since a guardrail here is usually checking semantic appropriateness of the content rather than just its syntactic shape.

A layered architecture, cheap first

A sensible guardrail pipeline runs a fast, cheap classifier — often a small, purpose-trained model running in milliseconds — as a first pass over every single request, catching the large majority of clearly obvious cases at negligible cost. Only requests the fast pass flags as uncertain or borderline get escalated to a slower, more expensive check — a larger model, a more thorough analysis, or a full human review — reserving that expensive step for the minority of genuinely ambiguous cases rather than running it on everything. This layering is what makes comprehensive guardrails affordable at real production volume, where running the most thorough possible check on every single request would be prohibitively slow and costly.

The trade-off, with worked numbers

Every classifier threshold trades false positives against false negatives, and tightening one side always loosens the other. Consider a toxicity filter set to catch 95 percent of genuinely toxic messages — a 95 percent recall. At that sensitivity it might also incorrectly flag 3 percent of entirely benign messages as toxic — a 97 percent precision. Pushing recall higher, to catch 99 percent of genuinely toxic content, might push the false-positive rate up to 8 or 10 percent of benign messages, because the classifier has to be more aggressive to catch the hardest, most borderline toxic cases, and that aggressiveness also sweeps in more innocent cases along with them. Which side to favour is a genuine product decision, not a purely technical one: a system moderating public-facing content at scale might tolerate more false positives to minimise any toxic content getting through, while an internal tool used by trusted employees might tolerate more false negatives to avoid interrupting legitimate work unnecessarily.

Open-source tooling, briefly

A number of open-source projects exist specifically for this layer — small, purpose-built classifiers for toxicity, PII and jailbreak detection, and broader frameworks for defining and enforcing guardrail policies declaratively rather than hand-coding each check. These are worth knowing exist before building equivalent classifiers from scratch, though evaluating any of them against your own specific traffic, using the same methodology as the eval set lesson, remains necessary — a classifier tuned on someone else’s data distribution will not automatically transfer its stated accuracy numbers to yours.

Human review, done right

For anything irreversible or high-stakes, routing to a human reviewer before an action proceeds is the strongest available guardrail. The review interface has to show the actual, concrete action about to be taken — the literal message about to be sent, the literal transaction about to execute — rather than a natural-language summary the model generated describing its own intended action, for exactly the reason covered in prompt injection: a model whose behaviour has been manipulated can write a reassuring summary of a harmful action just as fluently as an honest one.

What guardrails cannot promise

A guardrail reduces the rate of a bad outcome. It does not eliminate it. Classifiers are themselves models, and can frequently be evaded by the same general techniques they are built to detect, in an ongoing pattern familiar from spam filtering and other adversarial classification domains. Guardrails are defence in depth, layered on top of an architecture that already bounds the damage any single failure can cause — never the sole mechanism a system relies on for safety. And whatever rate is chosen, the false positives deserve active measurement: a blocked legitimate request produces a frustrated user and typically no log entry calling attention to it, so sampling what gets blocked and reading it periodically is the only reliable way to notice that a threshold has drifted too far toward caution.

Check yourself

  1. 01Why are output guardrails necessary if you already filter the input?

    Because harm often has no harmful input. A benign question can produce a leaked system prompt, an ungrounded claim or private data pulled in by retrieval, and only an output check sees that.

  2. 02What is the cost of tightening a guardrail, and why is it easy to miss?

    False positives — legitimate requests blocked. They rarely generate an error or an alert, so the damage shows up as users quietly giving up rather than in any metric you are watching.

Go deeper

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark