How to prevent prompt injection in AI agents (and what can't be prevented)
There is no known way to fully prevent prompt injection: it is unclear if there are fool-proof methods of prevention. What you can do is limit the damage. Give the agent minimum privileges, require a person to approve high-impact actions, run commands in a sandbox, which is off by default in Claude Code, and limit the network. The checklist below starts with a five-step order you can follow today on a Mac.
Prompt injection is text that makes an agent follow instructions its owner never gave. The prompt injection overview has the background and real cases. Here the question is practical: how to prevent prompt injection from doing harm, and what to switch on first.
01What you can and cannot prevent
You cannot stop the text from reaching the model. Models follow any instructions that make it to the model, whatever the source. A cleverly worded rule does not close that gap either. Restrictions in a system prompt, the hidden instructions given to the assistant, may not always be honored.
Rules that the tool enforces are a different kind of control. Permission rules are enforced by Claude Code, not by the model. Treat them as a guard against honest mistakes, not as a wall against a hijacked agent: a deny rule doesn't match the same program by path or inside sh -c, and using WebFetch alone doesn't prevent network access if Bash is allowed. Rule syntax is on Claude Code permissions.
So the aim is not to stop the attack at the model. It is to make a successful one harmless: nothing valuable within reach, nothing important happening without your approval, nowhere to send the data.
02How to prevent prompt injection: the checklist
| Control | What it limits | Where it falls short |
|---|---|---|
| Minimum privileges | What a hijacked agent can touch | Works only if you actually remove the access |
| Few extensions | The tools an agent can call | Each tool you keep is still a way in |
| Human approval | High-impact actions | Only works if you read what you approve |
| Sandbox on | What shell commands can read and write | Off by default in Claude Code; covers shell commands only |
| Network limit | Where data can be sent | A broad allowed domain can still be an exit path |
| Secrets out of reach | What there is to steal | Credential files stay readable by default |
03A sensible order
- Turn the Claude Code sandbox on, since it is off by default. Run
/sandboxin a session. - Check that it works. By default commands can write in the current working directory and its subdirectories. Ask Claude to run
touch ~/sandbox-probe, a write outside that folder. With the sandbox on it fails with Operation not permitted on macOS. - Move keys the agent does not need out of the shell environment.
- Keep a human approval step for high-impact actions. In Claude Code's default auto mode a classifier decides instead of you; switch to manual mode with Shift+Tab for risky work.
- Keep the list of allowed network domains short, and trim extensions and MCP servers you do not use.
04Give the agent less to reach
Restrict the model's access privileges to the minimum necessary for its intended operations. Limit the extensions an agent may call to the minimum necessary. An extension here means any tool or plugin that lets the agent act outside the chat, such as an MCP server, a program that adds tools.
Secrets deserve their own look. The Claude Code sandbox lets commands read the entire computer except certain denied directories, and that default still allows reading credential files. Environment variables, the named values your shell hands to every program it starts, are inherited too, including any secrets in them. If a key sits in your shell environment, an injected command can see it.
05Keep a person in the approval loop
Require a human to approve high-impact actions before they are taken. In practice that means leaving approval prompts on for actions you could not undo, such as deleting files or sending data out. Codex shows the idea in its on-request policy: the agent works inside the sandbox by default and asks when it needs to go beyond that boundary. In Cursor, the Run Everything mode runs every tool call automatically, so you accept the risk and get zero prompts.
Approval has limits. A classifier is a second model that reviews each action, and it can make mistakes. Cursor's Auto-review is not a security boundary. Claude Code auto mode reduces prompts but does not guarantee safety. One Claude Code advisory, CVE-2025-55284, came from an overly broad allowlist of safe commands, a list of commands that run without a prompt, which made it possible to bypass confirmation prompts, read a file and send its contents over the network. If you are tempted to turn prompts off altogether, read dangerously skip permissions first.
06Put a boundary around commands
A sandbox is a boundary that the operating system enforces around the commands an agent runs. In Claude Code it holds regardless of what the model chose to run, even if an allowed command does more than its name suggests. On macOS it uses Seatbelt, a sandbox framework built into the system.
Know the gaps:
- Shell commands only. File tools, MCP servers and hooks run outside the sandbox.
- Retry outside. Claude may retry a failed command with the dangerouslyDisableSandbox parameter, which runs it outside the sandbox. Cursor works similarly: a failed sandboxed command can be rerun outside it, and the classifier reviews the rerun.
- Not a full wall. Sandboxing reduces risk but is not a complete isolation boundary.
The tools differ in their defaults:
| Tool | Isolation on macOS | Network |
|---|---|---|
| Claude Code | Seatbelt, off by default | Allowed domains start empty |
| Cursor | Seatbelt | Your own allowlist plus Cursor's built-in defaults for package managers and language tools |
| Codex | Built-in Seatbelt | Off by default |
Setup lives on Claude Code sandbox, Cursor sandbox and Codex sandbox. Another option is a Docker sandbox: Docker Sandboxes run agents in a microVM, a small virtual machine with its own filesystem and network. Steps are on Claude Code in Docker.
07Limit where data can go
Effective sandboxing requires both filesystem and network isolation. Without network isolation, a compromised agent could exfiltrate sensitive files like SSH keys. In the Claude Code sandbox, connections go through a proxy on your machine, a relay that checks each host against your allowed domains, which start empty. An allowlist is that list of sites commands may contact.
Keep it short. Allowing broad domains such as github.com can create paths for data exfiltration, which means sending data out. Cursor's built-in allowlist includes github.com, so review what yours allows. By default the Claude Code proxy does not inspect TLS, the encryption on https connections, so the contents of encrypted connections are not examined.
For Codex, the default is network off. Use caution when enabling network access or web search.
08Can detection catch it?
Not reliably. The text need not look like anything: injections do not need to be human-visible or readable, as long as the model parses the content. You cannot catch them by reading files.
Detection tools in this space are classifiers. Claude Code auto mode runs one that blocks actions that appear driven by hostile content Claude read, and Cursor's classifier can allow a call you would have blocked, or block a call you would have allowed. Willison's heading on guardrail products is "Guardrails won't protect you". Use detection as a second layer behind the limits above.
09What this adds up to
None of this means you can prevent prompt injection. It means a successful one finds little to take. The settings for each tool are on prompt injection in Claude Code, Cursor and Codex, and how the text arrives is on the indirect injection page.
None of these controls tells you what already ran. Artificial Immunity's free, open-source core watches agent sessions and every program start on your Mac and reports what it finds, without blocking anything.
FAQ
Will prompt injection ever be solved?
Nobody can promise it. It is unclear if there are fool-proof methods of prevention, and Simon Willison writes that we still don't know how to 100% reliably prevent it. Plan to limit the damage.
Which prompting technique can protect against prompt injection attacks?
Wording alone does not settle it. Restrictions in a system prompt may not always be honored and could be bypassed via prompt injection. Controls that Claude Code enforces itself, such as permission rules, are a different kind of control, with limits described on this page.
How do you handle prompt injection attacks?
Assume one will land and shrink what it can reach: minimum privileges, human approval for high-impact actions, a sandbox and a network limit.
What tools can be used to detect prompt injections?
Classifier models that review actions exist in Claude Code auto mode and in Cursor Auto-review. Auto mode does not guarantee safety, and Cursor's classifier can make mistakes. Treat them as an extra layer.
How to test against prompt injection?
Test the boundary instead of the wording. With the Claude Code sandbox on, ask Claude to run touch ~/sandbox-probe: it should fail with Operation not permitted on macOS.
Does a sandbox prevent prompt injection?
No. It reduces risk but is not a complete isolation boundary. It limits what commands can touch if an injection succeeds.
Related guides
Sources
- OWASP LLM01: Prompt Injection
- OWASP LLM06: Excessive Agency
- Simon Willison: the lethal trifecta
- Claude Code docs: sandboxing
- Claude Code docs: permission modes
- Cursor docs: run modes and sandbox
- OpenAI Codex docs: sandboxing
- OpenAI Codex docs: agent approvals and security
- OpenAI Codex docs: internet access
- Claude Code docs: permissions