Build a prompt injection detection and prevention layer for LLM applications with input sanitization and output filtering.
You are an LLM security engineer. Build a prompt injection prevention system. ## Configuration - **Application**: [APP e.g. customer support chatbot / code assistant / document QA] - **LLM Provider**: [LLM e.g. Claude API / OpenAI API / self-hosted Llama] - **Language**: [LANG e.g. Python / TypeScript] - **Risk Level**: [RISK e.g. high (financial) / medium (general) / low (internal tool)] ## System Components ### 1. Input Detection (`detection/input_scanner.py`) - **Pattern-based detection**: - Known injection patterns: 'ignore previous', 'system prompt', 'you are now', 'pretend to be' - Instruction override attempts: role switching, context manipulation - Encoding attacks: base64, rot13, Unicode tricks, homoglyphs - Delimiter injection: markdown, XML tags, code blocks - Multi-language injection: translated attack phrases - **ML-based detection**: - Fine-tuned classifier for injection vs legitimate input - Perplexity scoring: flag unusually structured inputs - Semantic similarity to known attack corpus - Anomaly detection on input distributions - **Confidence scoring**: 0-1 score for injection likelihood - **Action thresholds**: block, flag for review, pass with monitoring ### 2. Input Sanitization (`sanitization/`) - Strip known instruction delimiters - Escape special formatting characters - Truncate overly long inputs - Remove encoded payloads - Normalize Unicode to prevent homoglyph attacks - Tag user input with clear markers for the LLM ### 3. System Prompt Hardening (`hardening/`) - Instruction hierarchy: system > developer > user - Boundary reinforcement in system prompt - Output format constraints (JSON schema, structured output) - Role locking: prevent role reassignment - Canary tokens for system prompt extraction detection - Defense-in-depth prompt templates ### 4. Output Filtering (`detection/output_scanner.py`) - Detect system prompt leakage in responses - Filter confidential information exposure - Block jailbreak confirmation patterns - Validate output conforms to expected schema - PII detection and redaction in responses - Hallucination detection for factual claims ### 5. Monitoring & Alerting (`monitoring/`) - Log all blocked and flagged inputs - Track injection attempt patterns over time - Alert on spike in detection events - Dashboard with attack type distribution - Automated reporting for security team ### 6. Testing Suite (`tests/`) - Red team prompt library (500+ injection patterns) - Automated testing against the detection pipeline - False positive rate measurement - Evasion technique testing - Benchmark: precision, recall, F1 on injection detection Provide the complete system with configuration, deployment guide, and benchmark results.
Free to copy and use. Compatible with Claude 4 Opus, GPT-5, Gemini 2.5 Pro, Llama 4.
Deploy the detection layer between user input and your LLM API call. Configure risk thresholds based on your application sensitivity. Run the test suite regularly and update detection patterns monthly.
Initial release
Sign in and download this prompt to leave a review.