# Gemini CLI in a sandbox: headless runs in a cloud workspace

> Run Gemini CLI headless, with JSON output, inside a Sandscale workspace, a persistent Firecracker microVM. Approval modes, exit codes and how to keep its shell commands off your laptop.

Run Gemini CLI headless, with JSON output, inside a Sandscale workspace, a persistent Firecracker microVM. Approval modes, exit codes and how to keep its shell commands off your laptop.

November 27, 2026 · 4 min read · The Sandscale team

**Short answer:** Gemini CLI has a `--sandbox` flag for local isolation, but if you want its shell commands on a different machine entirely, run Gemini CLI on a cloud computer and call it with `-p`. A Sandscale [workspace](https://sandscale.dev/workspaces) is a Firecracker microVM with its own kernel and a persistent disk, and Sandscale’s workspace docs name Gemini CLI as one of the agents you can run inside one.

Sources, read in November 2026: Gemini CLI’s [headless mode reference](https://geminicli.com/docs/cli/headless/) and [CLI reference](https://geminicli.com/docs/cli/cli-reference/), and Sandscale’s [workspaces concept page](https://sandscale.dev/docs/concepts/workspaces) and [workspaces guide](https://sandscale.dev/docs/guides/workspaces).

## 1. Create a workspace

```sh
sandscale login
sandscale workspace templates list
sandscale workspace create gemini-box --template TEMPLATE
sandscale workspace open gemini-box
```

`TEMPLATE` is a name `templates list` prints. Gemini CLI needs to reach Google’s API. Network access follows your plan and your organization’s policy, so check that yours lets the workspace reach it.

Inside the workspace, install Gemini CLI with npm, authenticate it the way its docs describe, and clone your repository:

```sh
npm install -g @google/gemini-cli
```

## 2. Run it headless

Per the headless reference, Gemini CLI runs non-interactively when it is not attached to a terminal or when you pass `-p` (or `--prompt`):

```sh
cd ~/src/my-service
gemini -p "List the modules without tests and suggest one test for each" --output-format json > out.json
```

With `--output-format json` the result is one JSON object with `response` (the final answer), `stats` (token use and latency) and, on failure, `error`. `--output-format stream-json` gives newline-delimited events instead: `init`, `message`, `tool_use`, `tool_result`, `error` and `result`.

## 3. Pick an approval mode on purpose

`--approval-mode` sets how tool calls are approved. Per the CLI reference the choices are `default`, `auto_edit`, `yolo` and `plan`; the older `--yolo` flag is deprecated in favour of `--approval-mode=yolo`.

On your laptop, `yolo` means the model’s shell commands run on your machine without asking. On a workspace they run in the workspace’s microVM, against its disk, which is a more reasonable place for an unattended run. A reasonable default for scripted runs is `auto_edit` for file edits only, and `yolo` only on a workspace you are prepared to throw away or restore from git.

## 4. Branch on the exit code

The headless reference lists exit codes: `0` success, `1` general error or API failure, `42` input error (invalid prompt or arguments) and `53` turn limit exceeded. From your laptop:

```sh
sandscale workspace ssh gemini-box -- 'cd ~/src/my-service && gemini -p "fix the type errors" --approval-mode auto_edit'
echo "gemini exited with $?"
```

`sandscale workspace ssh` runs the words after `--` on the workspace with a short-lived certificate and a checked host key. Like ssh, it joins those words with spaces, so quote the remote command.

## 5. Stop it

```sh
sandscale workspace schedule idle gemini-box 30m
sandscale workspace stop gemini-box
```

Stopping keeps the disk, and the workspace uses your plan’s vCPU-hours only while it runs. Your organization’s policy can shorten a schedule you set.

## Local `--sandbox` or a workspace?

Gemini CLI’s `--sandbox` flag runs it “in a sandboxed environment for safer execution” on the machine you start it on. A workspace moves the whole agent to a separate microVM with its own kernel. They are not exclusive: you can still use Gemini CLI’s own options inside the workspace. For a broader look at isolation layers, see [containers, gVisor and Firecracker for agent code](https://sandscale.dev/blog/isolating-ai-agent-code-containers-gvisor-firecracker).

## FAQ

### How do I run Gemini CLI non-interactively?

Pass `-p` (or `--prompt`) with your prompt. Add `--output-format json` for a single JSON object with the response and stats, or `--output-format stream-json` for an event stream.

### What does exit code 53 mean in Gemini CLI?

Per Gemini CLI’s headless reference, 53 means the turn limit was exceeded. 42 is an input error, 1 a general error or API failure, and 0 success.

### Is the yolo flag still supported?

The CLI reference marks `--yolo` as deprecated and says to use `--approval-mode=yolo` instead.

### Can Gemini CLI run in a Sandscale workspace?

Yes. Sandscale’s workspace docs list Gemini CLI among the agents you can run inside a workspace. The workspace needs network access to reach Google’s API.

## Related

- [Running Claude Code headless in a cloud workspace](https://sandscale.dev/blog/claude-code-headless-in-a-cloud-workspace)
- [Running Codex with codex exec in a cloud workspace](https://sandscale.dev/blog/codex-exec-in-a-cloud-workspace)
- [Use case: a coding agent that keeps working after you close the laptop](https://sandscale.dev/blog/use-case-long-running-agent-in-a-workspace)

**On this page**

1. 1. Create a workspace
2. 2. Run it headless
3. 3. Pick an approval mode on purpose
4. 4. Branch on the exit code
5. 5. Stop it
6. Local --sandbox or a workspace?
7. FAQ
8. Related

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

- [November 27, 2026 · **A LlamaIndex.TS data agent that runs its Python in a Sandscale sandbox** · Use LlamaIndex.TS's agent workflow with tools that write and run Python in a Firecracker sandbox, built per sandbox so a run can only reach its own box. Upload data files, answer with the numbers, clean up. Type-checked.](https://sandscale.dev/blog/llamaindex-ts-agent-with-sandscale)
- [November 27, 2026 · **A sandbox for Claude Code: running it headless in a cloud workspace** · Run Claude Code with claude -p inside a Sandscale workspace, a persistent Firecracker microVM, so its Bash tool works on a cloud disk instead of your laptop. Setup, permission flags and what stays where.](https://sandscale.dev/blog/claude-code-headless-in-a-cloud-workspace)

---
Source: https://sandscale.dev/blog/gemini-cli-headless-in-a-cloud-workspace
