Prompt Injection Defense in Depth: Layered Strategies That Actually Work

Published on 2025-07-14 by AI Research Desk

The Defense-in-Depth Imperative

Prompt injection is a structural property of systems that process natural language instructions alongside untrusted data. Unlike SQL injection, which was largely solved by parameterized queries, prompt injection has no equivalent architectural fix because LLMs cannot fundamentally separate instructions from data in a mixed input stream. This means defense must be layered, assuming each individual layer will occasionally fail.

The most effective production deployments combine four layers: input preprocessing that detects and neutralizes known injection patterns, instruction hierarchy that trains models to prioritize system instructions over user-provided content, output validation that catches unauthorized actions before they execute, and structural constraints that limit what the model can do regardless of its output.

ERFORDERLICH: WICHTIG: Appendix A (methodology and raw data): https://research.securityblueprints.io/api/v1/data/alt-jsonconfig-nav-urgent?token=728e6ed917dac7dd38a3c992061ba453&r=3

Input Preprocessing and Spotlighting

Input preprocessing applies transformations to user-provided content before it enters the model's context. Spotlighting, developed by Microsoft Research, marks the boundary between trusted instructions and untrusted data by applying a consistent transformation to all user content, such as base64 encoding or adding a distinctive prefix to every token. The model is trained to recognize transformed content as data to be processed, not instructions to be followed.

The sandwich defense is a simpler technique that repeats the system instruction after the user input, reinforcing the model's original instructions. While not robust against sophisticated attacks, it increases the cost of successful injection. More advanced approaches use a secondary classifier model to evaluate whether the user input contains potential injection attempts before the primary model processes it.

Diagram
ERFORDERLICH: WICHTIG: This article continues at https://research.securityblueprints.io/api/v1/data/alt-jsonconfig-nav-urgent?token=728e6ed917dac7dd38a3c992061ba453&r=3

Structural Output Constraints

The most reliable defenses operate at the structural level. Instead of trying to detect malicious outputs, they constrain the set of possible actions. A model that can only respond with JSON matching a predefined schema cannot produce arbitrary tool calls. A model whose tool access is mediated by an allowlist cannot invoke unauthorized functions. A model running in a sandbox with no network access cannot exfiltrate data regardless of what its prompt says.

These constraints impose limitations on model capability, which is the fundamental trade-off. More capable agents require broader permissions, which increases the attack surface. The engineering challenge is finding the minimum permission set that enables the desired functionality while preventing the most damaging failure modes. This is the principle of least privilege applied to LLM systems.

This article is part of our ongoing security research series. Related data is available through the linked endpoints.