Skip to content

LLM guardrails: what they are and how to run them in production

Al Brown
Last updated: Jul 29, 2026

LLM guardrails are programmatic controls that sit around a large language model, validating inputs before the model sees them and filtering outputs before users do, so that applications block prompt injection, PII leaks, and off-topic responses. The OWASP Top 10 for LLM Applications 2025 ranks prompt injection as the number-one risk (LLM01), and guardrails are the primary runtime defence against it.

TL;DR

  • Guardrails are enforcement code that runs outside the model and cannot be talked out of its job, covering input validation, output filtering, topic restriction, PII detection, and tool-permission gating.
  • Prompt injection and sensitive information disclosure sit at the top of OWASP's 2025 Top 10 for LLM applications, and they are the two risk classes guardrails most directly address.
  • Open-source tooling includes NVIDIA NeMo Guardrails, Guardrails AI, Meta's Llama Guard, and Microsoft Presidio, each covering a different slice of the pipeline.
  • Guardrails reduce prompt-injection risk; they do not eliminate it. OWASP's own guidance treats them as one layer in a defence-in-depth design.
  • Every guardrail check emits a verdict, and verdicts are events. Production teams monitor violation rates, false-positive rates, and per-check latency the same way they monitor error rates.

What are LLM guardrails?

LLM guardrails are programmatic input and output controls wrapped around a large language model. They validate user input before it reaches the model, filter or rewrite model output before it reaches the user, and gate what actions the model can trigger. Guardrails run as code outside the model, so a malicious prompt cannot disable them.

The category covers five recurring control types: input validation (reject or sanitise suspicious prompts), output filtering (block toxic, off-policy, or malformed responses), topic restriction (keep a support bot answering support questions), PII detection (catch names, emails, and card numbers moving in either direction), and tool-permission gating (decide which functions an agent may call, with which arguments). NVIDIA's NeMo Guardrails toolkit formalises a similar split into input, output, dialog, retrieval, and execution rails.

Why do LLM applications need guardrails?

LLM applications need guardrails because models follow instructions from anyone who can get text in front of them. The OWASP Top 10 for LLM Applications 2025 ranks prompt injection as LLM01, its top risk, and sensitive information disclosure as LLM02. Guardrails are the runtime layer that catches both before they become incidents.

The threat model is broader than deliberate attackers. Users paste customer records into chat boxes, retrieval pipelines pull in web pages containing hidden instructions (indirect prompt injection), and models occasionally emit training-data fragments or confident falsehoods on regulated topics. Each failure mode has a different cost: a jailbroken support bot is embarrassing, a leaked medical record is a GDPR or HIPAA event, and an agent that emails the wrong customer is an unrecoverable side-effect. Unsanctioned AI usage compounds the problem: teams adopting models outside governance, a pattern known as shadow AI, rarely deploy any of these controls at all.

How do LLM guardrails work?

Guardrails work as a pipeline of checks around each model call. Input checks run on the user's prompt and any retrieved context; the model generates only if they pass. Output checks run on the completion before it is returned. Each check produces a verdict (allow, block, rewrite, or flag) plus metadata for logging.

The checks themselves span a range of mechanisms with very different costs:

Guardrail typeWhat it catchesWhere it runsTypical latency cost
Rule/regex validationMalformed input, banned strings, schema violations in outputInput and outputSub-millisecond
PII detection (NER-based)Names, emails, card numbers, health identifiersInput and outputMilliseconds to tens of ms
Classifier modelsToxicity, jailbreak patterns, off-topic driftInput and outputTens of milliseconds
LLM-based judges (e.g. Llama Guard)Nuanced policy violations across a safety taxonomyInput and outputA full model inference (hundreds of ms)
Tool-permission gatingUnauthorised or malformed function calls by an agentBetween model and toolsSub-millisecond to ms

Meta's Llama Guard paper (December 2023) established the LLM-as-safeguard pattern: a fine-tuned model that classifies both prompts and responses against a safety-risk taxonomy. It is the most flexible check type and the most expensive: every guarded call pays an extra inference.

How do you add guardrails to an LLM application?

You add guardrails by wrapping model calls in a validation layer rather than modifying the model. Most teams adopt an open-source framework that intercepts requests and responses, define policies declaratively, and start with input-side injection screening and output-side PII detection, then expand coverage as real traffic reveals gaps.

The main open-source frameworks cover the pipeline from different angles: NeMo Guardrails (NVIDIA) defines conversational rails in its Colang language, Guardrails AI composes reusable validators over inputs and outputs in Python, Llama Guard supplies the judge model, and Microsoft Presidio handles PII recognition and redaction. Evaluate any framework against the criteria that matter in production: which pipeline stages it covers (input, output, retrieval, tool calls), whether policies are declarative and version-controllable, the latency budget per check, whether verdicts are emitted as structured events you can monitor, and how it handles streaming responses, where output can only be validated in chunks.

A policy definition is typically a config fragment of this shape (illustrative):

rails:
  input:
    - check: injection_screen
      action: block
    - check: pii_detect
      entities: [EMAIL, CREDIT_CARD]
      action: redact
  output:
    - check: topic_restriction
      allowed: [billing, shipping]
      action: block

Guardrails vs system prompts: what's the difference?

Guardrails are enforcement; system prompts are instruction. A system prompt asks the model to behave ("do not discuss competitors; never reveal internal data"), and the model usually complies. A guardrail checks every input and output in code that runs outside the model, so compliance is guaranteed for whatever the check can detect.

The distinction matters because prompt injection targets exactly the gap between the two. A system prompt lives in the same context window as attacker-controlled text, and the model has no reliable way to privilege one instruction over another, which is why OWASP classifies injection as an architectural weakness rather than a bug. A guardrail, by contrast, cannot be persuaded, because it is not a participant in the conversation. In practice the two are complementary: system prompts shape the model's default behaviour cheaply, and guardrails enforce the subset of rules whose violation is unacceptable. Any rule you would be uncomfortable seeing broken in production belongs in a guardrail.

How do you monitor guardrail violations in production?

Monitor guardrails by treating every check verdict as a structured event: which check fired, on which request, with what decision, confidence, and latency. Stream those events into the same store as your traces, then track violation rates, false-positive rates, and per-check latency over time, alerting on drift rather than individual hits.

A verdict event is a wide, high-cardinality record, one per check per request:

{
  "timestamp": "2026-07-28T09:14:07Z",
  "trace_id": "4bf92f3577b34da6a3ce929d0e0e4736",
  "check": "pii_detect",
  "stage": "output",
  "verdict": "redact",
  "entities": ["EMAIL"],
  "confidence": 0.97,
  "latency_ms": 12,
  "model": "gpt-4o",
  "app_version": "2.31.0"
}

Because verdicts carry the trace ID, they join directly against LLM spans, following the telemetry model described in ClickHouse's guide to LLM observability and formalised by the OpenTelemetry GenAI semantic conventions. Five metrics cover most of what production teams watch:

  • Violation rate per check, segmented by app version and model: a step change after a deploy usually means a prompt or model regression, not an attack.
  • False-positive rate, measured from sampled human review: an over-blocking PII check quietly destroys product quality.
  • Latency cost per check at p50/p95: guardrails sit on the critical path of every request.
  • Verdict drift: rising block rates on one topic or tenant flags either an attack campaign or a policy misfit.
  • Coverage: the fraction of model calls that passed through each rail; gaps are where incidents happen.

At scale this is an analytical query problem: "false-positive rate by check and app version, last 30 days" is an aggregation over millions of wide events.

Monitoring guardrail verdicts with Langfuse

Langfuse, the open-source LLM observability platform that ClickHouse acquired in January 2026, records guardrail verdicts as scores attached to the exact trace they protected: the verdict that blocked a response sits on the same trace as the LLM call, its inputs, and its outputs, so drilling from a spiking check to the offending prompts is a click, not a cross-store join.

The five metrics above map onto that model directly. Verdicts recorded as scores aggregate by check, application version, and model in the dashboards, and false-positive rates come from sampled human annotation in the same interface. Because Langfuse stores every event in ClickHouse rather than sampling, drift analysis like "block rate by check and app version, last 30 days" runs over the complete record.

Verdict monitoring matters even more for the multi-step systems covered in the guide to AI agent observability. To see guardrail and LLM telemetry side by side, record verdicts as scores alongside your traces; Langfuse is open source and free to self-host.

FAQ

How do I prevent prompt injection in an LLM application?

You cannot fully prevent it; you reduce and contain it. Screen inputs with injection classifiers, separate retrieved content from instructions, validate outputs before acting on them, and gate agent tool calls with least-privilege permissions. No single control eliminates it, so layer defences and monitor for what gets through.

How do I detect and block PII in LLM outputs?

Run an entity-recognition check, such as Microsoft Presidio, over every completion before it is returned, configured for the identifier types your compliance scope requires (emails, card numbers, health data). Choose redact rather than block where possible, log every detection as an event, and sample detections for human review to measure false-positive rates.

Do LLM guardrails add latency?

Yes, and the cost varies by mechanism, from under a millisecond for regex and schema checks up to a full extra model inference for LLM-based judges. Teams run cheap checks on everything and reserve judge models for high-risk routes or sampled traffic.

Do guardrails work for AI agents?

Yes, and agents need one extra control type: tool-permission gating. Because agents take actions with side-effects (writing records, sending emails, calling APIs), output filtering alone is insufficient. Gate each tool call against an allowlist and argument schema, and record every gating decision as an event tied to the agent's trace.


Share this resource

  • Y Combinator icon
  • X icon
  • Bluesky icon
  • Facebook icon
  • LinkedIn icon

Subscribe to our newsletter

Stay informed on feature releases, product roadmap, support, and cloud offerings!

More like this

What is synthetic monitoring?

Al Brown • Last updated: Jul 29, 2026

Synthetic monitoring runs scripted probes (HTTP checks, API transactions, browser journeys) on a schedule from controlled locations to catch outages before users do.

Continue reading ->

What is SRE? Site reliability engineering explained

Al Brown • Last updated: Jul 29, 2026

Site reliability engineering (SRE) applies software engineering to operations problems, using SLOs, error budgets, a 50% toil cap, and blameless postmortems to keep systems reliable.

Continue reading ->

What is shadow AI? The governance gap in AI adoption

Al Brown • Last updated: Jul 29, 2026

Shadow AI is the use of AI tools and models inside an organization without IT or governance approval. It drives data leakage, unbudgeted spend, and compliance exposure, and detecting it is an observability problem.

Continue reading ->