Prompt injection has no single fix. It's mitigated by layered defenses — input hardening, system prompt design, output control, tool and privilege, runtime monitoring. Each pattern here is a one-line summary plus a minimal code sample plus a primary-source citation. Pair with our Prompt Injection Puzzle which teaches the attack vocabulary; this deck teaches the defense vocabulary. Share any pattern via its #/pattern/<slug> permalink.
Every attack class in the Puzzle is mitigated by a stack of three to five patterns from different layers — not a single magic defense. Direct injection is stopped by input sanitization plus instruction precedence plus canned refusal; indirect injection needs dual-LLM separation plus output schema validation plus secret-leak scanning; tool-use hijack needs least-privilege plus human-in-the-loop plus tool allowlist. Pick your threat model, pick a pattern from each layer, stack them.
The code samples are deliberately minimal — illustrative shapes, not production-ready snippets. Each assumes you'll adapt it to your model provider, your runtime, and your data. Citations link to the primary source that best describes the pattern; follow them for rigorous treatment.
#/pattern/<slug>. Open that URL and the page scrolls to the matching card and highlights it for three seconds. The fragment is preserved across reloads and is share-safe — nothing leaks server-side.bc1qs04leape97ner4wqa98n94l9n0gv9aa84eg4ux — copy button in the tip jar below. No accounts, no signup, no middleman. If this deck saved you from shipping a broken defense, a few thousand sats is a fair thanks — but everything on Promptshelf stays free whether or not you tip.Saved you from shipping a broken defense? Tip the maker in BTC — no account, no signup, just paste.
bc1qs04leape97ner4wqa98n94l9n0gv9aa84eg4ux