How do you defend an AI agent against prompt injection?

Answered by Falk Gottlob · Reviewed · 2 min read

THE SHORT ANSWER

Assume some injections will get through, and design so they can't do much harm. Contain the agent first: limit what it can reach, and block the ways data could leave with fixed rules, not model judgment. Then add layers on the model side, like marking untrusted text and running a detector. Save human approval for the few actions that really matter, because people stop reading prompts they see all day. Anthropic found users approved about 93% of permission prompts. Last, keep every attack that works as a permanent test.

Start from the honest premise. Prompt injection won't be fully fixed. The UK's National Cyber Security Centre says so, and so do the AI labs. So the question isn't how to stop every injection. It's how to make sure a successful one can't do real harm.

Andrew Paverd of Microsoft's security response team puts the goal plainly: even if some prompt injections succeed, they shouldn't lead to security impact. Here's how I'd layer it.

Contain first. Anthropic's rule is to design for containment at the environment layer, then steer the model. Give the agent least privilege, meaning only the access a task needs. Run it in a sandbox. Deny network access by default. Anthropic says its sandbox cut permission prompts by 84%.

Close the exits. Exfiltration means data leaving where it shouldn't. Microsoft blocks known paths with fixed rules, like certain image links and untrusted URLs. Don't ask the model to decide.

Then add model-side layers. Microsoft's spotlighting marks untrusted text so the model can tell where it came from. A detector like Prompt Shields flags likely attacks. These lower the odds. They don't close the door.

Use people sparingly. Anthropic found users approved about 93% of Claude Code's permission prompts. Save approval for actions that are hard to undo or that send things outside.

Write it down per agent. I use four lines: what it may do alone; when a person joins, and how; who reads its work, and how often; who can stop it today.

Then test it like everything else. Every attack that works becomes a permanent case in your regression suite.

The build detail is in AI Security for Engineers. Start the series at AI Security From Zero, or browse the AI security hub.

SOURCES

THE LONG VERSION

RELATED ANSWERS

If this was useful, The AI Product Transformation is where I share more of it.

One email every Monday with the week's best posts and what changed on the site.

Unsubscribe anytime. Read past issues.