cd /news/ai-agents/claude-code-hooks-that-send-the-agen… · home › topics › ai-agents › article
[ARTICLE · art-179394] src=dev.to ↗ pub= topic=ai-agents verified=true sentiment=↑ positive

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.

recommendation

Route risky Bash commands like npm install and pytest through a PreToolUse hook that rewrites them to run in an isolated sandbox, and rely on upload filtering to keep .env files and credentials off the agent's machine.

more in playbook: Securing agents and AI tools
by read4 min views2 publishedDec 1, 2026

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.

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:

#!/bin/bash
CMD=$(jq -r '.tool_input.command')

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

── more in #ai-agents 4 stories · sorted by recency
── more on @claude code 3 stories trending now
sponsored brought to you by zahid.host 4,200+ EU-deployed projects
reading about agents? ship yours in a single git push.

Run your AI side-project on zahid.host

EU-based hosting, git-push deploys, automatic HTTPS, no cold starts. Free tier with a custom domain — perfect for shipping the agent you just read about.

$git push zahid main
→ Live at https://your-agent.zahid.host ✓
Get free account → Pricing
from €0/mo · no card required
LIVE [news/claude-code-hooks-th…] indexed:0 read:4min 2026-12-01 · —