Claude Code hooks that send the agent's Bash commands to its own machine, not your laptop A developer published a Claude Code PreToolUse hook that intercepts Bash commands matching installs and test runs (npm, pnpm, yarn, pip, pytest) and rewrites them via updatedInput to execute on a Sandscale Firecracker microVM sandbox instead of the local laptop. The hook uploads the working directory to the sandbox, which excludes .gitignore'd files and platform deny-listed secrets such as .env files, private keys and cloud credentials, and sets permissionDecision to "ask" so the user confirms each rerouted command. The author argues instruction files like CLAUDE.md are context rather than enforced configuration, while hooks can deny or modify tool calls before they run. Claude Code runs its Bash tool on the machine it is running on. For most commands that is fine. The ones that make teams nervous are a small, predictable set: npm install in a repository someone just cloned, pip install -r requirements.txt from a pull request, a test suite whose setup scripts nobody has read. On a laptop, those run as you, next to your SSH keys and cloud credentials. You can write a rule in CLAUDE.md or AGENTS.md that says "run installs and tests on a sandbox". We covered that approach for Cursor in a project rule that sends installs and tests to a sandbox https://sandscale.dev/blog/cursor-rule-run-tests-in-a-sandbox?ref=devto-claude-code-hooks-route-bash-to-a-sandbox . The weakness of a rule is that it is a request. Claude Code's own documentation says instruction files are context, not enforced configuration. Hooks are different. A PreToolUse hook is a command Claude Code runs before a tool call, and it can deny the call or change its input. This post shows a hook that catches the Bash commands you choose and rewrites them to run on a Sandscale sandbox https://sandscale.dev/sandboxes?ref=devto-claude-code-hooks-route-bash-to-a-sandbox , the agent's own Firecracker microVM, using the Sandscale CLI 1.4.1 . The hook fields come from Claude Code's hooks reference https://code.claude.com/docs/en/hooks , read on 1 December 2026. From the hooks reference: matcher here Bash and a command to run. The command gets the tool call as JSON on standard input, so jq -r '.tool input.command' gives you the shell command Claude wants to run. hookSpecificOutput . permissionDecision can be allow , deny , ask or defer . updatedInput replaces the tool's input before it runs. That is enough to route a command: read it, decide whether it should run elsewhere, and hand back a different command. Create one sandbox for the session and keep its id in the environment Claude Code starts from: sandscale sandbox create --template-id builtin:node export SANDSCALE SANDBOX ID=SANDBOX ID claude create answers with the sandbox id. The CLI's help says network is off by default --network none , and that allowlist reaches only your organization's egress allowlist and package registry presets, where your organization's policy and plan allow it. An install needs that; a test suite that does not fetch anything does not. Save this as .claude/hooks/route-to-sandbox.sh and make it executable: bash /bin/bash Rewrites installs and test runs so they run on the agent's sandbox. CMD=$ jq -r '.tool input.command' Already going to Sandscale, or no sandbox for this session: leave it alone. case "$CMD" in sandscale\ exit 0 ;; esac -z "$SANDSCALE SANDBOX ID" && exit 0 if echo "$CMD" | grep -Eq '^ npm install|ci|test|run test |pnpm install|test |yarn install|test |pip install|pytest |$ '; then NEW="sandscale sandbox instances-project --id $SANDSCALE SANDBOX ID --dir /workspace/project --body @. --yes && sandscale sandbox run $SANDSCALE SANDBOX ID --workdir /workspace/project --yes $ printf '%q' "$CMD" " jq -n --arg cmd "$NEW" '{ hookSpecificOutput: { hookEventName: "PreToolUse", permissionDecision: "ask", permissionDecisionReason: "Routed to the agent sandbox", updatedInput: { command: $cmd } } }' fi exit 0 The rewritten command does two things. sandbox instances-project uploads the current folder into the sandbox --body @. sends the folder as an archive , and sandbox run runs the original command there, in that folder, and prints its exit code, output and errors. The help lists what the upload leaves out on the server side: files your .gitignore ignores, and files on the platform deny-list, including .env files, private keys, cloud credentials and .git . So the secrets in your working copy do not travel with it. Then register the hook in .claude/settings.json : { "hooks": { "PreToolUse": { "matcher": "Bash", "hooks": { "type": "command", "command": "${CLAUDE PROJECT DIR}/.claude/hooks/route-to-sandbox.sh" } } } } Commit both files, and everyone on the team gets the same routing. The hook returns ask , so each routed command shows up in the permission prompt in its rewritten form. That is deliberate for the first days: you see exactly what is uploaded and run. When the team trusts the pattern, change ask to allow and routed commands run without a prompt. The hooks reference notes that permission rules are evaluated against the input the hook returns, not the input Claude sent, so write any allow rules for sandscale sandbox commands, not for npm test . If you would rather refuse than rewrite, return deny with a reason such as "Run installs with sandscale sandbox run on the session sandbox". Claude reads the reason and can issue the command itself. The hook moves the riskiest commands. The simpler arrangement, once a team runs several agents, is to run Claude Code itself on its own machine: a workspace https://sandscale.dev/workspaces?ref=devto-claude-code-hooks-route-bash-to-a-sandbox per agent, where every command, edit and install happens away from anyone's laptop. sandscale workspace create --name agent-1 --template-id TEMPLATE ID sandscale workspace ssh agent-1 The same hook still works there if you want a second boundary for untrusted installs. Either way, every sandbox and workspace start, every command run and every file upload lands in your organization's audit log https://sandscale.dev/docs/concepts/audit?ref=devto-claude-code-hooks-route-bash-to-a-sandbox . To set up the CLI and connect Claude Code, start with the Quickstart https://sandscale.dev/docs/quickstart?ref=devto-claude-code-hooks-route-bash-to-a-sandbox . Originally published on the Sandscale blog https://sandscale.dev/blog/claude-code-hooks-route-bash-to-a-sandbox?ref=devto-claude-code-hooks-route-bash-to-a-sandbox .