Lesson 64.1 · 1 of 11 · 4 min read · Reviewed 24 Nov 2026
Where guardrails sit in a system
By the end, you can
- Map where guardrails sit in an AI system.
- Explain defence in depth.
Chapter 45 introduced guardrailsGuardrail: A control that keeps an AI system within safe and allowed behaviour, such as input checks, tool limits or output filters. for agentsAgent: A system where a model decides its own next steps, calling tools in a loop until the task is done.. This chapter treats them as a system design topic.
The layers
user input -> [input checks] -> model -> [action checks] -> tools
\-> [output checks] -> user
| Layer | Examples |
|---|---|
| Input | Size limits, PII detection, jailbreak and injection classifiers, topic filters |
| Policy | Rules in code about who may do what (64.4) |
| Model | System prompt, examples, refusal behaviour |
| Action | Permissions, hooks, approval gates (64.5) |
| Output | Schema validation, PII redaction, moderation, citation checks |
Defence in depth
Each layer misses something. An input classifier misses a clever jailbreakJailbreak: An attempt to get a model to break its safety rules, usually by role play, obfuscation or persistence.; the model follows a hidden instruction; but the action layer blocks the tool call, or the output filter catches the leaked data. Layers that fail differently make the whole system much stronger.
Cost and placement
Cheap, deterministic checks in code (size, regex, permissionsPermission rule: An allow, ask or deny rule that controls which tools, commands or paths a coding agent may use.) go first. Model-based checks cost more and add latencyLatency: The time from sending a request until you get the response, or until the first token arrives when streaming., so use them where code can’t decide. Chapter 64.7 covers measuring them.
Key points
- Guardrails are checks around the model: before input reaches it, on what it decides to do, and on what it outputs.
- No single layer is perfect, so stack several cheap ones (defence in depth).
- Put each check where it's cheapest and most reliable: often in code, not in the prompt.
Stack three guardrail layers
Put cheap input, policy and output checks around a model and see which layer catches what.
You will have: Three check functions and a table of six requests showing which layer blocked each one.
Steps
- Run the starter: all six requests pass.
- Add an input size limit.
- Add a policy check that uses the real order owner and a refund limit.
- Add an output check for card digits in the reply.
- Count blocks per layer and note that each layer catches something the others miss.
Done when
- Four requests are blocked, spread across all three layers.
- The ordinary order question passes.
Open in the Playground No API key needed.
Starter code (Python)
# Defence in depth: three cheap layers around a pretend model.
REQUESTS = [
{"user": "u1", "text": "Where is order A-1001?", "action": None},
{"user": "u1", "text": "x" * 5000, "action": None},
{"user": "u2", "text": "Refund order A-1001 please", "action": ("refund", "A-1001", 30)},
{"user": "u1", "text": "Refund my order", "action": ("refund", "A-1001", 900)},
{"user": "u1", "text": "Show the address on order B-2002", "action": ("read", "B-2002", 0)},
{"user": "u1", "text": "Thanks!", "action": None, "reply": "Your card ends 4242 (test data). Anything else?"},
]
OWNER = {"A-1001": "u1", "B-2002": "u3"}
def input_check(r):
# TODO: block inputs longer than 2000 characters
return None
def policy_check(r):
# TODO: block actions on orders the user does not own, and refunds above 100
return None
def output_check(r):
# TODO: block replies that mention card digits
return None
blocked = {}
for r in REQUESTS:
for layer in (input_check, policy_check, output_check):
why = layer(r)
if why:
blocked[layer.__name__] = blocked.get(layer.__name__, 0) + 1
print("BLOCK", layer.__name__, "-", why)
break
else:
print("pass", "-", r["text"][:30])
print("blocked by layer:", blocked)
Show a solution
import re
REQUESTS = [
{"user": "u1", "text": "Where is order A-1001?", "action": None},
{"user": "u1", "text": "x" * 5000, "action": None},
{"user": "u2", "text": "Refund order A-1001 please", "action": ("refund", "A-1001", 30)},
{"user": "u1", "text": "Refund my order", "action": ("refund", "A-1001", 900)},
{"user": "u1", "text": "Show the address on order B-2002", "action": ("read", "B-2002", 0)},
{"user": "u1", "text": "Thanks!", "action": None, "reply": "Your card ends 4242 (test data). Anything else?"},
]
OWNER = {"A-1001": "u1", "B-2002": "u3"}
def input_check(r):
if len(r["text"]) > 2000:
return "input too long"
def policy_check(r):
if not r["action"]:
return None
kind, order, amount = r["action"]
if OWNER.get(order) != r["user"]:
return "not this user's order"
if kind == "refund" and amount > 100:
return "refund above limit, needs approval"
def output_check(r):
if re.search(r"card ends \d{4}|\b\d{4} \d{4} \d{4} \d{4}\b", r.get("reply", "")):
return "reply mentions card digits"
blocked = {}
for r in REQUESTS:
for layer in (input_check, policy_check, output_check):
why = layer(r)
if why:
blocked[layer.__name__] = blocked.get(layer.__name__, 0) + 1
print("BLOCK", layer.__name__, "-", why)
break
else:
print("pass", "-", r["text"][:30])
print("blocked by layer:", blocked)
print("passed:", len(REQUESTS) - sum(blocked.values()), "of", len(REQUESTS))