You can't fully prevent prompt injection today, but you can make it much less likely to work and much less harmful when it does.
Even OWASP says "it is unclear if there are fool-proof methods of prevention for prompt injection" (LLM01:2025). So the goal is layers: make injection harder, and make sure a successful one can't do much.
Here are the seven defenses OWASP lists, with what each looks like in practice.
1. Constrain the model's behavior
Give the model a clear role and scope in its system prompt, and tell it to treat instructions inside user content or documents as data. This raises the bar, but a determined injection can still talk its way past it, so never rely on it alone.
2. Define and validate output formats
If your app expects JSON with three fields, check that it got exactly that, using ordinary code rather than another prompt. Output that breaks the format, or contains things it shouldn't, gets rejected before it reaches anything that acts on it.
3. Filter input and output
Scan what goes in and what comes out. On the way in, look for known injection patterns. On the way out, look for leaked secrets, system prompt text, or links to unknown domains. Our analyzer is a simple example of input scanning.
Filters miss things, especially reworded or translated attacks, so treat them as one layer, not a wall.
4. Give the model least privilege
This is the defense that limits damage most. Keep API keys in your code, never in the prompt. Give the model access to only the tools and data the task needs. An assistant that summarizes support tickets doesn't need permission to issue refunds.
5. Require human approval for risky actions
Sending email, spending money, deleting data and changing permissions should wait for a person to confirm. If an injection does steer the model, the human sees the odd request before anything happens.
6. Separate and label outside content
Mark clearly which text came from the user, which came from a web page, and which came from a file, so both the model and your own code can treat untrusted content with less trust. Delimiters alone won't stop a strong attack, but labeling makes filtering and auditing far easier.
7. Test like an attacker, regularly
Treat the model as an untrusted user and test it the way an attacker would, every time you change the model, prompt or tools. Start with the canary payloads: they're harmless, they cover direct and indirect injection, and a failure is easy to spot.
A quick checklist
- Secrets stay in code, never in prompts
- Each tool the model can call is truly needed
- Risky actions need a human click
- Output is validated by code before it's used
- The app never renders images or links from model output without checking the domain
- Canary tests run after every model or prompt change
For a step-by-step testing routine, read How to Test Your LLM App for Prompt Injection.