Guardrails for AI agents: permissions, sandboxes, hooks and review
AI agent guardrails for a coding agent on your Mac are layers, not one switch: permissions, a sandbox, hooks, human review, network limits and a record of what ran. Each layer stops a different kind of mistake. The gaps sit between them. Deny rules are enforced by the program, not the model. The sandbox covers shell commands only. First action: check that your agent's sandbox is actually on, because in Claude Code it is off by default and /sandbox turns it on.
AI agent guardrails differ from the ones around a chatbot. The agent can run commands on the Mac where your SSH keys, browser sessions and projects live. This page is a map. It has one row per layer, a few lines on where each leaks, and a link to the page that covers it in full. For the general idea of guardrails and their types, see AI guardrails.
01The layers at a glance
Good AI agent guardrails answer six questions. May it run? What can it touch? Did my check pass? Who approves? Where can it connect? What did it do?
| Layer | The question it answers | Full page |
|---|---|---|
| Permissions | May this tool call run at all? | AI agent permissions |
| Sandbox | What can a command touch? | AI agent sandbox |
| Hooks | Did my own check pass? | Claude Code hooks |
| Review | Does a person or a second model approve? | Auto mode |
| Network | Where may a command connect? | AI agent firewall |
| Monitoring | What actually ran? | AI agent monitoring |
The sections below take the layers in this order. Each says what the layer is good at and where it leaks.
02Permissions: may this run at all
Permissions are the layer you meet first: rules and modes that decide how often the agent asks before acting. A deny rule is a rule that forbids a tool call. Rules are checked in a fixed order: deny first, then ask, then allow, and the first match wins. They are enforced by Claude Code, not by the model. Claude Code's modes decide how often it asks: default, acceptEdits, plan, auto, dontAsk and bypassPermissions. The other agents have their own forms. Cursor has three run modes: Auto-review, Allowlist and Run Everything. Codex has approval policies: on-request works inside the sandbox and asks when it needs to go beyond it, and never does not stop for approval prompts.
The limit to remember: a permission prompt is not a fence. An approved Bash command can still write anywhere your user account can, and a deny rule misses the same program called by its path. Modes, rule syntax and examples are on AI agent permissions.
03Sandbox: what a command can touch
A sandbox is an area the operating system fences off. On a Mac, Claude Code, Cursor and Codex use Seatbelt, the sandbox framework built into macOS. Because the OS enforces it, it holds whatever the model decided to run.
Defaults differ: in Gemini CLI you turn the sandbox on with a flag, a variable or a setting, and its default macOS profile confines writes to the project folder but allows broad reads and network access. Claude Code's sandbox starts off, and it covers shell commands only, so file tools, MCP servers and hooks run outside it. The four agents are compared on AI agent sandbox.
04Hooks: your own check, every time
A hook is a script you write that the agent runs at a fixed moment, such as just before a tool call. It gives deterministic control, because it does not depend on the model choosing to follow it. A PreToolUse hook that times out does not block the call unless you set onFailure: "block" on it. A hook on file edits does not see the same file changed from the shell. The docs therefore add an Edit deny rule, which catches some of those shell commands. Examples and exit-code rules are on Claude Code hooks.
05Review: a person or a second model approves
The first kind of review is a person. OWASP recommends requiring a human to approve high-impact actions before they happen. The second is a model. Claude Code's auto mode uses a classifier, a separate AI model that reads each action before it runs, and Cursor has a similar Auto-review.
Treat the model reviewer as a convenience. Auto mode does not guarantee safety. Cursor's Auto-review is not a security boundary: it can allow a call you would have blocked, or block a call you would have allowed. What auto mode blocks and allows is listed on auto mode.
06Network: where a command can connect
A fence on files alone is half of one. Network limits cover egress, meaning traffic going out of your machine. Without network isolation a compromised agent could send out sensitive files such as SSH keys. The defaults differ: Codex starts with the network off, while Gemini CLI's default macOS sandbox profile allows broad network access. An allowed domain stays open, and a broad one such as github.com can become a path out. The per-agent settings are on AI agent firewall.
07Monitoring: what actually ran
The layers above decide what may happen. None of them is a record of what did. Claude Code keeps session transcripts locally in plaintext under ~/.claude/projects/ for 30 days by default. Where to look on the Mac is on AI agent monitoring. If something already went wrong, Claude Code undo covers the way back.
08How to set up AI agent guardrails in order
Start from the hard limits and end with the soft ones.
- Turn on the sandbox: run
/sandboxin a Claude Code session. - For a file that must never change, such as
.env, add the deny ruleEdit(/.env)underpermissions.denyin.claude/settings.json. The rule also blocks some shell commands that change the file, not all of them, so keep the sandbox on from step 1. - For a check that must always run, write a hook. Claude Code hooks has an example to start from.
- For an action you cannot undo, keep a human approval step, and use a model reviewer only as a helper.
If the agent runs without prompts, with the --dangerously-skip-permissions flag, the layers above no longer ask you anything, so use it only in an isolated environment. The full story is on dangerously-skip-permissions.
09What the layers still leave open
The gap that every layer shares is prompt injection, meaning instructions slipped into text the agent reads. It is unclear whether fool-proof prevention exists. The fallback is to restrict the model's access to the minimum it needs. The page on preventing prompt injection covers the working steps.
The second shared gap is extensions. An MCP server is a helper program that gives the agent extra tools. Without sandboxing and consent, a malicious startup command in a client configuration is possible. Codex notes that project-defined MCP servers can run commands outside its sandbox. See MCP security.
Layers decide what an agent may do and say little about what happened afterwards. The last layer on this map is seeing what happened: Artificial Immunity's free, open-source core watches Claude Code, Cursor, Codex, MCP servers and git hooks on your Mac and reports what it finds.
10The short version
AI agent guardrails work as layers: permissions, a sandbox, hooks, review, a network limit and a record of what ran. Start with the hard layers that the system enforces. Keep a person on the actions you cannot undo. Open the linked page for each layer you are missing.
FAQ
How to implement guardrails in agentic AI?
Work from the bottom up. Turn on a sandbox or run in a container, add deny rules for files that must never change, add a hook for checks that must always run, and keep a human approval step for actions you cannot undo.
Do permission rules and a sandbox do the same job?
No. Permission rules decide whether a tool call is allowed. A sandbox is a fence the operating system builds around shell commands, so it also limits what an approved command can touch.
Is one layer enough?
No. Each layer leaves gaps: the sandbox covers shell commands only, a deny rule can miss a command written another way, and a hook that times out does not block unless it is set to. Together they overlap.
Where do I start on a Mac?
Check whether your agent's sandbox is on. In Claude Code it is off by default and you turn it on with the /sandbox command.
Can a hook still stop a command when prompts are off?
Yes, in Claude Code. A PreToolUse hook that returns a deny blocks the tool even with --dangerously-skip-permissions or in bypassPermissions mode.
Related guides
Sources
- Claude Code docs: permissions
- Claude Code docs: permission modes
- Claude Code docs: sandboxing
- Claude Code docs: hooks guide
- Claude Code docs: hooks reference
- Claude Code docs: development containers
- Claude Code docs: data usage
- Cursor docs: run modes
- OpenAI Codex docs: sandboxing
- OpenAI Codex docs: approvals and security
- Gemini CLI docs: sandbox
- OWASP GenAI: LLM01 Prompt Injection
- OWASP GenAI: LLM06 Excessive Agency
- Model Context Protocol: security best practices
- Artificial Immunity