@costgraph/computesdk, and every other ComputeSDK call stays
the same. Your sandboxes run in your own provider accounts, each provider bills
you as before, and your provider keys stay in your process.
Two other ways to use AutoPilot for sandboxes are available:
- From another language, call the REST API.
- To have CostGraph start sandboxes on the provider accounts your team connected on Integrations, refer to Run sandboxes on your connected accounts.
Requirements
- A CostGraph API key with the
sandbox:routescope. Create it in Settings > API Keys. - Node.js 18 or later, or another runtime with a global
fetch. computesdk4.1.15 or later, and the ComputeSDK package for each provider you use, such as@computesdk/e2b.
Set up the sandbox SDK
1
Install the package
Store the API key in your environment as
COSTGRAPH_API_KEY, then install
@costgraph/computesdk next to ComputeSDK.2
Wrap your providers
Move your ComputeSDK providers inside
costgraph(). The first provider in
the list is the baseline. Your sandboxes use it while AutoPilot is off, and
AutoPilot compares every other provider with it.3
Tag your sandboxes
Pass a size and tags when you create a sandbox. CostGraph groups cost by
the tags in The
metadata.run tag is the sandbox’s run key. Refer to
Group cost by run.4
Check that sandboxes arrive
Create a sandbox, then open AutoPilot in CostGraph and select the
Sandboxes tab. The sandbox appears under History with its
provider, size, and tags.
Flush reports on shutdown
The package sends start and stop reports in the background and does not keep your process running. Before your process exits, awaitflush(). It resolves
within 10 seconds by default, or within the limit you pass in milliseconds.
ctx.waitUntil(cg.flush()).
Package options
costgraph() accepts the following options.
Turn on routing
AutoPilot for sandboxes is off by default. While it is off, every sandbox starts on the first provider in your list, and CostGraph still records its cost.1
Open the settings
In CostGraph, open AutoPilot, select the Sandboxes tab, and select
Settings. Only admins can change these settings.
2
Turn it on
Turn on Enabled and select a Routing policy. The default is
Cheapest. Refer to Policies.
How a provider is chosen
When you create a sandbox with asizeClass, the package asks CostGraph to
order your providers for that size. It then creates the sandbox on the first
provider in that order. The following rules apply:
- Only providers that support the size take part. Refer to Sizes.
If none does,
create()throws ComputeSDK’sInstanceSizeError. - Your order is kept when AutoPilot is off, when the sandbox has no size, and when your first provider has no list price for the size.
- Only the first 20 providers are priced. A provider name CostGraph does not recognize keeps its place without a price.
Expected cost and start time
AutoPilot compares providers on expected cost, start time, and success rate.- Expected cost is the list price for the size over the expected run time, rounded up to the provider’s billing increment and minimum billed time. The expected run time is the median of your stopped sandboxes of that size from the last 30 days, capped at 24 hours. Until you have 20 of them, it is 10 minutes.
- Start time is the median time from the create request until the sandbox started, over your sandboxes of that size on that provider in the last 30 days.
- Success rate is the share of your creates on that provider in the last 30 days, at any size, that started.
Policies
Each policy orders your providers differently. A single sandbox can request another policy than the one in Settings.
In every policy, a provider other than the baseline with a success rate below
95% moves to the end. Providers without a price come after the priced ones.
The baseline stays first when no provider improves on it, and remaining ties
keep your order. The
routing endpoint reference
gives the full tie-break order.
When CostGraph doesn’t answer
A create waits up torouteTimeoutMs for a routing answer. If CostGraph is
slow, unreachable, or returns an error, the sandbox starts in your list order.
After a failed call, the package uses your list order for 30 seconds before it
asks again. A late answer is cached for the next create.
If your team has a budget set to stop sandboxes, the package waits up to 5
seconds instead. An over budget removes the providers it covers from the order.
When no provider is left, or when CostGraph cannot be reached, create() throws
and no sandbox starts. Refer to the enforce field of
Create a budget.
When a provider fails
If the chosen provider fails to create the sandbox, the package tries the next provider in CostGraph’s order, then your providers that the answer left out. It does not try another provider in the following cases, which match ComputeSDK:- The create has a
sizeClassor names a provider. - The create was aborted, or the provider marks the error as not safe to retry.
fallbackOnErrorisfalse.
Choose the provider yourself
To skip routing for one sandbox, call thecostgraph() provider directly and
name the provider. CostGraph still records the sandbox.
compute.instances.create({ provider: 'e2b' }) does not work for this, because
compute knows only the costgraph provider.
Sizes
sizeClass requests one of ComputeSDK’s standard sizes.
A provider whose ComputeSDK package maps sizes itself receives the size
unchanged. For the following providers, the package turns the size into the
provider’s own create options.
Runloop and Vercel are used for
xs only when none of your other providers
creates exactly 1 vCPU with 1 GiB. CostGraph then records the size they
created. ComputeSDK rejects CPU and memory options such as cpu, memory, or
resources next to sizeClass.
E2B sets CPU and memory per template, so it does not take part in sized
creates. Build an E2B template at the size you need, then create sandboxes
from it by name without sizeClass.
What CostGraph receives
The package sends only the following to CostGraph:- Your provider names and the size of each create.
- For each sandbox, its provider, sandbox ID, create, start, and stop times, and the routing answer it used.
- For each refused create, its provider, time, failure class, and error message, cut to 2,000 bytes.
- Your
metadatastring values and the run key. It sends at most 20 keys of up to 64 bytes, with values cut to 256 bytes.
costgraph.ai/run on the
sandbox, for providers whose ComputeSDK package passes metadata or labels on.
These are Blaxel, CreateOS, Daytona, E2B, Freestyle, GravixLayer, Lizard,
Microsandbox, Miosa, Novita, OpenComputer, Promigence, Runloop, Sprites,
Superserve, and Tenki.
Read the Sandboxes tab
Open AutoPilot and select the Sandboxes tab. The date range at the top of the page sets the period. The tab opens with the following totals.
A sandbox counts toward Saved only when it started on the provider
AutoPilot chose, at the size it was routed for.
The tab has the following views:
- Managed shows cost by provider, cost by tag for the 50 most expensive tags, and failed starts by provider.
- Settings turns routing on and sets the policy.
- History lists each sandbox with its provider, size, run time, cost, and tags. A routed sandbox shows how the provider was chosen and what it would have cost elsewhere.
- Runs lists each agent run with its sandbox cost, model cost, and total.
Group cost by run
A run key groups the sandboxes and model calls of one agent run, so you see what the whole run cost. Set the run key as therun tag on each sandbox of
the run. A sandbox without a run tag gets its own generated key, which you
read with cg.runKey(sandboxId).
To add the run’s model calls, send the same run key with that run’s usage
through your AI gateway. Refer to
Group usage by agent run.
The key must match the run tag exactly.
A workflow run also shows its
Workflow agent cost, which CostGraph pays for and does not add to the
run’s total.
Limits
- A sandbox that ends without
destroy()is not reported as stopped. This includes a sandbox that the provider’s own timeout ends. It shows as Running until its run time reaches 24 hours. Calldestroy()on every sandbox you finish with. - Costs are estimates from list prices. They exclude discounts, credits, and included usage. To see billed spend, connect the provider on the Integrations page where CostGraph supports it.