You have an AI feature that works in the demo. Now it is about to answer real customers, and someone asks the awkward question: what stops it saying something wrong, off-brand or dangerous? LLM output guardrails are the answer, but "add guardrails" is not a plan. Each check costs latency and money, and some protect against risks you do not have. This post covers which output checks are worth building, where they sit, and how to avoid paying for ones that do nothing.
What are LLM output guardrails, and what are they not?
Guardrails are checks that run on the model's response before it reaches a user or another system. They are not the system prompt. A prompt lowers the rate of bad answers; a guardrail catches the ones that happen anyway.
Anthropic's guidance on reducing hallucinations is a good illustration of the split. Its techniques (allow the model to say it doesn't know, ground answers in quotes, require citations) are prompt-level, and the page adds that they "don't eliminate" hallucinations entirely and that critical information should still be validated. That validation step is the guardrail.
Guardrails are also distinct from input defences. Containing prompt injection is an architecture question about what an agent is allowed to do; we cover it in AI agent security. This post is about the other side of the call: the text that comes back.
Which output risks should you actually guard against?
Start from what can go wrong for your product, not from a generic list.
| Risk | Example | Typical check |
|---|
| Unsafe handling downstream | Output rendered as HTML, run as SQL | Encoding, parameterised queries, schema validation |
| Ungrounded claim | Answer states a policy that is not in your documents | Citation or quote check against retrieved sources |
| Data leakage | Another customer's record or an internal prompt in the reply | Tenant check on referenced IDs, pattern scan |
| Out-of-scope answer | A support bot giving legal or medical advice | Topic classifier or allow-listed intents |
| Commitment you cannot honour | Bot promises a refund or a date | Rule check on phrases and amounts, human approval |
| Format break | Malformed JSON crashes the UI | Schema validation, see below |
Rank these by consequence. A wrong tone is an annoyance. A promised refund is a liability. Spend your latency budget on the second kind.
Why do you have to treat model output as untrusted?
Because the user influences it. OWASP's entry on improper output handling says that since prompt input can control model output, passing that output unchecked to other components is like giving users indirect access to extra functionality. Its listed examples include output fed to a shell or eval, generated JavaScript or Markdown rendered in a browser, and generated SQL run without parameterisation. Its prevention advice is blunt: treat the model like any other user, and apply context-aware encoding and parameterised queries.
This is the cheapest, highest-value guardrail, and it is ordinary engineering: escape before rendering, never concatenate model text into queries, never execute it. If your agent calls tools, the arguments it produces get the same scrutiny as a form submission from the internet.
Which checks are deterministic, and why should they go first?
Deterministic checks are fast, free per call and predictable. Put them in front of everything:
- Schema validation for anything structured. If the response must be JSON, parse it and validate types and enums. Where the provider supports constrained or structured output, use it; our structured output post covers the retry-cost trade-off.
- Allow-lists for actions, URLs and product names. If the bot may only link to your domain, check that.
- Pattern scans for things that should never appear: another tenant's ID format, API-key shapes, your system prompt text. For the input side of personal data, see PII redaction.
- Length and language limits, which catch runaway generations and wrong-language replies cheaply.
None of these understands meaning. That is fine: they remove the failures that do not need understanding.
When is a grounding check worth the extra call?
When the answer asserts facts about your business. For retrieval-based products, the strongest check is whether each claim is supported by a retrieved passage. Anthropic's guidance describes the pattern: have the model cite a supporting quote for each claim, and retract any claim with no quote. You can enforce it in code too: require the answer to carry citations, verify each cited passage was actually retrieved, and drop or flag uncited claims. We go deeper in RAG citations.
If the answer is a lookup (order status, plan name), skip the model-based check and compare against the database value directly.
Should you use a second model as a judge?
Sometimes, but not by default. A judge call roughly doubles the number of model calls on the guarded path and adds its own latency; a streamed answer can no longer stream if you must approve it first. Use judges where the cost of a bad answer is high enough to justify them, and otherwise:
- Run cheap checks inline on every response.
- Run model-based checks inline only for high-risk intents (money, legal, health, account changes).
- Sample the rest asynchronously and feed failures into your eval set. Our feedback loop post shows how.
Measure the judge too. A judge that disagrees with a human reviewer on a sample of cases is a guardrail you cannot trust, and it needs its own evaluation set.
What should happen when a guardrail trips?
Decide this up front, per check, because "block" is rarely the only option:
| Failure | Better than a bare refusal |
|---|
| Invalid format | Retry once with the error, then fall back to a safe default |
| Ungrounded claim | Remove the claim or answer "I couldn't find that in our documents" |
| High-risk commitment | Route to a person, see human-in-the-loop approval |
| Out of scope | Short redirect to the right channel |
| Suspected leak | Block, log, alert |
Log every trip with the input, output and the check that fired. Your block rate is a product metric: a rate near zero may mean checks that never fire, and a high rate means the prompt or retrieval is the real problem.
How do you keep guardrails from hurting the experience?
Three habits help. Order checks cheapest first and stop at the first failure. Run independent checks in parallel rather than in sequence. And test false positives as seriously as false negatives, because a guardrail that blocks legitimate answers trains users to stop trusting the feature. Add guardrail cases to your regression suite; see LLM evals in CI.
What to do next
- List the three worst things your AI feature could say or do, and map each to a check from the table above.
- Put the deterministic checks in first: schema, encoding, allow-lists.
- Add a grounding check only where answers assert facts about your business.
- Define the fallback for every check before shipping it, and log each trip.
If you are shipping an AI feature into a product with real customers and want the guardrail layer designed with it rather than bolted on, talk to us about AI SaaS.