Skip to content
Sandboxing Claude Code

Sandboxing Claude Code

With --dangerously-skip-permissions, Claude Code works through a task on its own, without asking before each command. That is best done somewhere it can do no harm. This page builds claude-sandbox, a short Go program you run on your laptop, in a Git repository, with a task. It gives the task a machine of its own on a Dicer host, and brings Claude’s changes back to your working tree.

How it works

For each task, claude-sandbox:

  1. forks a sandbox from a snapshot of a machine with Claude Code installed, in about a second;
  2. copies in the files of your last commit;
  3. runs Claude on the task
  4. applies Claude’s changes to your working tree, neither staged nor committed;
  5. deletes the sandbox

Claude can reach nothing of yours but that copy. The sandbox gets no Git history, so none of your remotes, and no ignored or untracked files, such as .env.

Your key for Claude reaches only the command that runs it, and is never written to the snapshot.

Set it up

On the Dicer host

Make the snapshot. Start from an image with your project’s toolchain, here Go, add a user for Claude, and install Claude Code as that user:

$ dicer run -d --name claude-base --vcpus 2 --memory 2GiB golang:1.25 sleep infinity
$ dicer exec claude-base useradd -m claude
$ dicer exec -u claude claude-base sh -c 'curl -fsSL https://claude.ai/install.sh | bash'
$ dicer snapshot create claude-base claude-base
Snapshot claude-base of instance claude-base created in 2.4s (memory, 2.7 GiB)
$ dicer rm -f claude-base

Claude Code refuses to skip permissions as root, which is why it gets a user of its own. Every sandbox gets the base’s 2 vCPUs and 2 GiB of memory.

Then make a token for your laptop:

$ dicer token create claude-sandbox --scopes instances:write,events:read

instances:write lets the token fork the snapshot, work in the fork and delete it, and events:read lets you look back at what happened. The command prints the token, once. The host must serve the API over TCP; see Remote access.

On your laptop

Build the program with go build. Then give it, and dicer, the host’s address and the token, and give Claude a key:

$ export DICER_REMOTE=dicer1.example.com:7443
$ export DICER_TOKEN=dicer_…
$ export CLAUDE_CODE_OAUTH_TOKEN=sk-ant-oat01-…

dicer also takes the name of a remote in DICER_REMOTE, but claude-sandbox needs the address itself, which it passes to the Go client.

CLAUDE_CODE_OAUTH_TOKEN takes a token for a Claude subscription, which claude setup-token makes. Set ANTHROPIC_API_KEY instead to use an API key from the Claude Console.

Run a task

The tests of this small Go module fail:

$ go test ./slug
--- FAIL: TestMake (0.00s)
    --- FAIL: TestMake/__Espresso_Cups__ (0.00s)
        slug_test.go:18: Make("  Espresso Cups  ") = "--espresso-cups--", want "espresso-cups"
    --- FAIL: TestMake/Milk__Frother (0.00s)
        slug_test.go:18: Make("Milk  Frother") = "milk--frother", want "milk-frother"
    --- FAIL: TestMake/Crème_Brûlée_Torch (0.00s)
        slug_test.go:18: Make("Crème Brûlée Torch") = "crme-brle-torch", want "creme-brulee-torch"
FAIL
FAIL	example.com/shop/slug	0.319s
FAIL

Give Claude the task:

$ claude-sandbox "make the failing tests in ./slug pass"
Claude is working in claude-base-49c6
The failing tests in `./slug` now pass. I ran `go test ./...` and it reports `ok`.

The three failures had two causes, and I fixed both in `slug/slug.go`:
- **Extra hyphens:** `Make` turned every space into a hyphen. That left leading and trailing hyphens on `"  Espresso Cups  "` and a double hyphen in `"Milk  Frother"`. Now any run of non-alphanumeric characters becomes a single hyphen, and none is written at the start or end.
- **Accents:** `"Crème Brûlée"` lost its accented letters entirely. I added a small table that folds common accented Latin letters to ASCII, such as `è` to `e` and `ß` to `ss`. I used a table rather than an external package, so letters outside it, like Cyrillic or CJK, are still dropped.

Punctuation now acts as a word separator, so `"Moka Pot (6 cups)"` still gives `moka-pot-6-cups`.

Forty-one seconds later, the fix is in the working tree, ready to review with git diff and to keep or discard as you would your own:

$ git status --short
 M slug/slug.go
$ go test ./slug
ok  	example.com/shop/slug	0.166s

Watch a run

dicer reads the same two variables, so it sees the sandbox on the host while Claude works:

$ dicer ps --wide
NAME              IMAGE                          STATE    STATUS         VCPU  MEMORY  DISK    NETWORK  IP            PORTS  CREATED
claude-base-49c6  docker.io/library/golang:1.25  Running  Up 20 seconds  2     2 GiB   10 GiB  default  172.20.40.72  -      22 seconds ago

Inside it, Claude runs as claude, and is building the tests:

$ dicer top claude-base-49c6
PID   PPID  USER    STATE  STARTED         CPUTIME  RSS        COMMAND
1     0     root    S      38 seconds ago  410ms    20 MiB     /init
642   1     root    S      38 seconds ago  90ms     15.4 MiB   /usr/local/bin/dicer-agent
648   1     root    S      38 seconds ago  0s       1.3 MiB    sleep infinity
723   642   claude  S      21 seconds ago  3.61s    235.8 MiB  /home/claude/.local/bin/claude -p make the failing tests in ./slug pass --dangerously-skip-permissions
…
783   781   claude  S      14 seconds ago  2.22s    21.2 MiB   go test ./...
1368  783   claude  R      1 second ago    340ms    57.4 MiB   /usr/local/go/pkg/tool/linux_amd64/compile -o /tmp/go-build4188076265/b061/_pkg_.a …

Once Claude is done, the sandbox is gone.

The program

main.go
// Command claude-sandbox runs Claude Code on a task in a sandbox on a Dicer
// host, and applies Claude's changes to the Git working tree it is run in.
//
//	claude-sandbox "make the failing tests in ./slug pass"
//
// The sandbox is a fork of the claude-base snapshot, given the files of the
// repository's last commit, and deleted when Claude is done. $DICER_REMOTE,
// the host's address rather than a remote's name, and $DICER_TOKEN say which
// host. Unset, they mean the daemon on this machine. Claude authenticates
// with $ANTHROPIC_API_KEY or $CLAUDE_CODE_OAUTH_TOKEN.
package main

import (
	"bytes"
	"context"
	"fmt"
	"log"
	"os"
	"os/exec"
	"os/signal"
	"strings"

	"github.com/konradasb/dicer"
)

func main() {
	log.SetFlags(0)

	ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt)
	defer stop()

	if err := run(ctx, strings.Join(os.Args[1:], " ")); err != nil {
		log.Fatal(err)
	}
}

func run(ctx context.Context, task string) error {
	client, err := dicer.NewClient(
		dicer.WithAddress(os.Getenv("DICER_REMOTE")),
		dicer.WithToken(os.Getenv("DICER_TOKEN")),
	)
	if err != nil {
		return err
	}
	defer client.Close()

	sandbox, err := client.Snapshots.Fork(ctx, "claude-base", dicer.ForkOptions{})
	if err != nil {
		return err
	}
	defer client.Instances.Delete(context.WithoutCancel(ctx), sandbox.Name, dicer.DeleteOptions{Force: true})
	log.Printf("Claude is working in %s", sandbox.Name)

	// The sandbox gets the last commit's files, in a repository of its own
	// whose one commit is tagged base.
	files, err := exec.Command("git", "archive", "--prefix=work/", "HEAD^{tree}").Output()
	if err != nil {
		return fmt.Errorf("git archive: %w", err)
	}
	if err := client.Instances.CopyArchiveTo(ctx, sandbox.Name, "/home/claude", bytes.NewReader(files)); err != nil {
		return err
	}
	setup := client.Instances.Command(sandbox.Name, "sh", "-c",
		"git init -q && git add -A && git -c user.name=sandbox -c user.email=sandbox@localhost commit -qm base && "+
			"git tag base && chown -R claude: .")
	setup.Dir = "/home/claude/work"
	if _, err := setup.Output(ctx); err != nil {
		return err
	}

	// Claude Code skips permissions only for a user other than root.
	claude := client.Instances.Command(sandbox.Name,
		"/home/claude/.local/bin/claude", "-p", task, "--dangerously-skip-permissions")
	claude.User = "claude"
	claude.Dir = "/home/claude/work"
	claude.Env = map[string]string{}
	for _, key := range []string{"ANTHROPIC_API_KEY", "CLAUDE_CODE_OAUTH_TOKEN"} {
		if value := os.Getenv(key); value != "" {
			claude.Env[key] = value
		}
	}
	claude.Stdout, claude.Stderr = os.Stdout, os.Stderr
	if err := claude.Run(ctx); err != nil {
		return err
	}

	// Claude's changes are what differs from base, its commits included.
	diff := client.Instances.Command(sandbox.Name, "sh", "-c", "git add -A && git diff --cached --binary base")
	diff.User = "claude"
	diff.Dir = "/home/claude/work"
	patch, err := diff.Output(ctx)
	if err != nil {
		return err
	}
	apply := exec.Command("git", "apply", "--allow-empty")
	apply.Stdin = bytes.NewReader(patch)
	apply.Stderr = os.Stderr

	return apply.Run()
}

The sandbox gets the files of your last commit, so commit or stash what Claude should see first. Its copy is a repository of its own, whose one commit is tagged base, so Claude’s changes are what differs from base, including anything it commits.

To make it your own:

  • Another language. Start the base from an image with its toolchain, such as node:22 or python:3.13.
  • More direction. Add Claude Code’s own flags to its command, such as --model or --max-turns.
  • More at once. Each run has a sandbox of its own, so several can run side by side, as many as the host has room for. See Capacity.