Course contentsShow
← Security
Lesson 2861 of 3,05253. AI and ML SecurityFree lesson

Defense Strategies Against Prompt Injection

Input validation, output filtering, privilege separation, and architectural patterns to mitigate prompt injection risks.

Defense Strategies Against Prompt Injection

What you'll learn: How to protect LLM systems from prompt injection through layered defense mechanisms.

Since you've learned how adversaries exploit LLM prompts, let's explore architectural and technical defenses that reduce injection risks.

Core Defense Principles

Defending against prompt injection requires defense in depth — no single technique provides complete protection. Think of it like protecting a medieval castle: walls, moats, guards, and internal compartments all work together.

Input Validation and Sanitization

Pre-process user inputs to detect and neutralize malicious patterns. Check for:

  • Known injection keywords ("ignore previous instructions")
  • Unusual delimiters attempting to break context
  • Character encoding attacks
  • Input length anomalies

However, simple pattern matching is insufficient — attackers constantly evolve techniques.

Output Filtering and Post-Processing

Monitor and filter LLM outputs before delivery:

  • Detect leaked system prompts or sensitive data
  • Validate outputs match expected format
  • Apply content security policies
  • Flag responses that violate usage boundaries

Privilege Separation Architecture

Critical defense: Separate the LLM into distinct privilege levels:

  • Unprivileged model: Handles untrusted user input, no access to tools/plugins
  • Privileged orchestrator: Controls tool access, validates intent before execution
  • Sandboxed plugins: Each tool operates with minimal necessary permissions

This is analogous to how operating systems separate user space from kernel space.

Architectural Patterns

Structured output constraints: Force LLMs to respond in structured formats (JSON schemas) rather than freeform text, making injections easier to detect.

Prompt/instruction separation: Store system prompts outside the main context window, with architectural enforcement preventing user inputs from accessing or modifying them.

Continuous monitoring: Log all prompts and responses, using anomaly detection to identify potential injection attempts.

Key Takeaway: Defense against prompt injection requires layered strategies combining input validation, output filtering, architectural privilege separation, and continuous monitoring — no single technique suffices against evolving attacks.