Build a comprehensive LLM guardrails system with input/output validation, content filtering, and structured output enforcement.
You are an AI safety engineer. Build a guardrails framework for LLM applications. ## Configuration - **Application**: [APP e.g. customer chatbot / code generator / content writer / data analyst] - **LLM**: [LLM e.g. Claude 4 / GPT-5 / Llama 4] - **Framework**: [FRAMEWORK e.g. NeMo Guardrails / Guardrails AI / custom] - **Risk Tolerance**: [RISK e.g. strict (finance) / moderate (general) / relaxed (creative)] ## Guardrail Components ### 1. Input Guardrails (`input/`) - **Topic boundaries**: Allow only [ALLOWED_TOPICS e.g. product questions, account help] - **Jailbreak detection**: Pattern matching + ML classifier - **PII detection**: Name, email, phone, SSN, credit card number - **Toxicity filtering**: Hate speech, threats, harassment - **Language detection**: Restrict to [LANGUAGES e.g. English, Hindi] - **Length limits**: Max [MAX_INPUT e.g. 2000] characters - **Intent classification**: Map to allowed intents, reject unknown ### 2. Output Guardrails (`output/`) - **Factual grounding**: Verify claims against knowledge base - **PII redaction**: Remove any PII from responses - **Toxicity check**: Ensure response is appropriate - **Brand safety**: No competitor mentions, no controversial topics - **Structured format validation**: JSON schema, markdown, specific format - **Citation verification**: All claims have source references - **Confidence thresholds**: Flag low-confidence responses - **Response length limits**: Min and max token counts ### 3. Conversation Guardrails (`conversation/`) - **Context window management**: Summarize long conversations - **Topic drift detection**: Alert when conversation leaves allowed topics - **Escalation triggers**: Transfer to human when conditions met - **Session limits**: Max turns, max duration, max tokens per session - **Repetition detection**: Prevent looping responses ### 4. Structured Output Enforcement (`structured/`) - JSON schema validation with retry - Pydantic model enforcement - XML/YAML format validation - SQL query validation (syntax and safety) - Code output validation (syntax check, dangerous pattern detection) ### 5. Monitoring and Analytics (`monitoring/`) - Guardrail trigger rates by type - False positive tracking and feedback loop - Response quality scoring over time - User satisfaction correlation with guardrail events - A/B testing guardrail configurations - Cost impact analysis of retries and filtering ### 6. Configuration System (`config/`) - YAML-based guardrail configuration - Environment-specific overrides (dev/staging/prod) - Hot-reload without restart - Versioned configuration with rollback - Per-user/role guardrail customization Provide the complete framework with middleware integration, tests, and tuning guide.
Free to copy and use. Compatible with Claude 4 Opus, Claude 4 Sonnet, GPT-5, Llama 4.
Define your application's allowed topics and risk tolerance. Configure input and output guardrails in YAML. Deploy as middleware between your application and LLM API. Monitor trigger rates and adjust thresholds.
Initial release
Sign in and download this prompt to leave a review.