Build a multi-layered prompt injection defense system with input classification, output filtering, and canary token detection for LLM-powered applications.
You are a prompt security engineer specializing in LLM application security. Build a production-grade prompt injection defense system. ## Configuration - **LLM Provider**: [LLM_PROVIDER e.g. OpenAI / Anthropic / self-hosted] - **Application Type**: [APP_TYPE e.g. chatbot / code assistant / document QA / agent] - **Language**: [LANGUAGE e.g. Python / TypeScript / Go] - **Risk Level**: [RISK e.g. high (financial) / medium (enterprise) / standard] ## Defense Layers ### Layer 1: Input Classification - Train a lightweight classifier to detect injection attempts - Pattern matching for known injection templates (jailbreaks, DAN, role-play escapes) - Unicode and encoding attack detection (homoglyphs, zero-width chars, RTL override) - Input tokenization analysis for suspicious token sequences - Perplexity-based detection for adversarial suffixes ### Layer 2: System Prompt Protection - Delimiter injection with random boundary tokens - Instruction hierarchy enforcement (system > user) - Canary token placement and monitoring - System prompt hash verification between calls - Role-based prompt template isolation ### Layer 3: Output Validation - Response content policy enforcement - PII/secrets detection in outputs - Tool call validation (for [APP_TYPE] agents) - Output schema enforcement with JSON validation - Hallucination detection for grounded responses ### Layer 4: Runtime Monitoring - Injection attempt rate tracking per user/session - A/B testing of defense effectiveness - False positive rate monitoring with user feedback - Automated threat intelligence updates - Incident alerting with Slack/PagerDuty integration Generate complete [LANGUAGE] implementation with middleware architecture, configuration files, test suites with adversarial examples, and deployment guide.
Free to copy and use. Compatible with Claude 4 Opus, Claude 4 Sonnet, GPT-5, Gemini 2.5 Pro.
Replace parameters with your LLM provider and application type. Start with Layer 1 classification and Layer 2 prompt protection. Add output validation and monitoring iteratively.
Initial release
Sign in and download this prompt to leave a review.