All posts

LLM output guardrails: what to check before a customer-facing AI answer goes out

LLM output guardrails are a layered set of checks between the model and your user. Which checks are worth their latency, which are theatre, and how to place them.

CodeKrypt 6 min read

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.

RiskExampleTypical check
Unsafe handling downstreamOutput rendered as HTML, run as SQLEncoding, parameterised queries, schema validation
Ungrounded claimAnswer states a policy that is not in your documentsCitation or quote check against retrieved sources
Data leakageAnother customer's record or an internal prompt in the replyTenant check on referenced IDs, pattern scan
Out-of-scope answerA support bot giving legal or medical adviceTopic classifier or allow-listed intents
Commitment you cannot honourBot promises a refund or a dateRule check on phrases and amounts, human approval
Format breakMalformed JSON crashes the UISchema 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:

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:

  1. Run cheap checks inline on every response.
  2. Run model-based checks inline only for high-risk intents (money, legal, health, account changes).
  3. 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:

FailureBetter than a bare refusal
Invalid formatRetry once with the error, then fall back to a safe default
Ungrounded claimRemove the claim or answer "I couldn't find that in our documents"
High-risk commitmentRoute to a person, see human-in-the-loop approval
Out of scopeShort redirect to the right channel
Suspected leakBlock, 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

  1. List the three worst things your AI feature could say or do, and map each to a check from the table above.
  2. Put the deterministic checks in first: schema, encoding, allow-lists.
  3. Add a grounding check only where answers assert facts about your business.
  4. 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.

CodeKrypt avatar

CodeKrypt

Drafted by CodeKrypt's AI writing pipeline, built and maintained by our engineers, under fixed rules on sources and claims. How we write

Frequently asked questions

What are LLM output guardrails?
Checks that run on a model's response before it reaches a user or a downstream system. They can be deterministic (schema, allow-lists, regex), retrieval-based (does the answer match the source), or model-based (a second call that judges the answer).
Do guardrails replace prompt instructions?
No. Instructions lower the rate of bad outputs; guardrails catch the ones that still happen. Anthropic's own hallucination guidance notes that its techniques reduce hallucinations but do not eliminate them, and says to validate critical information.
Should every response go through a second LLM as a judge?
Usually not. A judge call adds latency and cost to every request. Run cheap deterministic checks on everything and reserve model-based checks for high-risk categories, or sample them offline.
What is the most commonly missed guardrail?
Treating model output as trusted when it is rendered or executed. OWASP lists improper output handling as its own LLM risk: output rendered as HTML, run as SQL or passed to a shell can be exploited.

Related reading

Chat on WhatsApp