Sub topic 1
Why LLM applications need guardrails
A language model produces text that looks plausible. It does not guarantee that the text is safe, correct, or in the format your program expects. A guardrail is a check that runs around a model call and decides whether an input or an output may continue. This chapter explains why you need them and previews the rest of the tutorial.
Prerequisites
This chapter assumes you have:
- Basic Python, including functions and regular expressions.
- Experience calling an LLM through an API or SDK.
- A basic understanding of prompts and system prompts.
- Basic JSON knowledge.
The problem: the model is not the whole application
When you call a model in a demo, you read the answer and judge it yourself. In production, nobody reads each answer first. Your code passes the text on to a user, a database, or another tool. Three facts make this risky:
- Users send anything. Inputs can include personal data, abuse, or text written to override your instructions.
- Models are probabilistic. The same prompt can give different answers. A model can state false facts with confidence or ignore a format you asked for.
- Outputs have consequences. A malformed JSON reply crashes a parser. A leaked secret reaches a customer. A wrong claim reaches someone who acts on it.
Prompting the model to "be safe" helps, but it is a request, not a rule. The model can ignore it. A guardrail is code that enforces the rule outside the model.
What a guardrail looks like
A guardrail has one job: take a piece of text and return a decision. The simplest version is a plain Python function.
import re
EMAIL = re.compile(r"[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}")
US_SSN = re.compile(r"\b\d{3}-\d{2}-\d{4}\b")
def redact_pii(text: str) -> str:
text = EMAIL.sub("[EMAIL]", text)
return US_SSN.sub("[SSN]", text)
print(redact_pii("Mail [email protected], SSN 123-45-6789"))
# Mail [EMAIL], SSN [SSN]This check is deterministic: the same input always gives the same result. It runs in microseconds and costs nothing. It does not need a model, so a clever prompt cannot talk it out of its job. Later chapters cover checks that are less predictable but catch more, such as classifier models and LLM judges.
Where guardrails sit
Checks go at two points: before the model sees the input, and after the model produces the output.
The failure handler decides whether to block, redact, retry, or fall back to a safe answer. Chapter placement and failure handling are covered in their own chapters.
The risks guardrails address
The next chapter covers these in detail. In short, guardrails target:
| Risk | Where it is checked |
|---|---|
| Prompt injection | Input, and the model's actions |
| Personal data leaving or entering the system | Input and output |
| Harmful or off-policy content | Input and output |
| Output that does not match the expected format | Output |
| Answers not supported by source material | Output |
What guardrails cannot do
Guardrails reduce risk. They do not remove it. A regular expression misses an SSN written with spaces. A classifier blocks some harmless requests and passes some harmful ones. An LLM judge can be wrong too. Because every check fails sometimes, you will combine several and measure how often each one is wrong. The final chapters return to this idea of layered defense.
Summary
- Production code uses model text without a human reading it first, so something must check it.
- Asking the model to behave is not enforcement. Guardrails enforce rules in code.
- Checks run at two points: before and after the model call.
- No single check is enough, and every check has errors you must measure.
Exercise
Why is adding "Never reveal private data" to the system prompt not a sufficient guardrail, and what would you add in code?
Show answer
The system prompt is an instruction the model can ignore or be tricked into ignoring, so nothing enforces it. Add an output check in code, such as a PII pattern match that redacts or blocks matching text before it reaches the user.
Exercise
A check returns the same result for the same input every time and does not call a model. What is this kind of check called, and name one advantage over a model-based check.
Show answer
It is a deterministic (rule-based) check. Advantages include speed, zero model cost, and predictability. A prompt cannot manipulate it.