News / Learn / Production & LLMOps
What are guardrails and moderation?
You'll be able to add input and output checks that catch unsafe or off-policy content in an AI feature.
In plain English
Guardrails are checks that run before and after a model call. They stop bad input from reaching the model and stop bad output from reaching the user. Moderation is one kind of guardrail. It sorts text into categories such as harassment, violence, or self-harm, so your app can block or flag it.
Why it matters
A model will sometimes produce content you never want to show. Users also try to push a model into misbehaving. Guardrails give you a fixed layer of rules that does not depend on the model behaving well every time. They also make your product easier to defend when something goes wrong.
How it works
Think of guardrails as checkpoints on both sides of the model. The input checkpoint looks at what the user sent. It can reject messages that are too long, strip personal data, or flag attempts to override your instructions. The output checkpoint looks at what the model returned before the user sees it. It can check for banned topics, leaked internal text, or malformed structure.
Moderation is a classifier that scores text against categories of harm. You can run a dedicated moderation model or a general model with a strict prompt. Either way, the classifier returns a label or a score. Your code then decides what to do with that result. Choose the threshold for each category. A low threshold blocks more and annoys more users. A high threshold lets more through.
Rules-based checks are also useful. Regular expressions, length limits, allow-lists for tool names, and schema validation are cheap and predictable. Use them first, then add model-based checks where rules cannot cover the case. Layering the two gives you coverage without relying on one method.
When a check fails, decide the response ahead of time. You can return a fixed refusal, ask the user to rephrase, retry with a safer prompt, or send the item to a human. Log every block with enough context to review it later, but store only what your privacy rules allow.
A concrete example
A simple input and output check that runs around a model call. The moderate() and call_model() functions stand in for your own code.
BLOCKED_MESSAGE = "Sorry, I can't help with that request."
MAX_INPUT_CHARS = 4000
def check_input(text: str) -> bool:
if len(text) > MAX_INPUT_CHARS:
return False
result = moderate(text) # returns {category: score}
return all(score < 0.8 for score in result.values())
def check_output(text: str) -> bool:
if "INTERNAL" in text: # never leak internal markers
return False
result = moderate(text)
return all(score < 0.8 for score in result.values())
def answer(user_text: str) -> str:
if not check_input(user_text):
return BLOCKED_MESSAGE
reply = call_model(user_text)
if not check_output(reply):
return BLOCKED_MESSAGE
return replyA short sketch to show the idea. Check the official docs of the tool you use.
What goes wrong
- Only checking the input Models can produce harmful or wrong output from safe input. Check the output too, especially before it reaches users or triggers an action.
- One threshold for every category Different harms need different tolerance. Tune the threshold per category and test with real examples from your product.
- Trusting the model to police itself A system prompt that says 'don't do X' is a request, not a guarantee. Put a separate check in code that does not depend on the model's compliance.
- No plan for a blocked request Decide in advance what the user sees and what your team sees. Log blocks with enough detail to review false positives and misses.
Going deeper
Learn about prompt injection and how it bypasses input checks. Then study red-teaming, where you test your guardrails with adversarial inputs. Finally, look at evals that measure how often your guardrails block correct content and how often they miss bad content.
FAQ
Are guardrails the same as moderation?
No. Moderation is one type of guardrail that classifies content by harm category. Guardrails also include rules such as length limits, format checks, and blocking calls to tools you have not approved.
Can a moderation model catch everything?
No classifier is perfect. It will miss some harmful content and flag some safe content. Combine it with rules, human review for edge cases, and regular testing.
Should I run guardrails before or after the model?
Both. Input checks protect the model and your costs. Output checks protect the user and your brand. Each catches different failures.
Will guardrails slow my app down?
They add some latency, especially model-based checks. Rules-based checks are nearly instant. Measure the cost and decide which checks must run in the request path and which can run after.
What do I do when a guardrail blocks a real user?
Show a clear, polite message that explains what to change. Log the event, review it, and adjust the rule or threshold if it was a false positive.
Written with AI by CapyLaunch. Spotted a mistake? Email hello@capylaunch.com.