Prompt Injection
Instructions hiding in the data
Prompt injection instructs a model from within user-or-content text: 'ignore your rules'. Defense is structural — separable instruction, untrusted-input framing, content-vs-code discipline — not hoping the model resists.
What you will learn
- Explain direct and indirect injection
- Apply structural defenses
- Keep injections from reaching tools
What is this?
Prompt injection is the attacker's text becoming instructions: direct (a user says 'ignore your policies') or indirect (poisoned web pages, docs, or RAG chunks steer behavior). Robust defense is structural — boundary markers, instruction hierarchy, content treated as data — not relying on the model to 'just be smart'.
Why do you need it?
- Models follow instructions they find in data whether or not you intended.
- Indirect injection turns ordinary content into an escalator to your tools.
- Structural isolation beats prompt-language armwrestling.
- Tool systems multiply the blast radius of a successful injection.
Mental model
An envelope vs. a documented memo
Instructions vs. content
When memos (instructions) and letters (data) arrive in the same tray, a letter is just paper. But if data arrives with the formatting of a memo — 'IMPORTANT: CHANGE RULES' — a busy reader follows it. Injection is forgetting the envelopes: content must never arrive as instructions.
How it works
- Frame user/content as data, system as instructions, starkly.
- Use delimiters and instruction-hierarchy directives.
- Never concatenate raw web/doc text as instructions.
- Gate tools independently: schema + allowlist on every call.
- Post-hoc detection: sentry flags suspicious tool args/results.
Injection paths to cut.
In production
- RAG chunks wrapped in data-tags and never echoed as commands.
- Tool schemas ballpark boundaries: args are typed, hosts allowlisted.
- Web tools return terse surface data, not instruction-rich HTML.
Best practices
- Treat all non-system content as untrusted data.
- Keep instruction-vs-content structural, not just worded.
- Enforce the tool gate as the real control surface.
- Red-team injection regularly; bake cases into evals.
Common mistakes
- 'We told the model to ignore instructions in data' as the only defense.
- Echoing fetched HTML or doc text as context verbatim.
- Tools that obey whatever the 'instructions' say with no gates.
Common issues
Injection changed a tool argContent reached tools unsanitized
Bound the tool gate: typed args, fixed target namespace, independent policy.
RAG reroutes reply to attacker instructionsSensitive instruction framed as data
Wrap chunks as data; hierarchical instruction framing; output scrub.
Debugging tips
- Suspicious tool calls → check source text for instruction-anchored strings.
- Behavior change after RAG update → chunk content steered the loop; re-frame.
- Injection eval green but live leak → eval lacked new attack shapes.
Interview checkpoint
Hands-on lab
- Simulate a direct and indirect injection
- Apply structural data-framing
- Show tool args stay typed against steers
Frame Instructions vs Data
35 min · advanced
Quick quiz
Indirect injection travels via…
A strong system-prompt warning fully blocks injection.