Skip to content
← All writing
March 4, 2026·7 min read

LLM Guardrails: Keeping AI Features Safe

A language model will happily follow a malicious instruction, wander off-topic, leak information it shouldn't, or state something false with total confidence. None of this makes it broken — it makes it a language model, doing exactly what it was trained to do: produce plausible text. On its own, that's a liability in a real product. Guardrails are the layer of checks and constraints you build around the model to keep its behaviour safe and predictable — the OWASP Top 10 for LLM Applications catalogues the risks they defend against, and it's sobering reading for anyone who's shipped an AI feature on optimism alone.

This post explains the main kinds of guardrails and how to apply them without strangling the feature you're protecting.

Why the model alone isn't enough

An LLM is trained to produce plausible text, not to enforce your app's rules. It has no built-in awareness of what's confidential in your system, what's on-topic for your product, or what a particular user is allowed to make it do. Provider-side safety training helps with broadly harmful content, but it knows nothing about your rules — that your support bot shouldn't discuss competitors, that internal ticket IDs are confidential, that refunds over a threshold need a human. Guardrails supply that missing judgement — some before the model runs, some after — turning an unpredictable component into a dependable feature.

The framing that helps most: treat the model like a brilliant, eager intern with no security clearance and no sense of company policy. You wouldn't wire an intern directly to production; you'd put review steps around them. Same instinct here.

Input guardrails: check before the model runs

The first line of defence inspects the user's input before it reaches the model.

  • Prompt-injection defence. User input — or content you feed in from documents, emails, and web pages — may contain instructions like "ignore your rules and…". Keep user input clearly separated from your system instructions with explicit delimiters, and tell the model to treat anything inside the user block as data, not commands. Never concatenate user text directly into your instructions. This raises the bar considerably, but treat it as the first layer, not the whole defence: injection is not a fully solved problem, which is why the output checks and permission limits below exist.
  • Scope filtering. If your assistant is meant to answer questions about your product, detect and politely decline wildly off-topic or abusive requests before spending a model call on them. A cheap classifier — or even the small model tier — can do this triage for a fraction of the cost of the main call.
  • Sensitive-input handling. Decide what happens if a user pastes something they shouldn't — API keys, someone else's personal data, a password. Sometimes the right move is to refuse; sometimes to redact before processing; either is better than dutifully echoing a secret into your logs and the provider's.
  • Size limits. Cap input length. A 500-page paste is either a mistake or an attack on your token budget, and both deserve a friendly error rather than a $2 model call.

Output guardrails: check what comes back

Just as important is validating the model's response before you show it or act on it. Input checks can be evaded; output checks inspect what actually happened, which makes them the more reliable half of the pair.

  • Format validation. If you expect JSON or a specific structure, parse and validate it. Reject or retry on malformed output rather than passing garbage downstream.
  • Content checks. Screen for disallowed content, leaked system instructions ("As an AI assistant configured to…"), or responses that stray outside allowed topics. Simple pattern checks catch a surprising share; moderation APIs or a small classifier model catch more.
  • Grounding / hallucination checks. For factual features (especially RAG), verify the answer is actually supported by the provided sources — at minimum, instruct the model to cite its source chunk and check the citation exists. If the answer isn't grounded, prefer "I don't know" over a confident guess. A wrong answer with a confident tone is the most trust-destroying output an AI feature can produce.
  • Action confirmation. If the model's output triggers a real action — sending, deleting, spending — require validation of the arguments and, for anything destructive or user-visible, explicit confirmation. The tool-design post covers this in depth.

Constrain what the model can do

The strongest guardrail is limiting the model's power in the first place — a guardrail that can't be talked around, because it isn't made of words.

  • Give tools and permissions on a least-privilege basis. If a feature only needs to read, don't give it the ability to write. If it needs to write, scope the write to the current user's data at the API layer, not in the prompt.
  • Validate every tool argument the model produces as untrusted input before executing — because via prompt injection, tool arguments are partially attacker-controlled.
  • Cap loops, retries, and token budgets so a confused model can't run away with your compute bill. A hard iteration limit turns an infinite loop into a logged incident.

Prompts are persuasion; permissions are physics. When a rule truly matters, enforce it where the model can't negotiate.

Handle refusals and failures gracefully

Guardrails will sometimes block a response, and the model will sometimes fail. Design for it:

  • Have a safe fallback: a polite refusal, a default value, or a "couldn't handle that" state — never a crash or corrupted data.
  • Make refusals helpful, not robotic. "I can only help with questions about your orders and account" tells a legitimate user what to do next; "I cannot assist with that request" tells them your product is broken.
  • Fail closed on anything risky: when a check errors out or times out, do the safe thing rather than the permissive one.

Log, monitor, and improve

Guardrails aren't set-and-forget. Log what gets blocked and why — with enough context to review the decision later. Watch the blocked-request rate: a sudden spike means either an attack or a guardrail regression, and you want to know which within hours, not weeks. Feed real incidents back into your rules and your evaluation set, so yesterday's bypass becomes tomorrow's regression test. Adversarial users are creative, and their creativity is free red-teaming if you're logging it.

Before launch, spend an honest afternoon attacking your own feature: injection attempts, off-topic bait, requests for the system prompt, tool-abuse attempts. Whatever you find in that afternoon, users would have found in the first week.

Don't over-constrain

A caution the other way: too many aggressive guardrails make a feature frustrating, refusing reasonable requests and feeling broken. Users don't distinguish "safely declined" from "doesn't work." The goal is calibrated safety — block the genuinely harmful and out-of-scope while letting legitimate use flow. So test your guardrails against real, benign inputs too, not just attacks, and track false positives as seriously as misses. A guardrail that blocks 5% of legitimate requests is a bug with a safety costume on.

Summary

Guardrails are what make an LLM safe to put in front of users: input checks that catch injection and off-topic or sensitive requests, output checks that validate format, content, and grounding, and — most importantly — hard limits on what the model is allowed to do, enforced in code where no prompt can override them. Pair them with graceful fallbacks, thorough logging, and ongoing tuning against real traffic, and calibrate carefully so you stop the harmful without blocking the helpful. That layer of judgement around the model is the difference between an impressive demo and a feature you can actually trust in production.

Further reading