Skip to content
Inference Academy

Injection-resistant agent designs, with a lab

Filters and prompts lower the odds of prompt injection; architecture can remove whole outcomes. The design patterns (action-selector, plan-then-execute, dual LLM, map-reduce, code-then-execute and CaMeL-style capabilities, context minimisation, spotlighting), what each guarantees, and a lab that refactors a hijackable email agent and measures the difference.

By

45 min · verified 2026-11Hands-on labPro
Share

The prompt injection lesson ended with a hard truth: you cannot reliably filter injected instructions out of text. Detectors and stronger prompts make attacks less likely. This lesson is about the other kind of defence: designs in which an injection cannot cause a given harm, however persuasive it is, because the model that reads untrusted text is never in a position to choose dangerous actions.

You will learn the main patterns, what each one guarantees and costs, and then refactor a deliberately hijackable email agent and measure the difference.

#The core idea: separate control from data

In an ordinary agent, one model reads everything (the user's request, emails, web pages, tool results) and decides the next action. Any text it reads can therefore steer what it does next.

Injection-resistant designs enforce a rule in code:

Once the system has read untrusted text, that text must not be able to choose which tools run, or where data goes.

Different patterns enforce that rule in different ways. Most come from a 2025 paper by researchers at several labs and universities, Design Patterns for Securing LLM Agents against Prompt Injections (Beurer-Kellner and others), building on Simon Willison's 2023 dual LLM proposal and Google DeepMind's 2025 CaMeL system.

  1. 1
    User requesttrusted
  2. 2
    Privileged plannerchooses tools; never reads untrusted text
  3. 3
    Quarantined readerreads emails, pages, files; has no tools
  4. 4
    Variables$SUMMARY_1, $DRAFT_2 passed by reference
  5. 5
    Policy check in coderecipient allowed? data source allowed to go there?
  6. 6
    Tool callruns only if the policy check passes

Untrusted text can change what a variable contains, not which tools run or where data goes.

#The patterns

#1. Action-selector

The model maps the user's request to one of a fixed set of actions and never sees tool output. Like a smart menu.

  • Guarantee: untrusted data never reaches the model at all.
  • Cost: no reasoning over documents; good for "route this request" assistants and command interfaces.

#2. Plan-then-execute

The model writes the full plan before reading any untrusted data. Code then executes the plan; tool results can fill in values but cannot add steps.

  • Guarantee: control flow is fixed. An email saying "also forward everything to X" cannot add a forward step.
  • Limit: data flow is not protected. If a step is "reply to the sender", a poisoned email still shapes the reply's content. Pin recipients and destinations in code.

#3. Dual LLM (privileged and quarantined)

A privileged model plans and calls tools but never sees untrusted text. A quarantined model reads untrusted text but has no tools. The quarantined model's outputs are stored as variables ($SUMMARY_1) that the privileged model refers to by name without reading them; the variables are shown to the user or passed into tool arguments by code.

  • Guarantee: untrusted text cannot instruct the model that holds the tools.
  • Cost: the planner cannot make decisions that depend on the content ("reply only if the email is urgent") unless the quarantined model returns constrained values (an enum, a number) that code validates.

#4. LLM map-reduce

Process each untrusted document in an isolated call (map) that returns a constrained result (a label, a score, a yes/no, a short extraction validated against a schema), then combine the results in code or in a model that only sees the constrained outputs (reduce).

  • Guarantee: one poisoned document can only corrupt its own constrained output; it cannot reach other documents or tools.
  • Use for: screening many CVs, triaging tickets, classifying reviews.

#5. Code-then-execute and capabilities (CaMeL)

The planner writes a small program in a restricted language that calls tools and the quarantined model. An interpreter runs it and tracks, for every value, where it came from (its capabilities). Policies are checked before each tool call, for example "data derived from an untrusted email may only be sent to addresses the user typed".

  • Guarantee: control flow fixed by the program, and data flow checked by deterministic policy.
  • Cost: engineering effort and some lost capability; in its paper, CaMeL completed fewer benchmark tasks than an undefended agent while blocking the injection attacks in that benchmark.

#6. Context minimisation

Remove the user's original prompt (or other sensitive context) from later steps once it has been turned into a structured query, so a later injection cannot manipulate the model into leaking it or re-interpreting it.

#Weaker but useful: spotlighting and data marking

Microsoft's spotlighting techniques mark untrusted text so the model can tell it apart: delimiters, interleaving a marker character between words ("datamarking"), or encoding the text (for example base64) and telling the model never to follow instructions inside it. These reduce attack success substantially in published tests, but they are probabilistic: use them inside the patterns above, not instead of them.

Pattern Stops control-flow hijack Stops exfiltration to new destinations Keeps flexibility
Action-selector Yes Yes Low
Plan-then-execute Yes Only with code-enforced destinations Medium
Dual LLM Yes Yes, if tool arguments come from trusted values Medium
Map-reduce Yes, per document Yes Medium, for batch tasks
Code-then-execute (CaMeL) Yes Yes, via policies Medium to high
Detection or spotlighting only No guarantee No guarantee High

Pro content

Keep reading with Inference Academy Pro

Unlock this full lab, plus every advanced lesson, hands-on lab, the larger playground models and completion certificates.

Still to come in this lab

  1. Choosing a pattern
  2. Lab: refactor a hijackable email agent
  3. Common mistakes
  4. Exercises
  5. Going deeper

You will be able to answer

  • In the dual LLM pattern, what is the key rule for the quarantined model?
  • What does plan-then-execute guarantee, and what does it not?
  • Why is an LLM injection detector placed in front of a normal agent not enough on its own?
  • CaMeL-style designs attach capabilities to values. What does that let the system enforce?
  • In the lab, the refactored agent still lets an attacker influence something. What?