AGENSPHERE/ JOURNAL
← JOURNAL
J-047NOTE3 MIN READ

Guardrails belong in code, not in the prompt

LLM guardrails are the checks around a model that stop unsafe inputs, wrong outputs and forbidden actions. Instructions in a prompt are a request, not a guardrail. Where real guardrails go in the request path, and which ones every production system needs.

IN SHORT
  • LLM guardrails are checks that run outside the model: on what goes in, what comes out and what the system is allowed to do. A sentence in the prompt is a request the model may ignore.
  • Put guardrails at four points: input checks, retrieval and permission filters, output validation, and action limits enforced in the tools.
  • The most important guardrails are deterministic: schemas, permission checks, allow-lists and limits. Model-based classifiers add coverage for fuzzy cases such as tone or topic.
  • Every guardrail needs a defined response when it trips, and a log, so you can see how often it fires and why.

Ask a team how their AI system avoids saying or doing the wrong thing and the answer is often a paragraph in the system prompt: "Never reveal customer data. Never promise refunds. Always be polite." Those lines help, but they are not LLM guardrails. They are requests to a probabilistic system, and the right input will get around them. Real guardrails run outside the model, in code.

01What are LLM guardrails?

Checks around a language model that constrain what goes in, what comes out and what the system may do, enforced by software that the model cannot talk its way past. They are the difference between "the model usually behaves" and "the system cannot do this".

A prompt asks the model to behave. A guardrail makes misbehavior impossible or visible.

02Where do guardrails go in an LLM application?

At four points in the request path:

PointWhat it checksTypical mechanism
InputIs this request in scope? Does it contain secrets, personal data or an injection attempt?Rules, PII detection, a small classifier
RetrievalMay this user see these documents?Permission filter before ranking
OutputIs the response well formed, grounded and allowed?Schema validation, citation checks, policy classifier
ActionIs this tool call permitted, within limits, and approved if needed?Tool permissions, limits, idempotency, human approval

03Which guardrails does every production system need?

Deterministic ones first, because they are cheap, fast and certain:

Model-based ones for fuzzy cases: an off-topic or abusive request, a reply in the wrong tone, an answer that is not supported by its sources. A small, fast model or a dedicated classifier can check these on every response.

guardrails/pipeline.py
def answer(user, question):
    check_input(question)                         # size, PII, injection heuristics, topic scope
    passages = retrieve(question, allowed_for=user)
    draft = model.generate(question, passages, schema=Answer)
    Answer.model_validate(draft)                  # deterministic: shape and types
    if not cites_only(draft, passages):           # deterministic: every citation is a retrieved passage
        return fallback("I could not find that in the documents you can access.")
    if policy_classifier(draft).flagged:          # model-based: tone, promises, restricted topics
        return escalate(user, question, draft)
    return draft

04What should happen when a guardrail trips?

Decide in advance, per guardrail: refuse with a clear message, retry once with the error, fall back to a safe answer, or hand over to a person. And log it. The rate at which each guardrail fires is one of the most useful signals you have: a spike usually means a prompt change, a new kind of user request or an attack (see trace and cost every model call).

05Do guardrails stop prompt injection?

They limit the damage. Injection cannot be fully filtered out of input text, so the real defense is that injected instructions gain no authority: tools are scoped to the user, actions are limited, and content from documents or the web is treated as data (see prompt injection is an input trust problem).

06How do you test LLM guardrails?

Keep a red-team set alongside your normal evaluation set: requests that should be refused, documents with hidden instructions, attempts to reach other users' data, and outputs that should fail validation. Run it on every prompt and model change (see ship a prompt change with an eval).

07Where to go next

If you already run an AI system and are unsure what protects it, an AI Audit checks every one of these points. For the full picture of agents that act safely, see AI agent development.

Is your AI live, but fragile, slow or expensive?DESCRIBE YOUR SYSTEM →