Prompt injection explained simply is this: a trick that hides instructions inside data an AI system reads, so the system follows the hidden instructions instead of the ones its user or developer gave it. The central question is whether an AI can tell an instruction from ordinary text, and the short answer is that it cannot do so reliably, because a language model treats all text as input, with no built-in way to know which words are commands and which are content.
That weakness matters because AI systems now read emails, web pages, documents and databases on our behalf, and act on what they find. When an attacker slips a command into one of those sources, the AI may carry it out as if it came from a trusted person. This guide explains how prompt injection works, the forms it takes, and the defences against it.
Key takeaways
- Prompt injection hides instructions inside data, so an AI follows commands its user never gave.
- Language models cannot reliably tell instructions apart from ordinary text.
- Indirect injection, through web pages and documents, is the commonest route in real systems.
- The risk grows sharply when an AI can take actions, not just answer questions.
- No single defence is enough: layered checks, tool limits and human review work together.
What prompt injection is
A language model takes in a prompt, a block of text, and produces a response. That prompt usually mixes several sources: system instructions from the developer, the user’s request, and data pulled in from elsewhere, such as a document or a web page. The model reads all of it as one stream of text, with no reliable way to mark one part as a command and another as content that should only be read or summarised.
Prompt injection exploits that blending. An attacker writes text that looks like an instruction, places it where the AI will read it, and the model obeys it. The classic example is a web page with a hidden sentence telling the AI to ignore its earlier instructions. A user who asks the AI to summarise that page may also get the AI following the page’s hidden orders.
How an injection actually works
The mechanics are simple, which is part of the problem. An attacker needs three things: a place where text they control will be read by the AI, a sentence written as an instruction, and a model that follows instructions. The web provides endless places for the first, from web pages and comments to reviews and documents. The second is just writing, and the third is the default behaviour of every helpful AI.
A typical attack starts with the victim doing something ordinary: asking an assistant to summarise a document or research a topic online. The AI fetches the source, and inside it sits a sentence telling it to disregard earlier instructions, include a particular link, or send a copy of the conversation elsewhere. Because request and source both arrive as plain text, the injection can win. Some injections are blunt; others are subtle, phrased as helpful guidance, and often more effective, because they do not look like an attack.
Why models fall for it
Language models are trained to be helpful and to follow instructions, and that training is both why they are useful and why they are vulnerable. A model does not ask who wrote the words, and it has no memory of where each part of its input came from. A system instruction, a user message and a web page all arrive as text, weighed by wording and position, not by authority.

Direct and indirect injection
Direct injection comes from the user: a prompt typed into a chatbot to override the system’s rules, for example to reveal its instructions or produce content it was told not to produce. It is the form most people have seen, often a test of the model’s guardrails rather than an attack on anyone else.
Indirect injection comes from data the AI reads. The attacker never talks to the AI; instead they plant instructions in a web page, an email, a document or a database record, and wait for a victim’s AI to read it. This is the more dangerous form, because the victim does nothing wrong and may never see the text. It is harder to defend against, because reading outside data is the point of many systems. Our guide to retrieval-augmented generation explains how AI systems pull in outside documents, where injected text can enter.
What an attacker can gain
The simplest gain is control of the output. An injection can make an AI produce a false summary, recommend a particular product, include a malicious link, or present invented facts as true. That is a problem for anyone who trusts AI summaries, and worse when the output is published.
A more serious gain is access to information or action. An injection can tell the AI to include private details from the conversation or documents it has read, and if that reaches the attacker, the information leaks. When an AI can also send messages, change files or make purchases, an injection can turn those powers against the victim, which is why the permissions given to AI systems are a central part of security. Our guide to AI agents in production explains how these systems are built and why their actions make security harder.
Comparing the main types of injection
| Injection type | How it reaches the AI | Main danger | Best defence |
|---|---|---|---|
| Direct injection | Typed by the user into the chat | Guardrails bypassed, rules ignored | Strong system instructions, output checks |
| Indirect injection | Hidden in a web page or document | Victim tricked unknowingly | Treating retrieved data as untrusted |
| Instruction override | Commands the AI to ignore earlier rules | Intended behaviour lost | Layered instructions, confirmation steps |
| Data exfiltration | Commands the AI to reveal private details | Confidential information leaked | Limiting what the AI can read and share |
| Tool hijacking | Commands the AI to misuse its tools | Emails sent, files changed, money moved | Least-privilege access, human approval |
Why prompt injection is hard to stop
The first difficulty is that the boundary between instruction and data is a convention, not a technical feature of language models. Developers write system prompts saying data should be treated as data, but the model receives those words as text like everything else. A convincing counter-instruction can compete with the system prompt, and sometimes win.
The second difficulty is that filtering is a moving target. A filter that blocks known attack phrases will miss new phrasing, and an attacker can reword an instruction endlessly. The third difficulty is that strict defences break useful behaviour: an AI that refuses to follow any instruction found in data would also refuse to follow legitimate instructions inside documents, such as a recipe a user asked it to carry out. Drawing that line is a judgement call, and models make it imperfectly.
Defences that actually work
No single defence stops prompt injection, so the serious answer is layers. The first layer is separation: keep instructions and data apart as much as the system allows, and tell the model clearly which parts of its input are data. Some systems wrap retrieved content in markers, but the model can still be talked past them.
The second layer is limiting what the AI can do. An AI that can only answer questions can be embarrassed, not weaponised. One that can send email, delete files or move money needs tight limits: narrow permissions, read-only access, and approval steps for anything that cannot be undone. The principle is least privilege, giving the AI only the access it needs.
The third layer is checking the output before it acts, and the fourth is testing. A second model, or a set of rules, can review what the AI plans to do and flag anything from untrusted data. Security teams also probe their own systems with crafted injections, and repeat the testing whenever the system changes, since a new tool or data source can open a new route in. Our guide to prompt engineering covers how prompts are structured, and why that shapes how easily they can be overridden.
What good practice looks like
Good practice starts with assuming injection will happen. A team that designs as if its AI will one day read a poisoned document builds in limits from the start, not after an incident. That means deciding what the AI may do, what data it may read, and what needs human approval, before the system goes live.
Monitoring is the second pillar. Teams should log what their AI systems do, especially actions based on retrieved data, and review those logs for surprises. An assistant that suddenly sends an email or includes a strange link should leave a trace that someone notices.
Prompt injection checklist
- Treat all retrieved data as untrusted, and tell the model so.
- Give the AI least-privilege access: read-only where possible, no tools it does not need.
- Require human approval before irreversible actions, such as sending, deleting or paying.
- Check AI output before it is published or sent, especially output from outside sources.
- Log AI actions, and review them for behaviour driven by retrieved data.
- Test your systems regularly with direct and indirect injection attempts.
- Keep system instructions short and layered, so one override does not remove all rules.
- Warn users that AI summaries can be steered by the documents they read.
- Have a response plan for a successful injection: revoke access and warn affected people.
Frequently asked questions
What is prompt injection?
It is an attack that hides instructions inside data an AI reads, such as a web page or document, so the AI follows them instead of its own instructions. The AI cannot reliably tell which text is a command and which is ordinary content.
What is the difference between direct and indirect prompt injection?
Direct injection is typed by the user, usually to bypass the system’s rules. Indirect injection is planted in data the AI reads, so the victim’s AI is tricked without the victim doing anything wrong.
Can prompt injection steal my data?
It can, if the AI can reach private information and send its output somewhere the attacker can see it. An injection can tell the AI to include private details in an answer or message, so limiting what the AI can read and share is a key defence.
Why can’t AI companies just fix prompt injection?
Because language models read instructions and data as one stream of text, with no built-in way to tell them apart. Filters help, but attackers keep finding new phrasing, so the practical answer is layered defences and limits on what the AI can do.
Is prompt injection a real risk or only a research problem?
It is a real risk for any AI system that reads outside data and can take actions. Chatbots that only answer questions face a limited risk, but assistants that read emails, and agents that send messages or change files, can be turned against their users.
How can I protect myself from prompt injection?
Be cautious with AI output built from outside sources, check the sources before acting on it, and do not give an AI assistant access to sensitive data or irreversible actions. For teams, the defences are least-privilege access, human approval, output checks and regular testing.
The bottom line
Prompt injection explained in one line is this: because AI systems read instructions and data as the same kind of text, anyone who can place words where an AI will read them can try to give it orders, and the only reliable answer is to limit what the AI can do, check what it produces, and keep a human in charge. The threat will not disappear, but a system built on that assumption can still be useful.







