LLM Defense

Prompt-Injection Defense for RAGllm defenseisolating retrieved content, sanitising documents, and limiting what injected text can trigger. Output Filtering & Canary Tokensllm defensescanning model output for leaks and planting canaries to detect exfiltration.

Defending LLM applications: prompt-injection defense for RAG, and output filtering with canary tokens.

Output Filtering & 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…

Prompt-Injection Defense for RAG

In a retrieval-augmented generation (RAG) pipeline, the documents retrieved from the vector store are spliced directly into the model's context window. Tha

1. Overview

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.

2. Where the defenses sit

Defense Layers Around an LLM Call User promptuntrusted Input checkspolicy, length Retrievervector store Doc sanitiserstrip instructions LLMsystem + context Output filterPII, canaries Responseto user / tools Retrieved text is data, not instructions: mark it, isolate it and never let it grant tool permissions. The output filter is the last chance to stop leaked secrets or canary tokens from leaving the system.

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.

3. Choosing a control

ControlStopsMissesCost
Retrieved-document sanitising and delimitingObvious embedded instructions in RAG sourcesParaphrased or encoded injectionsLow; runs at ingest or retrieval
Least-privilege tools and human confirmationInjected text causing real actionsData exfiltration through the text answerDesign effort; some UX friction
Output filtering (PII, secrets, policy)Leaks in the final answerLeaks via side channels such as URLs the client fetchesLow latency per response
Canary tokensDetects context leakage and followed injectionsDoes not prevent the first leakVery low; a unique string per context

4. Common pitfalls

Related: Encryption & Keys and the AI/ML security overview.