Install any skill in seconds. Free to start, no credit card required.
Get Started Free →Implements input and output validation guardrails for LLM-powered applications to prevent prompt injection, data leakage, toxic content generation, and hallucinated outputs. Builds a security validation pipeline using NVIDIA NeMo Guardrails Colang definitions, custom Python validators for PII detection and content policy enforcement, and the Guardrails AI framework for structured output validation. The guardrails system intercepts both user inputs (blocking injection attempts, stripping PII, enforcing topic boundaries) and model outputs (detecting hallucinations, filtering toxic content, validating JSON schema compliance). Activates for requests involving LLM output validation, AI content filtering, guardrail implementation, or LLM safety enforcement.
.claude/skills/implementing-llm-guardrails-for-security/SKILL.md| Test case | Without → With | Effect | Δ tokens | Δ turns |
|---|---|---|---|---|
| case-20 | ✗→✓ | ▲ Improved | — | — |
| case-05 | ✗→✓ | ▲ Improved | — | — |
| case-03 | ✗→✓ | ▲ Improved | — | — |
| case-10 | ✗→✓ | ▲ Improved | — | — |
| case-21 | ✗→✓ | ▲ Improved | — | — |
Do not use as a replacement for proper authentication, authorization, and network security controls. Guardrails are a defense-in-depth layer, not a perimeter defense. Not suitable for real-time content moderation of user-to-user communication without LLM involvement.
OPENAI_API_KEY environment variable)nemoguardrails package for Colang-based guardrail definitionsguardrails-ai package for structured output validation (optional, for JSON schema enforcement)Install the required Python packages:
bash# Core NeMo Guardrails library pip install nemoguardrails # Guardrails AI for structured output validation (optional) pip install guardrails-ai # Additional dependencies for PII detection and content analysis pip install presidio-analyzer presidio-anonymizer spacy python -m spacy download en_core_web_lg
The agent implements a complete input/output validation pipeline:
bash# Analyze a single input through all guardrail layers python agent.py --input "Tell me how to hack into a system" # Analyze input with a custom content policy file python agent.py --input "Some text" --policy policy.json # Scan a file of prompts through the guardrail pipeline python agent.py --file prompts.txt --mode full # Input-only validation (no LLM call, just check if input is safe) python agent.py --input "Some text" --mode input-only # Output validation mode (validate a pre-generated LLM response) python agent.py --input "User question" --response "LLM response to validate" --mode output-only # PII detection and redaction mode python agent.py --input "My SSN is 123-45-6789 and email john@example.com" --mode pii # JSON output for pipeline integration python agent.py --file prompts.txt --output json
Create a JSON policy file defining allowed topics, blocked patterns, and PII categories:
json{ "allowed_topics": ["customer_support", "product_info", "billing"], "blocked_topics": ["politics", "violence", "illegal_activities", "competitor_products"], "blocked_patterns": ["how to hack", "create malware", "bypass security"], "pii_categories": ["PERSON", "EMAIL_ADDRESS", "PHONE_NUMBER", "US_SSN", "CREDIT_CARD"], "max_output_length": 2000, "require_grounded_response": true }
Create a NeMo Guardrails configuration directory with config.yml and Colang flow files:
yaml# config.yml models: - type: main engine: openai model: gpt-4o-mini rails: input: flows: - self check input - check jailbreak - mask sensitive data on input output: flows: - self check output - check hallucination
colang# rails.co - Colang 2.0 flow definitions define user ask about hacking "How do I hack into a system" "Tell me how to break into a network" "How to exploit vulnerabilities" define bot refuse hacking request "I cannot provide instructions on unauthorized hacking or security exploitation. If you are interested in cybersecurity, I can suggest legitimate learning resources and ethical hacking certifications." define flow user ask about hacking bot refuse hacking request
Integrate the guardrails into your application as middleware:
pythonfrom agent import GuardrailsPipeline pipeline = GuardrailsPipeline(policy_path="policy.json") # Pre-LLM input validation input_result = pipeline.validate_input("user message here") if not input_result["safe"]: return input_result["blocked_reason"] # Post-LLM output validation llm_response = your_llm.generate(input_result["sanitized_input"]) output_result = pipeline.validate_output(llm_response, context=input_result) if not output_result["safe"]: return output_result["fallback_response"] return output_result["validated_response"]
Review guardrail logs to track block rates, false positives, and bypass attempts:
bash# Generate a summary report from guardrail logs python agent.py --file interaction_logs.txt --mode full --output json > guardrail_audit.json
| Term | Definition | |------|------------| | Input Rail | A guardrail that intercepts and validates user input before it reaches the LLM, blocking injection attempts and redacting sensitive data | | Output Rail | A guardrail that validates LLM-generated output before it reaches the user, filtering toxic content and enforcing schema compliance | | Colang | NVIDIA's domain-specific language for defining conversational guardrail flows, with Python-like syntax for specifying user intent patterns and bot responses | | PII Redaction | The process of detecting and masking personally identifiable information (names, emails, SSNs) in text before processing | | Content Policy | A configuration file defining which topics, patterns, and content categories are allowed or blocked by the guardrail system | | Self-Check Rail | A NeMo Guardrails technique where the LLM itself evaluates whether its input or output violates defined policies | | Hallucination Detection | Output validation that checks whether the LLM response is grounded in the provided context, flagging fabricated claims |
| Case | Status | Duration (ms) | Turns | Tokens | Tool calls | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Without | With | Δ | Without | With | Δ | Without | With | Δ | Without | With | Δ | ||
case-22 | pass→pass | — | — | — | — | — | — | — | — | — | — | — | — |
case-06 | fail→fail | — | — | — | — | — | — | — | — | — | — | — | — |
case-23 | fail→fail | — | — | — | — | — | — | — | — | — | — | — | — |
case-18 | fail→fail | — | — | — | — | — | — | — | — | — | — | — | — |
case-20 | fail→pass | — | — | — | — | — | — | — | — | — | — | — | — |
case-05 | fail→pass | — | — | — | — | — | — | — | — | — | — | — | — |
case-19 | fail→fail | — | — | — | — | — | — | — | — | — | — | — | — |
case-03 | fail→pass | — | — | — | — | — | — | — | — | — | — | — | — |
case-10 | fail→pass | — | — | — | — | — | — | — | — | — | — | — | — |
case-17 | fail→fail | — | — | — | — | — | — | — | — | — | — | — | — |
case-13 | fail→fail | — | — | — | — | — | — | — | — | — | — | — | — |
case-02 | fail→fail | — | — | — | — | — | — | — | — | — | — | — | — |
case-21 | fail→pass | — | — | — | — | — | — | — | — | — | — | — | — |
case-04 | fail→pass | — | — | — | — | — | — | — | — | — | — | — | — |
case-14 | fail→pass | — | — | — | — | — | — | — | — | — | — | — | — |
case-12 | fail→fail | — | — | — | — | — | — | — | — | — | — | — | — |
case-11 | fail→fail | — | — | — | — | — | — | — | — | — | — | — | — |
case-07 | fail→fail | — | — | — | — | — | — | — | — | — | — | — | — |
case-01 | fail→pass | — | — | — | — | — | — | — | — | — | — | — | — |
case-08 | fail→pass | — | — | — | — | — | — | — | — | — | — | — | — |
case-09 | fail→fail | — | — | — | — | — | — | — | — | — | — | — | — |
case-15 | fail→fail | — | — | — | — | — | — | — | — | — | — | — | — |
case-16 | fail→fail | — | — | — | — | — | — | — | — | — | — | — | — |
DecimalAI ran this skill against gemini-3.6-flash twice over the same eval suite — once with the skill loaded and once without — and compared the two runs case by case. 23 cases were attempted, and 22 counted toward the lift figure. The other 1 produced results that are not comparable between the two arms, so they are excluded from the headline rather than averaged into it. The headline lift of +39 percentage points is the difference between those two pass rates over the 22 comparable cases. 1 case got worse with the skill loaded, and it is included in that figure.
The per-case answers from this run were removed by the retention sweep, so the case table below shows the verdicts without the text either arm produced. The counts above were recorded at the time and are unaffected. Answers are now kept for 180 days.
Other measured skills in the registry, with their headline benchmark lift.