Guardrails
Check what goes into and comes out of a language model with separate, independent rules, so unsafe input and bad output are stopped before they cause harm.
Description
A model can be steered by malicious input (prompt injection), can leak private data, and can produce wrong, off-topic or unsafe output. Asking it nicely in the prompt to behave isn't enough. Guardrails are separate checks around the model: input guards before the call, output guards after it.
Input guards look for injection attempts, personal data and off-topic requests. Output guards check format (valid JSON, schema), policy (no harmful content, no secrets), grounding (claims supported by sources) and business rules (no discounts above 10%). Many checks are plain code; some use a second, smaller model as a classifier.
System design
Guardrails system design diagram
- User (client).
- Input guard (gateway): injection · PII · topic.
- Language model (language model).
- Output guard (gateway): schema · policy · grounding.
- Send reply / call tool (service).
- Step 1: User → Input guard: message.
- Step 2: Input guard → Language model: clean prompt.
- Step 3: Language model → Output guard: draft output.
- Step 4: Output guard → Send reply / call tool: passed.
- Step 5: Input guard → User: blocked: reason (failure path).
- A user message arrives.
- The input guard checks it for prompt injection, personal data to mask and off-topic requests. Only a clean prompt reaches the model.
- The model's output goes to the output guard, never straight to the user or a tool.
- If it passes the schema, policy and grounding checks, the reply is sent or the tool is called.
- Anything blocked gets a safe response with a reason, and the case is logged for review.
- AWS
- Amazon Bedrock Guardrails; Amazon Comprehend PII detection.
- Azure
- Azure AI Content Safety and Prompt Shields; Azure OpenAI content filters.
- Google Cloud
- Vertex AI safety filters; Model Armor; Sensitive Data Protection.
When to use it
Use it when
- Any user-facing AI feature, especially with untrusted input.
- Model output triggers actions, such as tool calls, emails or database writes, or is shown without review.
- You handle personal or confidential data, or operate under regulation.
Avoid it when
- Never skip them for production user input. But keep internal, low-risk prototypes light: a schema check may be all you need at first.
Pitfalls
- Relying on the system prompt as the only guard. Prompts can be overridden; enforce rules in code.
- Over-blocking legitimate requests, which frustrates users. Measure false positives as well as misses.
- Guarding only the chat text but not tool arguments. The most dangerous output is an action.
- No logging of blocked items, so you never learn what attacks or failures look like.
Alternatives
Evaluator–Optimizer
Choose it when you want output improved through critique and revision, not just allowed or blocked.
Human review
Choose it when stakes are high and volume is low; a person approves before anything is sent or done.
Constrained decoding
Choose it when the main risk is malformed output; structured output modes force valid JSON at generation time.