Prompt injection is an attack where someone plants instructions in text that an AI system reads, such as a chat message, an email or a web page, so the model follows them instead of the rules you gave it. It’s one of the most important security problems for AI agents, because agents act on what they read. My agents read untrusted text all day, so I design for it and test for it. Before launch, the mustashar.ae shop assistant faced 40 attack attempts.

What is prompt injection?

A language model receives one stream of text. Your instructions, the user’s message and anything the agent has fetched all arrive together, and the model has no reliable way to tell which parts are commands and which are just data. Prompt injection exploits that gap: the attacker writes text that looks like instructions and gets it in front of the model.

It belongs to an old family of attacks. SQL injection slips commands into a database query. Email header injection slips extra headers into an email through a form field; on terragreen.ae, I wrote security tests for exactly that. Prompt injection is the same idea aimed at a language model, with one hard difference: there is no perfect escaping function for natural language.

It comes in two forms. In direct prompt injection, the user types the attack into the chat. In indirect prompt injection, the attack hides inside something the agent reads on the user’s behalf: an incoming message, a document, a review or a web page. Indirect attacks are the more dangerous kind for agents, because the person using the agent may never see them.

Picture an assistant that summarizes your email. One message contains a line addressed to the assistant rather than to you, asking it to forward your inbox somewhere. A well-built system treats that line as part of an email to summarize. A careless one treats it as an order.

What kinds of prompt injection attack should you expect?

Attackers are inventive, but most attempts fall into a handful of kinds. I describe them here in general terms only. I don’t publish my test cases.

  • Instruction override: text telling the model to drop its rules or take on a new role.
  • Instruction extraction: attempts to make the model reveal its hidden instructions or setup.
  • Hidden payloads: instructions buried in content the agent reads, sometimes disguised with formatting, odd spacing or another language.
  • Tool hijacking: getting the agent to call a tool it shouldn’t, or a legitimate tool with the attacker’s inputs.
  • Data exfiltration: tricking the agent into revealing other people’s information, or sending data out, for example inside a link.
  • Scope and cost abuse: dragging the agent far off its job, or keeping it busy to run up the bill.
  • False authority: pretending to be staff, a developer or a system message to borrow trust.
Bilingual systems need bilingual attacks

If your assistant speaks Arabic and English, attack it in both, and in a mix of the two. A defense that only works in one language is half a defense.

Why is prompt injection worse for AI agents than for chatbots?

A chatbot that’s fooled says something it shouldn’t. That’s embarrassing. An agent that’s fooled does something it shouldn’t: sends a message, changes a record, leaks data or spends money. Every tool you add is another thing an injection can reach, which is why prompt injection sits at the center of AI agent security.

Nifa shows the risk clearly. It’s a phone assistant that sorts your notifications, answers for you and searches your messages, which means strangers can put text in front of it whenever they like. So I built Nifa so that text inside incoming messages cannot instruct it. A message can be read, sorted, answered or flagged. It can’t give orders.

There’s a symmetry here. Nifa’s scam shield protects people from manipulative messages: it looks for impersonation, phishing links and newly registered domains, and warns when someone asks for a code during a call. The assistant itself has to resist the same kind of manipulation, aimed at the model instead of the person.

How do you prevent prompt injection?

Honestly, you can’t prevent it completely with today’s models. No filter or clever instruction catches every attack. What you can do is contain it, so a fooled model can’t do much harm. These layers work together:

  1. Treat outside text as dataKeep your instructions and fetched content clearly separated, and never let content change the agent’s goal, role or permissions.
  2. Give tools the least power they needRead-only where possible, narrow inputs, and no tool that can do more than the job requires.
  3. Enforce rules outside the modelCheck permissions, inputs and prices in code on the server. On mustashar.ae, checkout is priced on the server, so prices are worked out from the store’s own data and never taken from a conversation.
  4. Put a person before risky actionsIf an action matters, a human approves it. An attacker then has to fool the model and the person.
  5. Keep secrets out of reachDon’t put keys, other users’ data or internal notes anywhere the model can read them. In Nifa, a user’s own AI key is stored encrypted with Android Keystore.
  6. Cap what abuse can costThe mustashar.ae assistant has safety checks and a spending cap. A cap won’t stop an attack, but it limits what a flood of hostile chats can cost.
  7. Show your sourcesGatherChats brings several messaging apps into one inbox, and its AI summaries and reply drafts cite the exact messages they used, so a person can see what shaped a draft before sending it.
  8. Log everything and keep testingRecord every action, and run attack attempts before launch and again on every change.

No single layer is enough on its own, and that’s fine. Each one turns some attacks from a breach into a failed attempt, and together they make the model’s mistakes survivable.

How I test for prompt injection

Before the mustashar.ae shop assistant launched, I tested it against 40 attack attempts alongside 240 normal scenarios. I won’t list the attacks: a published attack set becomes a guide for attackers and stops being a test. Here is how to build and run a set like it:

  1. Write attacks by kindCover every kind of attack above, for every tool the agent has, in every language it speaks.
  2. Run them against the real systemUse the actual assistant with its real tools and realistic data, not the model on its own. Many failures only appear once tools are involved.
  3. Judge actions, not just wordsA pass means the agent stayed in its role, called no tool it shouldn’t, revealed nothing private and changed nothing. A polite refusal that still leaked something is a fail.
  4. Run normal scenarios alongsideDefenses can make an assistant refuse good questions. An assistant that blocks real customers has failed too, which is why the 240 normal scenarios mattered as much as the 40 attacks.
  5. Keep the set private and growingAdd every new trick you see, and never publish the set.
  6. Re-run on every changeA new prompt, model or tool can reopen an old hole. Attack tests belong before launch and again whenever the agent changes.

Treat a failed attack test like a failing unit test: the change doesn’t ship until it passes. Where you can, put the fix in code, such as a narrower tool or a stricter server check, rather than only in the prompt.

LLM security is more than prompt injection

Prompt injection gets the attention, but an LLM app has other weak points, and most of them call for ordinary security done properly:

  • Treat model output as untrusted. Escape it before showing it on a page, and validate it before passing it to another system.
  • Limit where output can point. If the app displays model output, don’t let it load images or links from arbitrary addresses, because that is a quiet way for data to leave.
  • Scope data to the user. The model should only ever see what the person it’s serving is allowed to see.
  • Protect accounts and secrets. GatherChats uses scrypt password hashing, AES-256-GCM encryption, signed webhooks and PKCE sign-in. None of that is specific to AI, and all of it matters in an AI product.
  • Make AI a choice. In GatherChats, the AI summaries and reply drafts are optional.
  • Watch volume and cost. Budgets and spending caps turn some attacks into a nuisance instead of a bill.

The common thread: assume the model can be fooled, and design the system so that fooling it isn’t enough.

Frequently asked questions

Can prompt injection be fully prevented?

Not with today’s language models. They read instructions and data as one stream of text, so no filter catches everything. Treat it the way you treat phishing: reduce it, and design so a single success can’t do real damage. Narrow tools, server-side checks, approvals, spending caps and regular attack testing do that job together.

What is the difference between prompt injection and jailbreaking?

Jailbreaking usually means a user trying to make a model break its own safety rules. Prompt injection is broader: untrusted text, often from a third party, overriding the instructions an application gave the model. For AI agents, injection is the bigger worry, because the attacker may not be the user at all, just someone who sent a message the agent will read.

Does telling the model to ignore malicious instructions work?

It helps a little and is worth doing, but it isn’t a defense on its own. An instruction to the model is a request, and attackers write text designed to talk around requests. Real protection sits outside the model: which tools exist, what they may do, what the server checks and which actions need a person.

How many attack tests does an AI app need?

There’s no magic number. Cover every kind of attack, every tool and every language the app supports, then grow the set whenever you see a new trick. The mustashar.ae shop assistant went through 40 attack attempts and 240 normal scenarios before launch. The normal scenarios matter because a well-defended assistant still has to be useful.

An AI agent, an app, a platform or a growth problem. Tell me what you want to achieve and I’ll tell you how I’d build it.

Start a project