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. 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, the agent's own Firecracker microVM, using the Sandscale CLI (1.4.1). The hook fields come from Claude Code's hooks reference, read on 1 December 2026.
What a PreToolUse hook can do
From the hooks reference:
- A hook entry has a
matcher(hereBash) and a command to run. The command gets the tool call as JSON on standard input, sojq -r '.tool_input.command'gives you the shell command Claude wants to run. - The hook answers with JSON under
hookSpecificOutput.permissionDecisioncan beallow,deny,askordefer.updatedInputreplaces the tool's input before it runs. - Combine
updatedInputwithaskand Claude Code shows you the changed command in its permission prompt. Combine it withallowand it runs without asking. -
denyblocks the call, and the reason is shown to Claude, so it can try something else. Deny and ask rules in your permission settings are still evaluated whatever the hook returns.
That is enough to route a command: read it, decide whether it should run elsewhere, and hand back a different command.
The sandbox
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.
The hook
Save this as .claude/hooks/route-to-sandbox.sh and make it executable:
#!/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.
Ask first, then allow
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.
What the hook does not cover
- Commands you did not list. The pattern is an allowlist of what to move, so a command it does not match runs locally. Start with installs and test suites, and add to the pattern when you see something else that should move.
- Edits. Claude Code's file edits still happen in your working copy. The upload sends the current state each time, so the sandbox sees the latest edits, but the edits themselves are local.
- Monitor commands. The hooks reference says a Bash-matcher hook also runs on shell commands started with the Monitor tool, but
allowandupdatedInputhave no effect on them. - Everything else on the laptop. A hook narrows what runs locally. It does not make the laptop a separate machine.
The next step: put the agent itself on its own machine
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 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.
To set up the CLI and connect Claude Code, start with the Quickstart.
Originally published on the Sandscale blog.

