How to run Claude Code in an isolated VM that can only open pull requests
Confirmed by source: A practitioner describes the setup they run (restricted VM, secrets manager, PR-only Git user, manual merge then CI deploy). The sandbox part matches Anthropic's official Claude Code dev container docs (code.claude.com/docs/en/devcontainer) and the reference container with an egress firewall in the anthropics/claude-code repository, both checked live on 2026-10-21. (checked 2026-10-21)
The problem
A coding agent running on your own laptop can read everything you can: SSH keys, cloud credentials, browser data, personal files. If a prompt injection or a bad tool call takes over, it can leak secrets or push straight to production. You want the agent to work unattended without giving it that reach.
Tools needed
- Claude Code (running unattended inside the sandbox)
- A dedicated VM or dev container on its own restricted network segment
- Infisical (or another secrets manager) injecting secrets at run time
- Self-hosted Forgejo (or any Git host) with a bot account limited to issues and pull requests
- CI pipeline that deploys only after a human merges
Step-by-step recipe
- Give the agent its own machine: a VM (or a dev container) used only for agent work. Put it on a separate network segment whose outbound traffic is limited to the internet hosts it needs, your Git server and your secrets manager, and allow SSH only one way, from your machine into the VM.
- Never copy secrets into the VM or mount host credential folders. Store them in a secrets manager and have commands receive them at run time (for example by wrapping the command with the manager's CLI), so the values do not end up in files, prompts or the agent's transcript.
- Create a Git bot account for the agent that can open issues and pull requests on the repositories it works on but cannot push to the main branch, merge, or change settings. Protect the main branch on the server.
- Run Claude Code inside the VM as a non-root user. Only there, where the blast radius is the sandbox, consider unattended mode; Anthropic's dev container docs show the same pattern with an egress firewall.
- Review every pull request yourself and merge by hand. Let your CI pipeline build and deploy only from the merged main branch, so nothing the agent writes reaches production without your click.
- Keep a separate local 'dev mode' for when you must run the app with real secrets, and assume anything visible in that mode can be seen by the agent.
Copyable prompt
You are working inside an isolated sandbox on [REPO]. You have no direct access to production or to secret values. Task: [TASK DESCRIPTION]. Rules: - Work on a new branch named [agent/SHORT-NAME]; never push to [main]. - When a command needs credentials, run it through [SECRETS CLI WRAPPER, e.g. 'infisical run -- <command>']; never print, write to files or echo secret values. - Run [TEST COMMAND] before you finish and include the result. - When done, open a pull request on [GIT HOST] with a summary of the change, the test output and anything you were unsure about. Do not merge it. - If the task needs anything outside this sandbox, open an issue describing what you need instead of working around the restriction.
Original template written by our AI agent for AgentRecipes, not copied from the source. Replace the [BRACKETS] and {{variables}} with your own values and test before relying on it.
What the builder reported
The author describes this as their working day-to-day setup: Claude Code runs in its own VM on a restricted network, secrets come from Infisical so the agent never sees the actual values outside a local dev mode, the agent's Forgejo user can only create issues and pull requests, and after a manual merge a CI pipeline packages and deploys the app to a separate app-server VM. No metrics are reported.
These are the source author's own claims. AgentRecipes has not run this recipe itself.
Watch out for
- The sandbox does not stop the agent from misusing what it can reach. Anything inside the VM, including the Claude Code login, can still be leaked, so keep the VM empty of personal data and use short-lived, repository-scoped tokens.
- Anthropic's own docs warn that unattended mode in a container does not stop a malicious project from exfiltrating whatever the container can access; the network allowlist is what limits where data can go.
- Manual merging is the real safety gate. If you start approving pull requests without reading them, the setup loses most of its value.
- The source author calls the setup overkill for most people and notes that some employers expect the agent harness on the work laptop; in that case the author isolates the laptop on its own network segment instead.
Source
Original write-up: Hacker News (2026-10-20) ↗
Found via Agent Use Cases: “Run Claude Code in an isolated VM that opens PRs”. This recipe summarises the source in words written by our AI agent; read the original for the full details. Link last checked 2026-10-21.
More engineering & security recipes
New agent recipes in your inbox
A short email with the newest automation recipes, each linked to its real-world source. No spam. Every email has a one-click unsubscribe link.