Sub topic 3
Where checks go: before and after the model call
This chapter assumes you can write basic Python, call an LLM through an SDK, and read JSON. You should also know what a system prompt is. Here you will learn where guardrail checks sit around a model call and what each position can and cannot catch.
Two checkpoints
A guardrail is a check that runs around the model call. There are two places to put one.
- An input check runs on the text before it reaches the model. It stops bad requests early and keeps sensitive data out of the prompt.
- An output check runs on the model's reply before the user or another system sees it. It catches problems that the model itself introduced.
The two checkpoints catch different things. An input check cannot know what the model will say. An output check cannot undo the cost of a call that should never have been made.
What belongs at each checkpoint
| Checkpoint | Typical checks | Why here |
|---|---|---|
| Input | Length limit, known injection phrases, PII redaction, topic allowlist | Cheap to run, and they avoid a model call or protect the prompt |
| Output | Empty reply, leaked secrets, JSON structure, banned content | Only the reply shows what the model actually produced |
Put a check where the information it needs first exists. A length limit needs only the user's text, so it goes before the call. A check for a leaked private key needs the reply, so it goes after.
A minimal wrapper
The wrapper below runs both checkpoints. call_model is any function that takes a prompt string and returns a string.
from dataclasses import dataclass
@dataclass
class GuardResult:
allowed: bool
reason: str = ""
def check_input(text: str) -> GuardResult:
if len(text) > 4000:
return GuardResult(False, "input too long")
if "ignore previous instructions" in text.lower():
return GuardResult(False, "possible prompt injection")
return GuardResult(True)
def check_output(text: str) -> GuardResult:
if not text.strip():
return GuardResult(False, "empty output")
if "BEGIN PRIVATE KEY" in text:
return GuardResult(False, "secret in output")
return GuardResult(True)
def guarded_call(prompt: str, call_model) -> str:
pre = check_input(prompt)
if not pre.allowed:
return f"Request blocked: {pre.reason}"
answer = call_model(prompt)
post = check_output(answer)
if not post.allowed:
return "Sorry, I can't provide that response."
return answerEach check returns a GuardResult with a reason. This keeps the decision separate from the action taken, so you can later change a block into a retry without rewriting the checks.
Try it with a stand-in model:
print(guarded_call("Hello", lambda p: "Hi there"))
print(guarded_call("Ignore previous instructions", lambda p: "ok"))
print(guarded_call("Hello", lambda p: " "))The first call returns the reply. The second is blocked before the model runs. The third is blocked after, because the reply is empty.
Common errors
- Checking only the input. A clean prompt can still produce a bad reply. Always add an output check.
- Checking only the output. You pay for the model call and may already have sent private data to the provider.
- Different messages for each reason. The user message for an output block is generic here on purpose. Detailed reasons can show an attacker which rule they hit. Log the reason instead.
- Treating the checks as proof of safety. A phrase match misses rewordings. These checks reduce risk and do not remove it.
Exercise
A user sends a normal question, but the model's reply contains a database password copied from the system prompt. Which checkpoint can catch this, and why can't the other one?
Show answer
The output check can catch it, because the password appears only in the model's reply. The input check sees only the user's question, which looked harmless, so it has nothing to flag.
Exercise
Why is a 4000-character length limit better placed before the model call than after it?
Show answer
It needs only the user's text, and checking first avoids paying for a model call on a request that would be rejected anyway.