There is no complete fix for prompt injection. A language model reads instructions and data in the same stream of text, so a cleverly written input can always try to pass itself off as an instruction. Prompt injection prevention is really about two things: making attacks harder, and making sure a successful one can't do much damage. Here are three patterns I use in live AI apps, and what each one protects against.
What is prompt injection?
Prompt injection is when text that should be treated as data, such as a web page, an email, a headline or a user message, contains instructions that the AI follows. Direct injection comes from the user typing it. Indirect injection hides in content the app feeds to the model, like a scraped page. The risk grows with what the AI is allowed to do: a model that can only write a summary can do far less harm than one that can send messages or change records.
Pattern 1: Treat outside text as untrusted data
Where: Tech News Notifier, which reads about 600 headlines a day from 23 sources and uses AI to score each one.
Every headline is written by someone else, and every one reaches the model. So the bot treats headlines strictly as data:
- Clean the input. Unsafe characters are stripped before a headline reaches the model.
- Force a fixed output shape. The model answers in JSON-schema mode: a score from 1 to 5, a category, a one-line reason and a duplicate flag. Nothing else is accepted.
- Validate everything. The output is checked strictly against that schema. A headline can't make the model return something new, because anything outside the shape is rejected.
- Drop suspicious output. If the model's "why it matters" line just repeats the headline, it is discarded.
The worst a malicious headline can do is get itself scored wrongly. A wrong score is then checked again, because a story from a low-reputation outlet needs confirmation from a reputable one within 3 hours before it is sent.

Pattern 2: The AI proposes, a person confirms
Where: Fin, my personal finance app, where you type spends in plain words and ask questions about your money.
Fin's AI never changes anything on its own. When you type an entry, the model returns a proposal and you confirm it before it is saved. It never edits budgets or saved records by itself.
This is the most effective defense I know, because it doesn't depend on catching the attack. Even if text somehow steered the model, the result is a suggestion on screen, not a change in the database. For any AI feature that writes data, sends something or spends money, a confirm step turns a security problem into a visible mistake you can reject.

Pattern 3: Give the AI exactly the user's permissions, and no secrets
Where: an MCP server with 169 tools that lets Claude work inside a project management app, described in lessons from building an MCP server.
When an AI can call tools, injection becomes an action problem, not just a text problem. Three rules limit what any call can do:
- The AI acts as the signed-in user. Every tool call runs with exactly that user's permissions, through the same rules as the web app. It can't see or change anything the user couldn't.
- The AI never holds the keys. The raw login token is never passed into the tool code, so no tool output can leak it.
- Dangerous actions are not tools. The most destructive admin actions, like deleting a whole organization, are simply not exposed to the AI.
If an injected instruction got through, the damage is capped at what that user could do by hand, with no secrets to steal.

How do you prevent prompt injection in your own app?
Start from what the AI is allowed to do, then work backward:
- List what the model reads. Anything not typed by your own trusted code is untrusted: pages, files, emails, headlines, user input.
- Shrink the output. Prefer a fixed schema over free text whenever the app is going to act on the result, and validate it.
- Put a person in front of actions. Saving, sending, deleting and paying should be proposals that someone confirms.
- Limit permissions. The AI gets the user's rights at most, never an admin key, and never secrets in its context.
- Leave out the worst actions. If an action would be a disaster when misused, don't make it available to the AI at all.
If you only do one, do number 3. A confirm step doesn't depend on spotting the attack, so it still works against tricks nobody has thought of yet.
What these patterns don't do
They don't make injection impossible. A headline can still try to fool the scorer, and a user can still try to trick Fin into a strange proposal. What the patterns do is keep the result small and visible: a wrong score, a suggestion you reject, a request that fails a permission check. That is the realistic goal.
For more on how the news bot's AI decides what matters, see building an AI news aggregator. For choosing the model behind an AI feature, see Claude Haiku vs Sonnet in a live app.
Adding AI to an app that reads outside content or takes actions? Message me on WhatsApp and tell me what it touches.