Defending LLM applications: prompt-injection defense for RAG, and output filtering with canary tokens.
Ingest-time redaction ( see the redaction page ) removes PII and privileged content from prompts before inference. But an LLM can still synthesize or…
In a retrieval-augmented generation (RAG) pipeline, the documents retrieved from the vector store are spliced directly into the model's context window. Tha
An LLM application has an unusual attack surface: the same channel carries both instructions and data. Anything placed in the context window, whether a user message, a retrieved document, a web page or a tool result, can contain text that the model treats as a command. Defending these systems therefore means controlling what enters the context, limiting what the model is allowed to do with it, and inspecting what comes out. This section covers two complementary controls. Prompt-injection defense for retrieval-augmented generation (RAG) focuses on the input side, where poisoned documents in a vector store try to hijack the model. Output filtering with canary tokens focuses on the output side, catching responses that leak sensitive data or prove that hidden instructions were followed.
No single filter stops prompt injection, because the model cannot reliably tell instructions from data. The practical approach is defense in depth. Before the call, constrain the user input and sanitise retrieved passages, delimit them clearly, and keep privileged instructions in the system prompt. During the call, give the model the minimum tool permissions it needs and require confirmation for side-effecting actions. After the call, scan the response for sensitive patterns, secret values and canary strings that should never appear, and log every block for review.
| Control | Stops | Misses | Cost |
|---|---|---|---|
| Retrieved-document sanitising and delimiting | Obvious embedded instructions in RAG sources | Paraphrased or encoded injections | Low; runs at ingest or retrieval |
| Least-privilege tools and human confirmation | Injected text causing real actions | Data exfiltration through the text answer | Design effort; some UX friction |
| Output filtering (PII, secrets, policy) | Leaks in the final answer | Leaks via side channels such as URLs the client fetches | Low latency per response |
| Canary tokens | Detects context leakage and followed injections | Does not prevent the first leak | Very low; a unique string per context |
Related: Encryption & Keys and the AI/ML security overview.