AI Agents

Guardrails Guide

Implementing production guardrails for LLM systems - NeMo Guardrails, input/output validation, dialog flows, policy enforcement, and when programmatic rails beat prompt-only controls.

50 min readIntermediateLast reviewed: 16 July 2026

Quick Summary

Guardrails are programmatic checks on LLM inputs, outputs, and tool actions that enforce policy outside the model's probabilistic behavior.

One Analogy

Like airport security and baggage screening: prompts ask passengers to behave; rails actually scan and stop what violates policy.

Engineering Rule

Put hard stops in code - schema, allowlists, and block reasons with metrics - never rely on 'please be safe' alone.

TL;DR

  • Guardrails are programmatic policy enforcement on LLM inputs and outputs - not synonyms for "be safe" in a system prompt.

  • NeMo Guardrails (Colang) lets you define dialog flows, topical boundaries, and tool restrictions as executable rules rather than hopeful instructions.

  • Validate outputs structurally - JSON schema, regex, NLI faithfulness checks, PII/secret scanners - before returning responses to users or downstream systems.

  • Input rails catch injection and off-topic requests early; output rails catch hallucinations, policy violations, and format errors before they ship.

  • Guardrails add latency and can over-block - tune thresholds, log false positives, and provide graceful fallbacks instead of silent failures.

Why This Matters

System prompts ask models to behave. Guardrails verify they did. In production, verification is non-negotiable when outputs drive customer-facing actions, legal disclosures, medical triage, or financial recommendations.

A model that occasionally ignores "only answer from context" creates liability. A model that returns valid JSON 94% of the time breaks integrations the other 6%. Guardrails convert probabilistic behavior into bounded, auditable interfaces.

For agents with tool access, guardrails are the difference between "the model decided to email all customers" and "the action was blocked and logged."

The Problem Guardrails Solve

Prompts alone fail because:

Models comply inconsistently. Temperature, context length, and adversarial input change behavior.

Integrations need guarantees. APIs expect schema-valid JSON, enum values, and max lengths - not prose apologies.

Policies are multi-dimensional. Block PII, enforce topic boundaries, require citations, refuse medical advice - expressing all of this reliably in one prompt is unmaintainable.

Regulatory and brand requirements need evidence. Auditors want logs of what was blocked and why, not "we told GPT to be careful."

Guardrails implement testable policies outside the model, with metrics on block rates and false positives.

How We Got Here

Safety moved from prompt instructions to layered, measurable controls:

Diagram: From prompts alone to layered rails

flowchart LR
    A[System prompt rules] --> B[Moderation APIs]
    B --> C[Schema validators]
    C --> D[NeMo / Colang flows]
    D --> E[Tool rails + HITL]

Major components and how control or data moves between them.

Era Pattern Gap
Prompt-only "Never discuss X" Inconsistent compliance
Hosted moderation Toxicity / abuse classifiers Narrow policy surface
Structural validators JSON Schema, Pydantic Doesn't cover dialog policy
Dialog rails NeMo Guardrails / Colang Needs ops + false-positive tuning
Agent rails Tool allowlists + HITL Complements, doesn't replace, evals

Public building blocks: NVIDIA NeMo Guardrails, Guardrails AI, Llama Guard, OpenAI/Azure moderation APIs.

What Are AI Guardrails?

AI guardrails are automated checks and control flows applied before and after LLM inference (and sometimes between agent steps) to enforce safety, quality, and format constraints.

Categories:

Type Function Examples
Input rails Filter or transform user input Injection detection, topic classifier, PII stripping
Output rails Validate model response Schema validation, toxicity, faithfulness, secret scan
Dialog rails Control conversation flow NeMo Colang flows, escalation to human
Tool rails Constrain agent actions Allowlist, argument bounds, approval gates
Retrieval rails Constrain knowledge use Require min similarity score, block uncited claims

NeMo Guardrails (NVIDIA) is an open-source toolkit using Colang - a modeling language for dialog flows - to orchestrate when to call the LLM, which model, when to refuse, and when to run custom actions.

Guardrails differ from AI Security breadth - guardrails are the enforcement mechanism; security is the overall threat model.

How Guardrails Work

Pipeline Placement

Diagram: Input, process, and output rails

flowchart TD
    U[User input] --> IR[Input rails]
    IR -->|block| FB[Fallback]
    IR -->|pass| LLM[LLM / agent step]
    LLM --> OR[Output rails]
    OR -->|retry| LLM
    OR -->|block| FB
    OR -->|pass| Out[Deliver]
    LLM -->|tool proposed| TR[Tool rails]
    TR -->|deny| FB
    TR -->|allow| Exec[Execute tool]
    Exec --> LLM

Input rails run before expensive inference. Cheap classifiers and rules reject or sanitize early.

Output rails run on model text before delivery. Can trigger retry with correction prompt ("your JSON was invalid, fix keys") - cap retries at 2.

Tool rails validate proposed tool name and arguments before execution - orthogonal to model intent.

NeMo Guardrails Architecture

NeMo Guardrails config typically includes:

  • config.yml - models, rails to enable, general settings.

  • rails.co - Colang flows defining user intents, bot responses, and branching.

  • Custom actions - Python functions for retrieval, API calls, validation.

Colang example (conceptual):

define user ask about competitor
  "what do you think of [Competitor]"
  "is [Competitor] better"

define bot refuse competitor comparison
  "I can only discuss our products. I can't compare competitors."

define flow competitor question
  user ask about competitor
  bot refuse competitor comparison

Flows can invoke LLM only when allowed, call retrieval for grounded answers, or escalate:

define flow medical disclaimer
  user ask medical question
  bot offer disclaimer
  bot suggest professional advice

NeMo integrates with LangChain and standalone deployments. Rails run in a guardrails server or embedded in your app.

Colang Policy as Code

Treat Colang files like application code: PR review, unit tests for flows, staging deployment before production. Example test pattern - assert competitor flow never calls RAG:

def test_competitor_question_refuses_without_rag(nemo_rails):
    result = nemo_rails.generate("Is CompetitorX better than you?")
    assert "can't compare" in result.lower()
    assert result.get("rag_invoked") is False

Version Colang alongside prompt_version in observability spans. When a rail blocks unexpectedly after deploy, diff Colang changes first - not the LLM model.

Output Validation Patterns

  1. Structural - Pydantic/JSON Schema, Guardrails AI validators.

  2. Semantic - NLI model checks answer entailed by retrieved context (RAG faithfulness).

  3. Policy - Regex blocklists, Llama Guard toxicity scores.

  4. Business - Required disclaimer strings, citation count minimum.

Architecture

Diagram: Guardrail pipeline architecture

flowchart TB
    Pol[Policy store / Colang] --> Ex[Rail executor]
    Ex --> Clf[Classifier models]
    Ex --> Val[Schema / NLI / scanners]
    Ex --> FB[Fallback generator]
    Ex --> Met[Metrics + audit]
    Ex --> Esc[Human escalation]

Major components and how control or data moves between them.

Component Role
Policy store Versioned rules (Colang, YAML, code)
Rail executor Runs checks in defined order with timeouts
Classifier models Small/fast models for input/output scoring
Fallback generator Static or template responses on block
Metrics + audit Block reason, latency, false positive feedback
Human escalation queue Low-confidence or repeated blocks - see HITL

Run guardrails close to the orchestrator - same process or sidecar with low latency. Do not call a slow external service synchronously unless cached.

Step-by-Step Flow

Step 1: Define policies with owners. Legal owns disclaimer text; eng owns schema; security owns injection rules.

Step 2: Implement input rails. Injection score, max length, allowed languages, off-topic classifier.

Step 3: Generate with constrained decoding where possible. OpenAI structured outputs, grammar-guided generation for JSON.

Step 4: Run output rails in sequence. Fast checks first (regex, length), expensive last (NLI faithfulness).

Step 5: On failure, decide: retry, fallback, or escalate. Log reason code. Never return raw blocked content.

Step 6: For agents, rail each tool call before execution - schema + permission + rate limit.

Step 7: Emit metrics - guardrail_block_total{reason=...}, latency histogram.

Step 8: Weekly review false positives from user feedback and thumbs-down.

Real Production Example

RAG support bot with NeMo Guardrails-style flow and Python output validation:

from pydantic import BaseModel, ValidationError
from typing import Optional
import json

class SupportAnswer(BaseModel):
    answer: str
    citations: list[str]  # chunk IDs
    confidence: float

class GuardrailPipeline:
    def __init__(self, nemo_rails, faithfulness_checker, secret_scanner):
        self.rails = nemo_rails  # NeMo Guardrails LLMRails instance
        self.faithfulness = faithfulness_checker
        self.secret_scanner = secret_scanner

    async def input_rails(self, user_message: str, ctx) -> tuple[bool, str, Optional[str]]:
        # NeMo: run input rails (injection, topic)
        result = await self.rails.generate_async(
            messages=[{"role": "user", "content": user_message}],
            config={"rails": ["input"]},  # input-only pass
        )
        if result.get("blocked"):
            return False, result.get("fallback", "I can't help with that."), "input_policy"
        return True, user_message, None

    async def output_rails(
        self,
        query: str,
        raw_response: str,
        context_chunks: list[str],
        max_retries: int = 2,
    ) -> tuple[bool, str, Optional[str]]:
        text = raw_response
        for attempt in range(max_retries + 1):
            if self.secret_scanner.contains_secret(text):
                return False, "I couldn't produce a safe response.", "secret_leak"

            try:
                parsed = SupportAnswer.model_validate_json(text)
            except ValidationError:
                if attempt < max_retries:
                    text = await self._repair_json(text)
                    continue
                return False, "Something went wrong formatting the answer.", "schema_invalid"

            if len(parsed.citations) < 1:
                return False, "I don't have enough sources to answer confidently.", "no_citation"

            if not self.faithfulness.is_supported(parsed.answer, context_chunks):
                return False, "I couldn't verify that answer against our docs.", "faithfulness_fail"

            if parsed.confidence < 0.6:
                return False, "I'm not confident in that answer - a human agent can help.", "low_confidence"

            return True, parsed.answer, None

        return False, "Unable to generate a valid response.", "max_retries"

    async def handle(self, user_message: str, ctx, rag_pipeline):
        ok, msg, reason = await self.input_rails(user_message, ctx)
        if not ok:
            metrics.increment("guardrail_block", tags={"reason": reason})
            return msg

        rag_result = await rag_pipeline.query(msg, ctx)
        ok, answer, reason = await self.output_rails(
            msg, rag_result["structured_json"], rag_result["chunk_texts"]
        )
        if not ok:
            metrics.increment("guardrail_block", tags={"reason": reason})
            return answer
        return answer

NeMo Colang would handle competitor questions and medical disclaimers before RAG runs; Python rails enforce schema and faithfulness on structured output.

Design Decisions

Decision Option A Option B When to choose
Framework NeMo Guardrails (Colang) Custom Python + classifiers NeMo for dialog policy complexity; custom for simple schema/topic checks
Block vs retry Hard block Retry with repair prompt Retry for format errors; block for policy/toxicity
Faithfulness NLI model LLM-as-judge NLI faster/cheaper; LLM judge for nuanced RAG
Deployment Embedded in API Sidecar service Sidecar when multiple products share policies
Strictness High (more blocks) Low (more passes) High for regulated domains; tune with false positive feedback

Comparisons

Guardrails vs HITL

Dimension Guardrails Human-in-the-Loop
Who decides Automated policy / classifiers Named human reviewer
Speed Milliseconds–hundreds of ms Minutes to hours
Best for Schema, toxicity, allowlists, injection Judgment, edge cases, irreversible writes
Failure mode False positives / over-blocking Latency and reviewer fatigue

Use rails for mechanical policy; use HITL when organizational judgment is required. They stack: rails can escalate to HITL.

Input vs output rails

Dimension Input rails Output rails
When Before inference After generation (or between agent steps)
Examples Injection detection, topic block, PII strip Schema, faithfulness, secret scan, toxicity
Cost effect Saves model spend on bad requests May trigger capped retries

Guardrails vs evals

Dimension Guardrails LLM Evaluation
Timing Online, per request Offline / CI / sampling
Purpose Enforce policy in production Measure quality and regressions
Signal Block / pass / reason code Task success, faithfulness, regressions

Rails without evals drift; evals without rails don't stop bad outputs in prod. Ship both.

Common Mistakes

  1. Guardrails only in the system prompt. Not guardrails - suggestions.

  2. No fallback message. User sees empty response or generic 500.

  3. Unbounded retry loops. Each retry doubles cost and latency. Cap at 2.

  4. Ignoring false positives. Support team disables rails. Review weekly.

  5. Running heavy NLI on every request. Sample or run only when confidence low.

  6. Blocking without audit logs. Cannot tune policies or investigate incidents.

  7. Same rails for internal and external users. Internal tools need lighter touch; use role-based rail profiles.

Where It Breaks Down

Ambiguous policy boundaries. "General wellness tips" vs "medical advice" - classifiers disagree; human escalation required.

Multilingual inputs. English-only rails miss Spanish injection.

Adversarial targeting of rails. Attackers probe what gets blocked to craft bypasses. Rotate rules; monitor novel inputs.

Latency stacks. Five sequential checks add 300ms+. Parallelize independent rails; cache classifier results for repeated patterns.

Over-automation. Some edge cases need human judgment - build escalation paths, not infinite rails.

Decision tree: how much guardrail depth?

Decision tree: Choosing guardrail depth

flowchart TD
    A[External users or irreversible actions?] -->|No| B[Log + light moderation]
    A -->|Yes| C[Input + output rails]
    C --> D{Tools can write / spend / send?}
    D -->|Yes| E[RBAC + HITL on high-risk tools]
    D -->|No| F[Schema validation + PII filters]
    E --> G[Cap retries; escalate on ambiguity]
    F --> G

Match rail cost to blast radius - internal prototypes should not ship five sequential classifiers.

When NOT to Use Guardrails

Skip heavy rail stacks when:

  1. Internal prototypes with no side effects - start with logging; add rails before external users.
  2. You only need format guarantees - constrained decoding / structured outputs may suffice for JSON.
  3. Policies are purely judgment calls - escalate to HITL instead of brittle classifiers.
  4. Latency budget cannot absorb checks - then redesign (cache, parallelize, cheaper classifiers) rather than skip safety on high-risk paths.
  5. You confuse rails with auth - guardrails do not replace authentication, RBAC, or network controls (AI Security).

Running in Production

Best Practice

Best Practices - Version policies like code, fail closed when rails are down, log every block reason, and CI-test must-block / must-pass golden sets.

Dimension Consideration
Scaling Stateless rail workers scale horizontally. Cache classifier models in memory.
Latency Budget 50–200ms for rails total. Run cheap checks first; parallelize where possible.
Cost Faithfulness NLI + retry loops add tokens. Monitor cost per blocked vs passed request.
Monitoring Block rate by reason, false positive rate (user override), p99 rail latency.
Evaluation Golden set with policy violations that must block; valid queries that must pass. CI gate.
Security Rails are not substitute for auth - pair with AI Security tool permissions.

Important

Log every block with reason code and trace ID. Guardrails you cannot measure will be disabled under production pressure.

  • NeMo Guardrails: Colang flows, input/output/dialog rails, LangChain integration.

  • Guardrails AI: Python validators, .rail spec files, hub of pre-built validators.

  • Llama Guard / Mistral Moderation: Safety classifiers.

  • Azure Content Safety, OpenAI Moderation: Hosted toxicity APIs.

  • Rebuff / Lakera: Prompt injection detection (pairs with input rails).

  • Instructor / Outlines: Structured generation reducing output rail failures.

  • Agent frameworks: Tool rails in LangGraph and peers - Best AI Agent Frameworks.

  • Comparisons: LangGraph vs CrewAI · OpenAI Agents SDK vs LangGraph

  • AI Security: Threat model guardrails implement.

  • Human-in-the-Loop: Escalation and approval when rails are not enough.

  • Observability: Trace guardrail spans and block metrics.

  • Prompt Engineering: Complements rails; does not replace them.

  • RAG / Agentic RAG: Faithfulness rails validate grounding.

  • Tool Calling: Tool rails before execution.

  • AI Agents: Agents need rails at every loop iteration.

  • LLM Evaluation: Offline measurement pairs with online rails.

  • Agent Evaluation: Score whether rails fired, and whether unsafe actions still reached the environment.

If you understood this topic, read next:

Diagram: Learning path for guardrails

flowchart LR
    A[Security] --> B[Guardrails]
    B --> C[HITL]
    C --> D[Agents]
    D --> E[Observability]
    E --> F[Evals]

Prerequisites: AI Security · Prompt Engineering · Large Language Models

Next topics: Human-in-the-Loop · Observability · AI Agents

Estimated time: 50 min · Difficulty: Intermediate

Key Takeaways

  • Guardrails are programmatic, testable policy enforcement - not prompt wishes.
  • NeMo Guardrails (Colang) handles dialog flows; Python validators handle schema and faithfulness.
  • Place input rails before inference, output rails before delivery, tool rails before execution.
  • Cap retries, log block reasons, and tune false positives with production feedback.
  • Rails enforce mechanical policy; HITL covers judgment; evals measure quality offline.
  • Pair guardrails with AI Security and observability for operable production systems.

FAQs

What are AI guardrails?

Automated checks on LLM inputs and outputs (and agent actions) that enforce safety, format, and policy constraints outside the model's probabilistic behavior.

How is NeMo Guardrails different from a system prompt?

NeMo uses Colang to define executable dialog flows - when to refuse, escalate, or call tools - rather than relying on the model to interpret static instructions.

Do guardrails replace AI security?

No. Guardrails enforce policies; security includes auth, tool permissions, threat modeling, and architecture. Use both.

Should I block or retry on output validation failure?

Retry (max 2) for format/schema errors. Block immediately for toxicity, secrets, or policy violations.

How do I reduce false positives?

Tune classifier thresholds, maintain allowlists for known good patterns, review blocked traces weekly, add user override with logging for internal tools.

What is a faithfulness rail?

Checks that the generated answer is supported by retrieved context - typically NLI entailment or LLM-as-judge - to reduce RAG hallucination.

Can guardrails work with streaming?

Validate incrementally where possible (PII scan on buffer); full schema validation may require buffering final JSON. Stream prose after output rails pass or use block-then-stream pattern.

How much latency do guardrails add?

50–200ms typical for classifiers + regex. NLI faithfulness adds 100–300ms. Budget in SLO; parallelize independent checks.

How do I test guardrails in CI?

Golden dataset: must-block (injection, competitor, toxic) and must-pass (valid support queries). Assert block/pass decisions and fallback messages.

NeMo Guardrails vs Guardrails AI?

NeMo focuses on dialog flows and Colang orchestration. Guardrails AI focuses on output schema validation with Pydantic-like specs. Many teams use both.

Where do tool rails run?

In the orchestrator, immediately before tool execution - after model proposes call, before API/database access.

How do I deploy NeMo Guardrails in production?

Run as a sidecar or embedded Python service with config.yml and Colang files in version control. Load configs at startup; hot-reload only in staging. Pair with GPU if using local classifier models; CPU suffices for rule-heavy flows. Health check the rails server independently of the LLM provider - a down rails service should fail closed (safe fallback), not passthrough.

Should guardrails differ by API endpoint?

Yes. Public chat needs strict topical and toxicity rails. Internal codegen tools need schema validation but lighter topic restriction. Define rail profiles (external, internal, agent_write) selected by orchestrator based on route and user role.

How do guardrails interact with observability?

Each rail emits a span: guardrail.input.injection, guardrail.output.schema, with pass/fail and latency. Alert on block rate anomalies.

How do rails compose with MCP tools?

Apply tool rails at the MCP client in your orchestrator - validate tool name against allowlist, schema-check arguments, and rate-limit per tool before the MCP server executes. MCP does not replace rails; it standardizes transport only.

References

Further Reading

Next Topics

Learning Path

Continue Learning

Related Guides

Related Tools

ToolCategoryPurposeWebsiteBest For
OpenAI Agents SDK
Open SourceAPI
frameworksOfficial OpenAI framework for tool-using agents with handoffs, guardrails, and tracing.openai.github.ioMulti-step agent workflows
LangGraph
FeaturedOpen SourceAPI
frameworksGraph-based orchestration runtime for long-running, stateful agents.langgraph.devMulti-agent orchestration
CrewAI
NewOpen SourceAPI
frameworksMulti-agent framework with Crews, tasks, and event-driven Flows.crewai.comContent pipelines
LangChain
PopularOpen SourceAPI
frameworksFramework for building LLM-powered applications and workflows.langchain.comRAG systems
Patronus AI
APICloud
EvaluationAutomated evaluation and scoring platform for LLM outputs and safety.patronus.aiSafety evaluation
Guardrails AI
Open SourceAPI
guardrailsOpen-source framework for validating and structuring LLM inputs and outputs.guardrailsai.comOutput validation
NeMo Guardrails
Open SourceSelf-hosted
guardrailsNVIDIA toolkit for programmable guardrails using Colang dialogue flows.github.comDialogue policy control
Lakera
APICloud
guardrailsAI security platform for prompt injection detection and LLM firewall protection.lakera.aiPrompt injection defense
Llama Guard
Open SourceSelf-hosted
guardrailsMeta's open safety classifier model for input and output moderation.ai.meta.comSelf-hosted moderation
Microsoft Presidio
Open SourceSelf-hosted
guardrailsOpen-source PII detection and anonymization for text and images.microsoft.github.ioPII redaction in LLM pipelines
Prompt Security
CloudEnterprise
securityGenAI security platform protecting employees and apps from AI-specific threats.prompt.securityEmployee AI usage governance
Pangea
APICloud
securitySecurity API platform with AI guard services for prompt and response scanning.pangea.cloudDrop-in security APIs