> ## Documentation Index
> Fetch the complete documentation index at: https://docs.beam.cloud/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> Install the Beam CLI, sign in, and connect coding agents to the Beam MCP server with `curl -fsSL https://www.beam.cloud/install | sh`. To also create a VM, run `curl -fsSL https://www.beam.cloud/install | sh -s -- --vm`.
> VMs are persistent Linux machines without GPUs. Defaults: 4 CPUs, 8 GiB of RAM, a 50 GiB disk, Ubuntu 24.04, and a browser desktop. A running VM accrues usage until you stop, pause, or remove it, or until an idle timeout puts it to sleep.
> Stopping a VM keeps its disk and discards RAM and processes. Pausing keeps both, and resuming restores them. To pause after 5 idle minutes and wake on access, run `beam vm update NAME --ttl 300 --idle-action pause --auto-resume`.
> Core VM commands: `beam vm new NAME`, `beam vm ssh NAME`, `beam vm sync NAME ./dir`, `beam vm exec NAME -- CMD`, `beam vm expose NAME PORT --protected`, `beam vm fork NAME NEW_NAME`, `beam vm pause NAME`, `beam vm stop NAME`, `beam vm rm NAME`.
> For GPUs, use a sandbox or a deployed endpoint. Sandboxes are disposable environments that boot in seconds and fork from snapshots.
> Current rates are at https://www.beam.cloud/pricing.

# Run Agent Code in a Sandbox

> Create a sandbox, run code, serve a URL, and fork it from a snapshot

A sandbox is a disposable environment your code creates, uses, and throws away. In about five minutes you'll create one, run code in it, serve a URL, fork it from a memory snapshot, and attach a GPU. For a persistent machine that keeps its disk between sessions, start with the [VM quickstart](https://docs.beam.cloud/v2/getting-started/quickstart-vm.md).

> [!TIP]
>
> Using Cursor, Claude Code, or Codex? Run `beam setup agent` to connect your coding agent to your workspace. See [agent setup](https://docs.beam.cloud/v2/getting-started/add-to-cursor-claude.md).

## Install and Sign In

```bash theme={null}
python3 -m venv .venv
source .venv/bin/activate
python -m pip install --upgrade beam-client
beam login
```

Finish signing in through your browser, then run `beam whoami` to check your workspace.

## Run Code, Serve a URL, and Fork

Save this as `sandbox.py` and run `python sandbox.py`:

```python sandbox.py theme={null}
from beam import Sandbox

# Create a sandbox and run code
sb = Sandbox(cpu=1, memory="1Gi", keep_warm_seconds=600).create()
print(sb.process.run_code("print(sum(range(10)))").stdout)  # 45

# Start a web server and get a URL
sb.fs.write_text("/workspace/index.html", "Hello from a Beam sandbox")
sb.process.exec(
    "python3", "-m", "http.server", "8000", "--bind", "0.0.0.0",
    cwd="/workspace",
)
print(sb.expose_port(8000))

# Snapshot memory, including the running server, then fork it
snapshot_id = sb.snapshot_memory()
sb.terminate()

forks = [Sandbox().create_from_memory_snapshot(snapshot_id) for _ in range(3)]
for fork in forks:
    print(fork.list_urls())  # each fork serves its own copy
    fork.terminate()
```

> [!WARNING]
>
> Preview URLs are public. Add authentication in your app before serving anything sensitive.

## Add a GPU

Attach a GPU with one argument:

```python theme={null}
from beam import Sandbox

sb = Sandbox(gpu="RTX4090", memory="8Gi").create()
p = sb.process.exec("nvidia-smi")
p.wait()
print(p.logs.read())
sb.terminate()
```

Requesting a GPU doesn't install CUDA libraries. Add packages such as `torch` to the sandbox [image](https://docs.beam.cloud/v2/environment/custom-images.md).

## Control Outbound Traffic

Each sandbox is isolated from your app and other workloads. Block outbound traffic or allow only specific hostnames, wildcard domains, IPs or CIDRs:

```python theme={null}
from beam import Sandbox

sb = Sandbox(block_network=True).create()                        # no outbound traffic
sb.update_network_permissions(block_network=False, allow_list=[])  # reopen it at runtime
sb.terminate()

sb = Sandbox(allow_list=["api.openai.com"]).create()             # or allow only this API
sb.terminate()
```

To give the agent credentials, use [brokered secrets](https://docs.beam.cloud/v2/environment/secrets.md#brokered-secrets-for-vms-and-sandboxes). The guest receives placeholders; the worker supplies the real values only in HTTPS headers to approved hosts. With `block_network=True` and secrets, only their approved hosts are reachable. The guest has no automatic Beam workspace token.

## Next Steps

> [card] [Give an Agent a Dev Box](https://docs.beam.cloud/v2/getting-started/quickstart-vm.md)
>
> Create a persistent VM that pauses when idle.

> [card] [Snapshots](https://docs.beam.cloud/v2/sandbox/snapshots.md)
>
> Save filesystem or memory state and fork it.

> [card] [Networking](https://docs.beam.cloud/v2/sandbox/networking.md)
>
> Preview URLs and outbound rules.

> [card] [TypeScript SDK](https://docs.beam.cloud/v2/reference/ts-sdk.md)
>
> Create and operate sandboxes from Node.js.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.