# Claude Code hooks that send the agent's Bash commands to its own machine, not your laptop

> A project rule asks Claude Code to run installs and test suites somewhere else. A PreToolUse hook makes it happen: it catches the Bash calls you choose and rewrites them to run on a Sandscale sandbox, with the change shown to you before it runs. The hook, the settings, and what it does not cover.

A project rule asks Claude Code to run installs and test suites somewhere else. A PreToolUse hook makes it happen: it catches the Bash calls you choose and rewrites them to run on a Sandscale sandbox, with the change shown to you before it runs. The hook, the settings, and what it does not cover.

December 1, 2026 · 5 min read · The Sandscale team

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). 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), 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.

## What a PreToolUse hook can do

From the hooks reference:

- A hook entry has a `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.
- The hook answers with JSON under `hookSpecificOutput`. `permissionDecision` can be `allow`, `deny`, `ask` or `defer`. `updatedInput` replaces the tool’s input before it runs.
- Combine `updatedInput` with `ask` and Claude Code shows you the changed command in its permission prompt. Combine it with `allow` and it runs without asking.
- `deny` blocks 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:

```sh
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:

```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`:

```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 `allow` and `updatedInput` have 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](https://sandscale.dev/workspaces) per agent, where every command, edit and install happens away from anyone’s laptop.

```sh
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).

To set up the CLI and connect Claude Code, start with the [Quickstart](https://sandscale.dev/docs/quickstart).

**On this page**

1. What a PreToolUse hook can do
2. The sandbox
3. The hook
4. Ask first, then allow
5. What the hook does not cover
6. The next step: put the agent itself on its own machine

**Run your agents in a box of their own**

Each sandbox is a Firecracker microVM. Free includes 100 awake vCPU-hours a month.

[Start free](https://cloud.sandscale.dev/auth/signup)

## Keep reading

- [December 1, 2026 · **A team workflow for coding agents: who starts them, who approves, who reviews, who stops them** · When a team runs Claude Code, Codex or Cursor agents every day, someone has to own each step. A simple division of roles, a naming convention, and the Sandscale commands and controls behind each step, with no code to write.](https://sandscale.dev/blog/team-workflow-for-coding-agents)
- [December 1, 2026 · **Cloud workspaces built for one developer vs a team running many coding agents: what to look for** · Most cloud development environments were designed around one person and their editor. A team running many Claude Code, Codex or Cursor agents asks different questions: who owns each machine, what runs while nobody watches, which credentials it holds, who approves what, and how it all gets stopped. A checklist.](https://sandscale.dev/blog/cloud-workspaces-for-one-developer-vs-a-team-of-agents)

---
Source: https://sandscale.dev/blog/claude-code-hooks-route-bash-to-a-sandbox
