kilazi. wiki
Open source

agent-cage: run Claude Code or Codex truly read-only on macOS

Tool allowlists leak. agent-cage wraps the agent in a macOS seatbelt profile, so it can read everything but write only where you say.

Install
curl -fsSL https://raw.githubusercontent.com/kilazi/agent-cage/main/install.sh | sh

Leer en español

The short answeragent-cage is a free shell script that runs Claude Code, Codex or any command on macOS under sandbox-exec. Every file write is denied except the agent's own state, temp dirs and the folders you pass with -w. The kernel enforces it, so a global allow rule, a hook or a prompt cannot get around it. Add -n to cut the network too.

What problem does agent-cage solve?

You want an AI coding agent to read a repo and report, without touching anything. Or you want it to write a report into one folder and nowhere else. The usual tool for that is the agent's own permission system: an allowlist of tools and commands. That is not a cage.

Allowlists leak. In Claude Code, an allow rule in your user settings applies to every session, including the ones you start with claude -p and a narrow --allowedTools flag. If your settings allow Bash, a "read-only" session can still run touch, rm or curl. Only the Write tool is blocked. A flag cannot narrow a rule that is already allowed globally, and any shell command can write a file.

agent-cage moves the rule out of the agent and into the operating system. It runs the agent under a macOS seatbelt profile that denies every file write except the folders you name. The kernel enforces it, so it does not matter what the agent's settings, hooks or prompts say. It is free and open source at github.com/kilazi/agent-cage.

1shell script, no dependencies
0writes outside the folders you allow
1flag to also cut the network

How does it work?

macOS ships sandbox-exec, the command-line front end to the same sandbox (seatbelt) that App Store apps run in. agent-cage generates a profile and runs your command under it:

  1. Allow everything by default, so reads, the Keychain, process launches and the network keep working.
  2. Deny every file write.
  3. Allow writes again only in: device files (/dev), the temp dirs (/private/tmp, /private/var/folders, $TMPDIR), the agent's own state (~/.claude, ~/.claude.json, ~/.codex, ~/.cache, ~/Library/Caches) and every folder you pass with -w.
  4. Deny again the files that would let the agent change its own rules for a later session: ~/.claude/settings.json, ~/.claude/settings.local.json, ~/.claude/CLAUDE.md, ~/.claude/hooks/, ~/.codex/config.toml and ~/.codex/AGENTS.md.
  5. With -n, deny the network too, except local unix sockets.

The profile applies to the agent and every process it starts: its shell, git, python, npm, anything. A blocked write fails with Operation not permitted. Both Claude Code and Codex read that as a normal error and carry on.

Run agent-cage --print to see the exact profile for your machine.

How do I set it up?

One command, then check it on your Mac:

curl -fsSL https://raw.githubusercontent.com/kilazi/agent-cage/main/install.sh | sh

The installer puts one script in ~/.local/bin (change it with AGENT_CAGE_BIN) and runs agent-cage --selftest, which tries a write outside the cage, a write in your home folder, a write in an allowed folder, a read and a delete, and prints pass or fail for each.

Then put agent-cage in front of the command you already use:

agent-cage claude -p "review this repo and list the risks"
agent-cage -w ./out claude -p "write the audit to out/report.md"
agent-cage -w . codex exec "fix the failing test"
agent-cage -n -w ./out python3 untrusted_script.py

Options: -w DIR (repeatable, created if missing), -n to block network, --no-state to also block the agent's state dirs (for plain commands that need none), --print, --selftest. Environment: AGENT_CAGE_WRITE=dir1:dir2 adds writable dirs, AGENT_CAGE_STATE=dir1:dir2 replaces the default state dirs (for another agent), AGENT_CAGE_DEBUG=1 prints the profile before each run.

Requirements: macOS (it uses /usr/bin/sandbox-exec) and bash, both built in.

What does it not protect?

Be clear about the edges, so you do not rely on a wall that is not there.

  • Temp dirs are writable. /tmp and $TMPDIR stay open, because almost every tool needs them. If your project lives under /tmp, the cage does not protect it. Keep projects in your home folder.
  • Reads are open. The agent can still read any file your user can read, including secrets in your home folder. agent-cage stops changes, not reading. If that matters, run the agent as a separate macOS user.
  • Network is open by default. A read-only agent can still send data over the network. Add -n for plain commands. Claude Code and Codex need the network to talk to their model, so -n stops them from working; for them the cage is about writes.
  • Agent state is writable. Session logs, caches and login tokens in ~/.claude and ~/.codex can change. Settings, hooks and global instructions cannot.
  • No nested sandboxes. macOS does not let a sandboxed process apply a second sandbox. If Codex reports sandbox_apply: Operation not permitted, run it with --sandbox danger-full-access: agent-cage is the sandbox now.
  • sandbox-exec is deprecated. Apple marks it deprecated but still ships it and uses the same engine for its own apps. --selftest tells you if it stops working on a future macOS.

Can I reuse the code?

Yes, under Apache-2.0. Keep LICENSE and NOTICE, and credit the repo in your README or NOTICE with one line: Uses agent-cage by Kilazi (https://github.com/kilazi/agent-cage). The same rule is written for coding agents in the repo's AGENTS.md. See also done-check, which makes the agent run your tests before it says done.

Get agent-cage

curl -fsSL https://raw.githubusercontent.com/kilazi/agent-cage/main/install.sh | sh

Open the repo on GitHub

Questions people ask

Can I reuse it?

Yes. agent-cage is Apache-2.0: copy it, change it, ship it in your own product. Keep the LICENSE and NOTICE files with the code, and add one line to your README or NOTICE: Uses agent-cage by Kilazi (https://github.com/kilazi/agent-cage). Coding agents get the same rule in the repo's AGENTS.md.

Is this better than Claude Code's --allowedTools or permission modes?

They solve different problems. Permission modes decide which tools the agent may call, and an allow rule in your user settings applies to every session, so a flag cannot narrow it. agent-cage decides what any process can write, whatever tool or command started it. Use both: permissions to keep the agent focused, the cage as the wall.

Does it work with Codex?

Yes. codex exec --sandbox read-only is already a real sandbox, but it also turns off shell network. agent-cage allows writes in one folder and keeps the network on. If Codex reports sandbox_apply: Operation not permitted, run it with --sandbox danger-full-access, because macOS does not allow a sandbox inside a sandbox.

Can the agent turn the cage off?

No. The kernel applies the sandbox before the agent starts, and a process cannot remove its own sandbox. The agent also cannot edit its settings, hooks or global instructions to escape in a later session, because those files are denied too.

Does it stop the agent from reading my secrets?

No. It stops writes, not reads. The agent can read any file your user can read. For that, run the agent as a separate macOS user. Add -n to stop plain commands from sending data out; Claude Code and Codex need the network to reach their model.

Does it slow the agent down?

No measurable cost. Install, first run and a caged task take under a second. The kernel checks each write as it happens.

Does it work on Linux?

Not this script, because it uses macOS sandbox-exec. On Linux, bubblewrap or Landlock do the same job: mount the filesystem read-only and one folder writable.

Open source by kilazi, Apache-2.0. Repo: https://github.com/kilazi/agent-cage. Short version: the README.

Sources

  1. Claude Code docs: settings and permissions
  2. Codex docs: sandbox and approvals
  3. Apache License 2.0