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

# Run sandboxes on your connected accounts

> Start, use, and end sandboxes on the sandbox providers your team connected to CostGraph, with AutoPilot choosing the provider and trying the next one when a start fails.

CostGraph can start sandboxes for you on the sandbox provider accounts your
team connected on Integrations. Your code calls the CostGraph sandbox API and
holds no provider key. AutoPilot for sandboxes chooses the provider and tries
the next one when a start fails. The sandboxes run in your provider accounts,
each provider bills you as before, and the sandboxes appear on the
**Sandboxes** tab of **AutoPilot**.

If your code holds the provider keys itself, use
[AutoPilot for sandboxes](https://docs.costgraph.ai/costgraph/autopilot/sandboxes.md) with ComputeSDK, or
the [routing API](https://docs.costgraph.ai/costgraph/autopilot/sandboxes-api.md), instead.

To start and use sandboxes from your coding agent, refer to
[Run code in a sandbox](https://docs.costgraph.ai/costgraph/mcp/usage.md#run-code-in-a-sandbox).

## Requirements

* At least one sandbox provider connected on **Integrations > Sandbox**, as
  described in [Connect](https://docs.costgraph.ai/costgraph/workflows/managed-sandboxes.md#connect).
  [Supported sandbox providers](https://docs.costgraph.ai/costgraph/workflows/sandbox-providers.md) lists
  the providers.
* A CostGraph API key with the `sandbox:run` scope. Create it in
  **Settings > API Keys**. Signed-in members and the
  [CostGraph MCP server](https://docs.costgraph.ai/costgraph/mcp/install.md) can also call these endpoints.

Send the key in the `X-API-Key` header to `https://api.costgraph.ai`. Responses
wrap their payload in `data`.

## How a provider is chosen

When you start a sandbox without naming a provider, CostGraph orders your
connected providers with your
[routing policy](https://docs.costgraph.ai/costgraph/autopilot/sandboxes.md#policies). It tries them in that
order until one starts the sandbox. The following rules apply:

* Your first connected provider is the baseline. Providers are ordered by
  when you connected them, oldest first.
* While AutoPilot for sandboxes is off, CostGraph tries your providers in that
  order.
* If a provider has several connections, CostGraph uses the oldest working
  one.
* `provider` limits the start to one provider.
* A [budget set to stop sandboxes](https://docs.costgraph.ai/api-reference/budgets/create-a-budget.md)
  removes the providers it covers while it is over.

CostGraph tries the next provider when a provider refuses the create or the
sandbox is not ready within the readiness limit. Each failed start is recorded
with a class of `quota`, `capacity`, `auth`, `timeout`, or `other`, and lowers
the provider's success rate. When a provider refuses CostGraph's key, later
sandboxes skip it until you reconnect it on **Integrations**.

### Readiness limit

A provider has 30 seconds to make a new sandbox ready. After that, CostGraph
counts the start as failed with class `timeout` and tries the next provider.
Admins can set the limit from 5 to 600 seconds in **Ready timeout**, under
**AutoPilot > Sandboxes > Settings**. The limit also applies to workflow
sandboxes that
[AutoPilot chooses](https://docs.costgraph.ai/costgraph/workflows/your-environment.md#choose-where-a-goal-runs).

## Start a sandbox

Send the size and your tags. Allow the request a few minutes, because CostGraph
can try more than one provider.

```shell theme={null}
curl https://api.costgraph.ai/api/v1/tenant/sandboxes \
  -H "X-API-Key: $COSTGRAPH_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "size_class": "md",
    "metadata": { "agent": "researcher", "run": "run-42" },
    "lifecycle": { "timeout_ms": 3600000 }
  }'
```

The request takes the following fields. All are optional.

| Field | Description |
| - | - |
| `provider` | Start the sandbox only on this connected provider, such as `e2b`. |
| `size_class` | One of the [sizes](https://docs.costgraph.ai/costgraph/autopilot/sandboxes.md#sizes), from `xs` to `2xl`. |
| `vcpus`, `memory_mib` | A size that is not a standard size. Without any size, each provider starts its own default size. |
| `policy` | `cheapest`, `balanced`, or `fastest`, to override your team's policy for this sandbox. |
| `metadata` | Up to 16 string tags. Keys can be up to 64 characters and values up to 256. |
| `lifecycle.timeout_ms` | How long the provider keeps the sandbox before it ends it, from 1,000 to 86,400,000 milliseconds. |

CostGraph passes your tags to the provider as labels. The `run` tag is the
sandbox's run key. Without one, CostGraph generates a key. Refer to
[Group cost by run](https://docs.costgraph.ai/costgraph/autopilot/sandboxes.md#group-cost-by-run). Keys that
start with `costgraph.ai/` are reserved, and CostGraph drops them from your tags.

The response describes the sandbox.

```json theme={null}
{
  "data": {
    "provider": "daytona",
    "sandbox_id": "sbx_123",
    "decision_id": "6f1c2a9e-0b7d-4c41-9a53-2f7f4f0f9b11",
    "started_at": "2026-11-08T12:00:00Z",
    "price_per_hour": 0.0828,
    "routed_from": "e2b"
  }
}
```

Use `provider` and `sandbox_id` in the sandbox's paths. `price_per_hour` is the
list price in USD, or `null` without a price. `routed_from` names the provider
tried first, when the sandbox started on another provider.

## Use a sandbox

Each sandbox has its own path, `/api/v1/tenant/sandboxes/{provider}/{sandbox_id}`.
These calls reach only sandboxes started through this API or the CostGraph MCP
tools. Every call records the time as the sandbox's last use.

| Call | Does |
| - | - |
| `GET /api/v1/tenant/sandboxes` | Lists your running sandboxes, newest first, with `started_at`, `last_used_at`, `price_per_hour`, `metadata`, `seconds`, and `cost_usd`. |
| `GET .../{provider}/{sandbox_id}` | Checks with the provider that the sandbox still runs, and returns it with `running: true`. |
| `POST .../{provider}/{sandbox_id}/exec` | Runs a shell command. |
| `GET .../{provider}/{sandbox_id}/files?path=<path>` | Returns the file at `path` as `content_base64`. |
| `PUT .../{provider}/{sandbox_id}/files?path=<path>` | Writes `{"content_base64": "<base64>"}` to `path`, replacing any file there, and answers `204`. |
| `DELETE .../{provider}/{sandbox_id}` | Ends the sandbox and answers `204`. |

Files can be up to 4 MiB. The list does not include sandboxes started by
workflows, CI jobs, or your own code.

### Run a command

Send the command to the sandbox's `exec` path.

```shell theme={null}
curl https://api.costgraph.ai/api/v1/tenant/sandboxes/daytona/sbx_123/exec \
  -H "X-API-Key: $COSTGRAPH_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"command": "python3 task.py", "cwd": "/workspace", "timeout_ms": 50000}'
```

`command` is required, up to 64 KiB. `env` takes up to 100 environment
variables. `timeout_ms` can be up to 55,000 milliseconds, and the default wait
is 30 seconds. The response has `exit_code`, `stdout`, and `stderr`. Each stream
holds up to 256 KiB, and `truncated` is `true` when CostGraph cut either one. For a
longer task, send `background: true`, write the output to a file, and read the
file when the task ends.

## Connect to sandboxes from TypeScript

`costgraphConnections()` in `@costgraph/computesdk` is a ComputeSDK provider
that calls this API.

```ts theme={null}
import { compute } from 'computesdk';
import { costgraphConnections } from '@costgraph/computesdk';

compute.setConfig({
  provider: costgraphConnections({ apiKey: process.env.COSTGRAPH_API_KEY, policy: 'cheapest' }),
});

const sandbox = await compute.instances.create({ sizeClass: 'md', metadata: { agent: 'researcher' } });
const { stdout, truncated } = await sandbox.runCommand('python3 task.py', { timeout: 50_000 });
await sandbox.filesystem.writeFile('/tmp/input.json', JSON.stringify(input));
const report = await sandbox.filesystem.readFile('/tmp/report.md');
await sandbox.destroy();
```

`costgraphConnections()` accepts the following options.

| Option | Default | Description |
| - | - | - |
| `apiKey` | None | A CostGraph API key with the `sandbox:run` scope. Set this or `accessToken`. |
| `accessToken` | None | An OAuth access token that can use the CostGraph MCP tools. |
| `baseUrl` | `https://api.costgraph.ai` | The CostGraph API address. |
| `tenantId` | None | The tenant, sent as the `X-CostGraph-Tenant-ID` header. |
| `policy` | Your team's policy | `cheapest`, `balanced`, or `fastest` for these sandboxes. |
| `provider` | None | Start every sandbox on this connected provider. |
| `fetch` | Global `fetch` | A custom `fetch` implementation. |

Sandboxes support `runCommand`, `getInfo`, `destroy`, `filesystem.readFile`, and
`filesystem.writeFile`. Snapshots and volumes are not available. `timeout` on
`create()` becomes `lifecycle.timeout_ms`. Read a generated run key with
`runKey(sandboxId)` on the provider.

## How sandboxes stop

CostGraph does not end a sandbox on its own. A sandbox stops when you end it,
or when the provider ends it after `lifecycle.timeout_ms` or its own timeout.
When a call finds that the provider ended the sandbox, CostGraph records it as
stopped and the call answers `404`.

A running sandbox keeps billing until it stops. When its cost since its last
use passes the insight cost floor, CostGraph opens a
[forgotten sandbox insight](https://docs.costgraph.ai/costgraph/insights.md#sandboxes).

## Errors

Each error answers with a message that says what to do.

| Status | Meaning |
| - | - |
| `400` | The request is not valid. The message names the field. |
| `403` | The API key lacks the `sandbox:run` scope. |
| `404` | The sandbox does not exist, was not started through this API or the MCP tools, or has ended. |
| `409` | No sandbox provider is connected, the named provider is not connected, or a budget set to stop sandboxes is over. |
| `413` | The file is larger than 4 MiB. |
| `422` | Every provider refused the sandbox, and the message says what to change. For a file, the path is not a readable file. |
| `502` | The sandbox may not have started, or may still be starting. List your sandboxes before you retry, and end any you do not need. |
| `503` | The provider is unavailable. Retry shortly. |
| `500` | The request could not be completed. Retry shortly. |


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