跳到正文
DEV Community· Sandscale·· 1 小時前AI 評分61

如何在 Sandscale 雲端工作區以非互動模式執行 Claude Code

A sandbox for Claude Code: running it headless in a cloud workspace

AI 導讀

Sandscale 的指南示範在雲端工作區以 `claude -p` 非互動執行 Claude Code,讓命令及編輯操作都在工作區內進行。工作區採用具獨立核心、磁碟可跨工作階段保留的 Firecracker microVM;使用 `--output-format json` 可取得結果和 session ID,再以 `--resume` 繼續同一工作階段。

正文

Short answer: if you want Claude Code's commands off your machine, run Claude Code itself on a cloud computer and drive it with claude -p. A Sandscale workspace is a Firecracker microVM with its own kernel and a disk that survives between sessions, so the repository, the installed tools and Claude Code's own session files are still there next time, and nothing it runs touches your laptop.

This guide uses Sandscale's workspaces guide and workspace reference for the Sandscale side, and Anthropic's Run Claude Code programmatically page for the Claude Code side, read in November 2026.

Workspace, sandbox or MCP?

There are three ways to give Claude Code a separate place to run code, and they solve different problems:

  • Claude Code runs locally, commands run in a sandbox. Connect the Sandscale MCP server and let Claude Code start sandboxes and run commands in them. Your files stay local; only what the agent sends goes to the sandbox.
  • Claude Code runs in a workspace. Everything, including the agent's edits, happens on the workspace. This is the setup in this post, and it suits long tasks that should keep going when your laptop is closed.
  • Agent Cloud. Sandscale hands the task to an agent in one of your workspaces and returns a branch and pull request. See Agent Cloud.

1. Create and open a workspace

Install the CLI on macOS or Linux, sign in, then pick a template your organization set up:

curl -fsSL https://sandscale.dev/install | sh
sandscale login
sandscale workspace templates list
sandscale workspace create claude-box --template TEMPLATE
sandscale workspace open claude-box

TEMPLATE is one of the names templates list prints. open starts the workspace if it is stopped, waits until it runs, and connects over SSH with a short-lived certificate and a checked host key. Claude Code needs to reach Anthropic's API, so the workspace needs network access; network follows your plan and your organization's policy, so check that yours lets the workspace reach that API.

2. Install and sign in to Claude Code on the workspace

Inside the workspace shell, install Claude Code the way Anthropic's setup docs describe, clone your repository, and sign in once. Because the workspace disk persists, the sign-in and the clone are still there after the workspace stops and starts again.

If you run Claude Code with --bare, it does not read OAuth credentials or the keychain, and Anthropic's docs say to set ANTHROPIC_API_KEY in the environment instead. Export it in the shell session rather than writing it into a file inside the repository.

3. Run it headless

-p (or --print) runs Claude Code non-interactively. --allowedTools pre-approves the tools the run may use without a prompt:

cd ~/src/my-service
claude -p "Run the test suite, fix the first failing test, and summarize the change" \
  --allowedTools "Read,Edit,Bash" \
  --output-format json > result.json

Per Anthropic's docs, Claude Code exits 0 on success and non-zero when the run fails, and with --output-format json the text is in the result field and the session ID in session_id. To continue the same session later:

session_id=$(jq -r '.session_id' result.json)
claude -p "Now run the linter and fix what it reports" --resume "$session_id"

Two notes from the same page. Without --bare, a -p session runs the hooks in a project's .claude/settings.json and connects the servers in its .mcp.json with no trust prompt, so only point it at repositories you would open yourself. With --bare, it skips that auto-discovery but still reads settings files and their permission rules.

4. Keep it running after you disconnect

Run the command inside a terminal multiplexer such as tmux on the workspace, detach, and close your laptop. The workspace keeps running until it is stopped, goes idle, or hits a stop schedule:

sandscale workspace schedule idle claude-box 30m
sandscale workspace schedule stop claude-box 8h

Your organization's policy can shorten what you set, and sandscale workspace schedule show claude-box prints the values in force. Hours count against your plan only while the workspace runs.

5. Look at the result from your machine

Reconnect with sandscale workspace open claude-box, or with sandscale workspace ssh from an editor that connects over SSH. If the agent started a dev server, reach it locally without opening anything to the internet:

sandscale workspace port-forward claude-box 3000

When you are done for the day, sandscale workspace stop claude-box stops it and keeps the disk.

What Sandscale adds around it

The workspace is a separate microVM, so a bad command hits the workspace's disk, not yours. Starting, stopping and connecting are in your organization's audit log, policies decide which templates and sizes members may use, and secrets can come from Vault instead of a prompt. Claude Code's own permission rules still decide which tools the model may call; the workspace decides where those calls land.

FAQ

Can I sandbox Claude Code without Docker?

Yes. Running Claude Code inside a Sandscale workspace puts it in a Firecracker microVM with its own kernel, so its Bash tool runs there instead of on your machine. You do not need Docker on your laptop for this.

How do I run Claude Code non-interactively?

Pass -p with a prompt, for example claude -p "fix the failing test" --allowedTools "Read,Edit,Bash". Add --output-format json to get the result and session ID as JSON, and --resume with that session ID to continue.

Does the workspace keep Claude Code's session when it stops?

Yes. A workspace keeps its disk when it stops, so the repository, installed tools and Claude Code's session files are still there when you open it again. Deleting the workspace deletes its disks.

Does the workspace need network access?

Yes, Claude Code calls Anthropic's API. Network access follows your plan and your organization's policy, so check that yours lets the workspace reach that API.

Related

Originally published on the Sandscale blog.

來源:DEV Community · dev.to