What you’ll build
You will start a sandbox from theclaude-code template — which ships with Anthropic’s Claude Code CLI pre-installed — and run Claude Code in headless mode on a coding task. Claude Code reads and edits code, runs commands, and executes what it writes inside the sandbox, so none of its actions touch your machine. The model can be Anthropic’s or any Novita-hosted model behind Novita’s Anthropic-compatible API.
Prerequisites
- Novita API key — create one in Key Management and export it as
NOVITA_API_KEY. - A model key — an Anthropic key exported as
ANTHROPIC_API_KEYfor the Quickstart, or nothing extra if you use Novita models. - Novita Sandbox SDK — Python 3.10+ or Node.js 20+:
- Claude Code version — the
claude-codetemplate comes with the Claude Code CLI pre-installed; nothing to install. Runclaude --versionin a sandbox to see the bundled version.
Quickstart
Create a sandbox from theclaude-code template, run one headless task with claude -p, stream its output, and kill the sandbox.
-p "<prompt>" (--print) runs Claude Code in headless mode: it processes the prompt, prints the result, and exits, which makes it scriptable. --dangerously-skip-permissions skips the approval prompts Claude Code normally shows before editing files or running commands — as the name warns, use it only in an isolated sandbox like this one.How it works
Claude Code gets a full Linux environment inside the sandbox. You drive it with the SDK:files and git to move code in and out, commands to run Claude Code and anything else. The snippets below assume a sandbox created as in the Quickstart.
Execute generated code
Claude Code writes code and runs it in the sandbox’s shell. Its output streams back throughon_stdout / on_stderr, and the call returns the exit code.
Read and write files on a cloned repository
The most common workflow: clone a repository withsandbox.git.clone (with credentials for private repos), let Claude Code change it, then read the result back — here, the diff.
sandbox.files.write and sandbox.files.read; see Read and write files.
Run commands yourself
Run any command next to the agent — install dependencies before it starts, or verify its work afterwards without trusting its own report.Resume a session
Claude Code can continue a previous session, so you can run a multi-step workflow across several calls. Run with--output-format json to get a session_id, then pass it to --resume.
Use Novita models
Novita provides an Anthropic-compatible API, so Claude Code can run on Novita-hosted models. Replace theenvs in the Quickstart with the values below — no other change is needed. Change ANTHROPIC_MODEL to switch to any supported model. See Claude Code with Novita for details.
env block of /home/user/.claude/settings.json before running claude. Model usage is billed separately from sandbox runtime, at the model’s own per-token rate.
Production tips
- Timeouts —
timeout(seconds;timeoutMsin JavaScript) onsandbox.createis the sandbox’s maximum lifetime; extend it while Claude Code works withset_timeout. Passtimeout=0tocommands.runso a long Claude Code turn isn’t cut off at the SDK’s default command timeout. Without long-running mode the timeout is capped by your tier (1, 3, or 4 hours); for longer runs, see Long-running sandboxes. - Pause and resume — between steps of a multi-step job, pause the sandbox instead of killing it. The repo, installed packages and Claude Code sessions survive, so
--resumestill works after you reconnect, and CPU and RAM billing stops while it is paused. - Concurrency — each sandbox is isolated, so you can run one Claude Code task per sandbox in parallel — for example, one per repository or per issue. Concurrent sandboxes are capped by your account tier (5 on Free, 100 on Paid); see Quota Limits.
- Cost — sandboxes are billed per second while running. A 2 vCPU / 2 GiB sandbox costs about $0.0000260/s, so a 10-minute task is roughly $0.016 in sandbox time, plus model tokens. See Pricing.
- Keep secrets out of the sandbox — pass header-based API keys, such as your model key, as Secrets so code in the sandbox only sees a placeholder. For Git, use a short-lived token scoped to the one repository.
Troubleshooting
402: insufficient balance when creating a sandbox
402: insufficient balance when creating a sandbox
Claude Code asks you to log in, or returns 401
Claude Code asks you to log in, or returns 401
Claude Code found no usable credentials. With Anthropic, pass
ANTHROPIC_API_KEY in envs. With Novita, pass ANTHROPIC_AUTH_TOKEN (your Novita key) together with ANTHROPIC_BASE_URL, and don’t set ANTHROPIC_API_KEY at the same time.The command hangs or produces no output
The command hangs or produces no output
Without
-p, Claude Code opens its interactive UI and waits for input that never comes. Always pass -p "<prompt>", and --dangerously-skip-permissions so tool approvals don’t block the run.Next steps
Sandbox SDK reference
Every option for creating and managing sandboxes.
Codex Sandbox
Run OpenAI’s coding agent the same way.
Sandbox cookbook
Runnable Novita Sandbox examples on GitHub.
OpenCode and OpenClaw
Run open-source agents in a sandbox.