NewProject Coding is here — build features and fix bugs in real codebases with an AI assistantSee what's new
Advanced
Prompt Injection Defense
40 min readChapter 2Core
1Prompt injection defenseChapter 1

Prompt injection is text an attacker gets into a model's context that the model then follows as if the developer had written it. Picture a SaaS support-inbox assistant that summarizes customer emails, searches the help center, and drafts and sends replies: anyone who can email the inbox can put words in front of the model.

The model reads the system prompt and the email as one stream of text, so either can steer it. Defenses lower how often injected text is followed, and limit what the application lets the model do when it is followed anyway. Recapping LLM Engineering 101 Guardrails and Safety in one line: direct injection is the user typing the hostile instruction, and indirect injection is the instruction arriving inside content the model reads, such as an email, file, web page, or image. This tutorial is mostly about the indirect kind, where the attacker is not the person using the feature.

You need these defenses when the model reads text your team did not write, when its output triggers an action or renders as markup, or when its context holds secrets or another user's data. A model that only sees your fixed prompt and returns plain text to the same user needs basic input and output guardrails, not this architecture. A user talking the model past its content policy is a jailbreak, covered in Jailbreaks, Misuse, and Abuse.

This question comes up in nearly every AI security round, often as the opener or the first follow-up to a retrieval-augmented generation (RAG) design, and "add a filter" is the answer that fails. A mid-level candidate separates direct from indirect, names delimiting and a classifier, and says why neither is complete. A senior candidate adds privilege separation, output constraints that stop data from leaving, and a way to measure a defense against both attacks and normal traffic.

Your progressLogin to save progress
Spot an issue or have a suggestion?
Data Leakage and Access Control  |

Discussion

Log in to join the discussion.