Web Security

How to Protect an AI Agent from Prompt Injection (Beyond Detection)

Back to Articles
2026-11-30
7 min read
MAAuthor Marius Andronie
How to Protect an AI Agent from Prompt Injection (Beyond Detection)

How do you protect an AI agent from prompt injection? Scan what it reads for injected instructions, but do not rely on the scan. The protection that holds is a set of hard permission checks written in code, outside the model, so that a hijacked prompt still cannot send mail, write files or touch a secret. Detection lowers the noise. Permissions limit the damage.

The problem in plain words

If you give an AI agent a browser, an inbox or a folder of documents, it will read text written by strangers. A web page can contain a line, often hidden in white text or in a comment, that says "ignore your previous instructions and do this instead". An email can carry the same line. The model reads it next to your real instructions and cannot always tell which is which.

A chat assistant that only talks is a small risk. An agent with tools is different: files, email, APIs, a shell. The injected line is no longer just text, it can become an action. OWASP, in its LLM Prompt Injection Prevention Cheat Sheet, describes the indirect form as "malicious instructions hidden in external content that the LLM processes". That is the case I worry about, because nobody typed it into your system. It arrived inside something your agent was asked to read.

The idea for this post came from a newsletter by Vaibhav Sisinty, "a webpage hijacked my ai", which walks through building a small firewall for exactly this. I wrote my own version from how we run our agents, so the points below are ours.

Two detection layers

Most teams start with two layers, and both are worth having.

1. A phrase and pattern scan. A short list of patterns such as "ignore previous instructions", "you are now", "reveal your system prompt", plus checks for hidden text and odd encodings. It is cheap, it runs in microseconds, and it catches lazy attacks. It misses anything phrased differently, translated, or split across sentences.

2. A small classifier model. One example is Meta's Llama Prompt Guard 2, which is available in an 86M variant. Its Hugging Face page says it is trained to detect prompt injection and jailbreaking attacks, and that both Prompt Guard 2 models support a 512-token context window. For longer input, the page says to split prompts into segments and scan them in parallel. In practice that means a long web page or email thread has to be cut into chunks, each chunk scored, and the worst score kept. The model is gated: you log in and accept the conditions on Hugging Face before you can download it.

A classifier is better than a phrase list at catching rewording. It still guesses. It can miss a new trick and it can flag harmless text. OWASP says it plainly: input screening "is not a security boundary", and screening "does not stop an injection that it fails to detect".

So use both layers, treat their score as a signal, and then build the part that does not depend on guessing.

The real protection: checks the model cannot talk its way past

A model can be persuaded. Code that checks a permission cannot. The principle in the OWASP sheet is least privilege: grant minimal necessary permissions, and require human approval for consequential tool actions before execution.

Here is how we run our own agents, in general terms:

  • Content is data, never instructions. Anything the agent reads from a page, an email or a file is handed to it as material to analyse. Instructions come only from the task we gave it.
  • Every write needs its own human approval, tied to the exact task. An approval for one job does not carry over to the next. If the agent wants to change something, it asks, and a person says yes to that specific change.
  • Some sites are read-only for the browser agent, by a coded rule. It is not a polite request in the prompt. The tool itself refuses the write, whatever the model says.
  • Outgoing mail passes code gates. Before a message leaves, scripts check things like recipient, duplicates and attachments. The model cannot skip a gate by arguing with it.
  • Secrets never go into the agent's brief. An agent that never saw a key cannot be tricked into repeating it.

None of this makes us immune. It means that when a page does manage to fool the model, the worst outcome is bounded by what the agent was allowed to do in the first place.

A checklist you can apply this week

  1. List every source your agent reads that a stranger can write to: web pages, inbound email, uploaded files, support tickets.
  2. Label that content as untrusted data in the way you pass it to the model, and keep your instructions in a separate channel.
  3. Run a phrase scan and a classifier on it, chunking anything longer than the classifier's limit, and log what they flag.
  4. Give the agent the smallest set of tools that does the job, and read-only access wherever writing is not needed.
  5. Put a human approval in front of every action that changes something outside the agent: sending, deleting, paying, publishing.
  6. Enforce those limits in code, not in the prompt.
  7. Keep secrets out of the context window entirely. Use tools that hold the credential and expose only the action.
  8. Test it: plant a harmless injected line in a page and an email, and confirm the checks stop the action, not just the text.

Frequently Asked Questions

What is prompt injection? It is an attack where text the AI reads, such as a web page or an email, contains instructions that the model follows as if they came from you. When the text comes from external content rather than the user, it is called indirect prompt injection.

Can a prompt injection scanner stop all attacks? No. A phrase scan catches simple attacks and a classifier catches more, but both can miss a new one. OWASP states that screening is not a security boundary. Pair detection with permission limits and human approval.

What is Llama Prompt Guard 2? It is a small classifier model from Meta, available on Hugging Face, trained to detect prompt injection and jailbreaking attacks. It reads 512 tokens at a time, so long text must be split into segments. Access is gated behind a license acceptance.

Do I need a human in the loop for every action? Not for every action. Reading and summarising can run freely. Anything that changes something outside the agent, such as sending an email or writing to a system, should need an approval tied to that specific task.

Is a firewall for an AI agent only about detection? No. Detection is one layer. The part that limits damage is the permission layer: least privilege, read-only defaults, coded gates on outgoing actions and no secrets in the agent's context.

If you run AI on emails or web content

We build this kind of guard layer for companies whose AI reads emails or web pages: the scan, the chunking, the approval step and the permission rules, delivered as a fixed-price project. If that is your situation, tell me what your agent reads and which tools it can use, through the contact page, or see how we work on the AI development page.

Sources: Llama Prompt Guard 2 86M on Hugging Face, OWASP LLM Prompt Injection Prevention Cheat Sheet, and the newsletter that prompted this post, Vaibhav Sisinty on building an AI firewall.

Tell us what you need built

Python and AI systems, websites and rescues for stuck platforms, scoped and quoted in writing. No sales call.

Tell us what you need built →
Opens in a new tab