AgentRecipes
Recipes › Engineering & security

How to run Claude Code in an isolated VM that can only open pull requests

Engineering & securityClaude Code✓ Confirmed by source

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

Step-by-step recipe

  1. 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.
  2. 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.
  3. 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.
  4. 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.
  5. 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.
  6. 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

Prompt template
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

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

Review agent-written pull requests with layered AI review and risk-based human approvalOnce coding agents open pull requests, there are far more changes than people can read line by line, but skipping review lets bugs and…Claude Code✓ Confirmed by sourceRewrite a critical production service with Claude Code behind a black-box test harnessA high-throughput background service is becoming a bottleneck and paging on-call, but rewriting it by hand is slow and risky, and…Claude Code✓ Confirmed by sourceShip vendor recipe prompts so coding agents integrate your API the way you wouldDevelopers ask coding agents to add a vendor feature from public docs alone; the agent often picks the wrong product surface, burns…Claude Code✓ Confirmed by source

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.