Intermediate

AI Guardrails

Guardrails are the runtime safety mechanisms that prevent AI systems from producing harmful, inaccurate, or policy-violating outputs. They are your last line of defense between the model and the user.

What Are Guardrails?

Guardrails are programmable constraints applied to AI systems at inference time. Unlike training-time safety measures (like RLHF), guardrails operate as external checks that validate inputs and outputs against predefined rules, policies, and safety criteria.

Types of Guardrails

Type When Applied Purpose
Input Guards Before the model processes the input Block harmful, malicious, or out-of-scope requests
Output Guards After the model generates a response Filter harmful content, validate accuracy, enforce formatting
System Prompts At the start of every conversation Define the model's role, boundaries, and behavioral guidelines
Tool Guards Before executing function calls Validate parameters, check permissions, limit scope of actions
Rate Limiters Across multiple requests Prevent abuse, detect automated attacks, manage resource usage

Implementing Input Guards

Python - Input Validation Pipeline
class InputGuard:
    def validate(self, user_input: str) -> tuple[bool, str]:
        # Check 1: Content moderation classifier
        if self.content_classifier.is_harmful(user_input):
            return False, "Request contains potentially harmful content."

        # Check 2: Topic boundary check
        if not self.is_in_scope(user_input):
            return False, "This question is outside my area of expertise."

        # Check 3: PII detection
        if self.pii_detector.contains_pii(user_input):
            user_input = self.pii_detector.redact(user_input)

        # Check 4: Injection detection
        if self.injection_detector.is_suspicious(user_input):
            return False, "Request could not be processed safely."

        return True, user_input

Output Filtering

Output guards validate model responses before they reach the user:

  • Toxicity classifiers: Score responses for harmful language and block those above a threshold
  • Factual grounding: Compare claims against verified knowledge bases or retrieved documents
  • PII scrubbing: Detect and redact any personal information in the output
  • Format validation: Ensure responses meet structural requirements (JSON schema, length limits)
  • Consistency checks: Verify the output does not contradict the system prompt or established facts

Guardrail Frameworks

NeMo Guardrails

NVIDIA's open-source toolkit for adding programmable guardrails to LLM applications. Uses Colang, a modeling language for conversational flows.

Guardrails AI

Open-source framework that validates LLM outputs against user-defined schemas and policies. Supports automatic re-asking when outputs fail validation.

LLM Guard

Security-focused guardrails library with scanners for prompt injection, toxic language, PII, and invisible text attacks.

Lakera Guard

API-based service specializing in prompt injection detection with continuously updated threat intelligence.

Important: Guardrails are not foolproof. Sophisticated attackers can sometimes bypass runtime guards. Always combine guardrails with training-time safety measures and continuous monitoring. Defense in depth is essential.

Ready to Go Deeper?

Live instructor-led courses from our partners. Affiliate disclosure.