Building with AILesson 62 of 80

Module 7 · Safety, security and the adversary

How prompt injection actually works, and why filters for it fail

In this lesson

  • 10 min read
  • 9 reading sections
  • Knowledge check
  • Lesson 2 of 10 in this chapter

This chapter’s goalThreat-model an AI feature and defend it by architecture: name every channel by which untrusted text reaches the model and every channel by which its output reaches a privileged action or the outside world, restrict each with a control that does not depend on the model’s obedience, layer moderation and abstention with measured false-positive costs, and state plainly what a user must be told and what you cannot promise about a provider’s handling of their data.

This chapter’s media

The film explains chapter ideas. The audiobook narrates the complete teaching text.

FormatEnglishहिंदी
Chapter filmNot availableNot available
Full audiobookNot availableNot available
Separating the reader from the actorUntrustedcontent arrivesA ticket, a webpage, a PDF, atool result.Anyone may havewritten it.Reader call: notools at allAsked only tofill a fixedschema. It canbe persuaded tosay anything,and can donothing.Your codevalidates thestructureEnums against alist, idsagainst theretrieved set,everything elsedroppedActor call:tools, and noraw contentIt sees thevalidatedfields, neverthe sentencethat tried toinstruct itThe action isproposed, nottakenA person seesthe recipient,the amount andthe text, andconfirms orrefusesThe instruction hidden in the document reaches a model that holds no tools, and the model that holdstools never sees the document. Nothing here depends on the model refusing to follow an instruction,which is the only reason it holds.
The instruction hidden in the document reaches a model that holds no tools, and the model that holds tools never sees the document. Nothing here depends on the model refusing to follow an instruction, which is the only reason it holds.

Figure

A little at a time

Explore an idea, try it, then check what stays.

Step 1 of 10Free · no account needed
Lesson steps

There is no boundary in the token stream

The failure-modes lesson gave the rule. This lesson gives the mechanism, because a rule you cannot explain is a rule you will relax under pressure.

A model receives one sequence of tokens. Your system prompt is in it. The user’s message is in it. The web page the model fetched, the PDF the user attached, the email it was asked to summarise, the tool result that came back: all in it, all tokens, all processed by the same attention over the same weights. The roles are labels the API adds, and they shift the model’s behaviour; the system-prompts lesson showed they carry real weight. But weight is not authority. Nothing in the architecture makes an instruction from one role incapable of being followed when it appears in another. An instruction in a retrieved document competes with yours on the merits, and sometimes wins.

Your place and first choices stay on this browser. Classroom credit is recorded separately.

Every lesson here is open. Your progress is recorded once you pass the Python, from Zero, for AI exam.

Stuck on something?Ask Sayali, your AI tutor

How to cite: Addaly (2026) ‘How prompt injection actually works, and why filters for it fail’, in Building with AI. Edited by M. Mun. Addaly. Available at: https://addaly.com/learn/building-with-ai/how-prompt-injection-actually-works (Accessed: 29 November 2026).