Learn the MachineDansk

Explained with an interactive demo

What is prompt injection?

Prompt injection is when text a language model reads contains instructions that make it do something other than what the developer and the user wanted. It is possible because instructions and data end up in the same text. Build the prompt for a small email assistant below and see what each defence changes.

The demo needs JavaScript. The text below explains what it shows.

Embed this demo on your site

Copy the code into your page. The demo loads from learnthemachine.com, so it stays up to date, and the link under it shows where it comes from.

Instructions and data in the same channel

A language model receives one long sequence of tokens. The developer’s instructions, the user’s question and everything the app fetches (emails, web pages, documents, tool results) are joined into the same text. The chat template marks where each role starts with special tokens such as <|im_start|>system, but inside a message nothing separates an instruction from a sentence that merely looks like one. Press “Show it as the model sees it” in the demo: the colours exist only for you.

It resembles SQL injection, where data is read as code. The difference is that a database has a strict grammar, so data and code can be separated completely. Natural language does not.

Direct and indirect prompt injection

In direct prompt injection the user writes the instructions, for example to make a chatbot reveal its system prompt. In indirect prompt injection the instructions come from a third party, through content the model reads on the user’s behalf: an email, a web page, a PDF or a document retrieved by a RAG system. The user does nothing wrong and may never see the text. The hidden instruction in the demo sits in an HTML comment that an email program does not display.

Jailbreaks are related but not the same: there the user tries to make the model break its own rules. In prompt injection the target is the app, and in indirect prompt injection its user as well.

Why a filter is not enough

The first thing many people try is to block known phrases such as “ignore previous instructions”. Switch the filter on in the demo: it stops the hidden instruction, but not the disguised one, which says the same thing in other words. An attack can be rephrased, translated, misspelt or split up, and the filter also stops innocent emails that happen to use the words. Some use another language model as the filter instead. That catches more, but it can be fooled too.

Defences that help

Mark data as data. Put outside content between clear tags and say in the instructions that the text is data. Models are increasingly trained to give system and developer messages priority over text further down (an instruction hierarchy). It helps, but it is learned behaviour, not a rule the model cannot break.

Grant as few permissions as possible. The strongest defence in the demo is removing the tool: if the assistant cannot send email, an injection can at most produce a wrong summary. In general, give only the tools and data access the task needs.

Let a person approve actions that cannot be undone. Sending, paying or deleting should need a clear approval that shows exactly what will happen. It only works if the user actually reads it.

Separate reading from acting. Some systems let one model read the untrusted content without access to tools, and another model act without seeing that content directly.

No known method removes the risk completely. OWASP puts prompt injection at the top of its list of security risks for LLM applications. So the question is not only “can the model be fooled?” but also “what is the worst that can happen when it is?”.

How to test it

The claim “our assistant is protected” should be measured, not assumed. Build paired test cases: the same email with and without an embedded instruction, several wordings of the same attack, and controls where nothing should happen. Measure how often the model attempts the action, repeat over several runs and report the uncertainty. A few successful tries do not prove that a defence works.

Measure model behaviour as in AI safety

In the AI safety lab you start from a small chat model’s answer to a request for a made-up password, and then learn to measure behaviour properly: paired examples, controls, several runs and uncertainty. As an extra project you evaluate open language models on your own prompts. The lab requires full access. To see the chat template’s real markers first, the free Tokenization lab shows them token by token.

See the AI safety labStart free with Tokenization

More explainers