Skip to main content
AutoPilot for sandboxes works through two REST endpoints. The TypeScript package @costgraph/computesdk calls them for you. From Python or any other language, call them directly:
  1. Before you create a sandbox, ask CostGraph which of your providers to try first with POST /api/v1/tenant/sandboxes/route.
  2. When a sandbox starts or stops, report it with POST /api/v1/tenant/sandboxes/events.
You can report sandboxes without routing them. CostGraph then tracks what they cost, and they stay on the providers you chose.

Requirements

  • A CostGraph API key with the sandbox:route scope. Create it in Settings > Personal API Keys.
  • Your provider names as ComputeSDK spells them, such as e2b, daytona, modal, and vercel. CostGraph prices providers by these names.
Send the key in the X-API-Key header. The base URL is https://api.costgraph.ai. To act for a specific tenant, add the X-CostGraph-Tenant-ID header. Responses wrap their payload in data. Refer to the CostGraph API introduction for authentication and tenants.

Choose a provider

Send the size of the new sandbox and your providers in your preferred order.
The request takes the following fields. The response lists the order to try your providers in.
The numbers in this example are illustrative. The response has the following fields. Each candidate has the following fields. A figure that is unknown is null or left out. The guide explains expected cost and start time.

Order within each policy

When AutoPilot is on, the order follows your routing policy. The baseline is the first provider you sent.
  • cheapest puts providers with a price for the size first, lowest expected_cost first, then the others. A provider whose startup_p50_ms is more than twice the baseline’s goes after the other priced providers. A missing start time never holds a provider back.
  • fastest puts providers whose expected_cost is no more than the baseline’s first, fastest start first, then lower expected_cost. Providers without a start time follow them by expected_cost. Providers that cost more come next in the same way, then providers without a price. The baseline stays first unless a provider starts strictly faster.
  • balanced sets a limit at the baseline’s start time plus 100ms or 10%, whichever is larger. A provider whose expected_cost is no more than the baseline’s and whose start time is within the limit scores the baseline’s expected_cost minus its own, multiplied by the limit minus its startup_p50_ms. Providers that score above zero come first, highest score first, then faster start, then lower expected_cost. The other priced providers follow by start time, with those without one last, then by expected_cost. Providers without a price come last. The baseline stays first when no provider scores above zero.
In every policy, a provider other than the baseline whose success_rate is below 0.95 moves to the end. Remaining ties keep your order.

Reasons

reason takes one of the following values. While AutoPilot is off, CostGraph still records each answer, so the Sandboxes tab can show what routing would change.

Use a short timeout and fall back

The route endpoint answers 200 with your own order whenever it cannot choose. It answers 400 only when providers is empty or the body is not valid JSON, and 403 when the key lacks the sandbox:route scope. Don’t make a sandbox wait on CostGraph. Give the call a short timeout, such as 300ms. If the call times out, fails, or returns an error status, create the sandbox in your own provider order. Try the providers in order until one creates the sandbox. To avoid a call before every sandbox, reuse an answer for a few minutes for the same size and provider list. A changed setting reaches your sandboxes when your reused answer expires.

Report sandboxes starting and stopping

Send up to 100 events in one call.
The response gives the number of events recorded, as in {"data": {"accepted": 2}}. Each event takes the following fields. CostGraph records events with the following rules:
  • A started event is priced at the list price for its size at that moment. Without a size in the event, CostGraph uses the size from its decision_id. With neither, it uses the provider’s documented default size when the price list gives one.
  • A sandbox started where AutoPilot sent it counts toward savings. This applies when the answer moved the sandbox off your first provider and it started there at the routed size. CostGraph ignores an unknown decision_id.
  • Repeats are ignored. CostGraph ignores a second started or a second stopped for the same sandbox.
  • A stop can arrive before its start. CostGraph keeps it for up to 2 days until the start arrives. A stop is never recorded before its start.
  • Times are kept within the last 24 hours. An at older than 24 hours counts as 24 hours ago. An at in the future counts as the time CostGraph receives the event.
  • One invalid event rejects the whole request with 400. Keys and values in metadata cannot contain control characters.
  • Too many requests answer 429. Send events in batches of up to 100 and retry shortly.
CostGraph records events even while your organization’s billing needs attention. Report a stopped event for every sandbox you end. A sandbox without one shows as running, and its estimated cost grows until the 24-hour cap.

Python example

The following example uses the requests library. create_sandbox stands for your own code. It tries each provider in the order it receives and returns the provider that created the sandbox and the sandbox’s ID.
Call the final report when you destroy the sandbox. In a busy service, queue events and send them in batches of up to 100 so reports don’t slow your sandbox calls.

Read settings and results

The AutoPilot page reads the following endpoints. You can call them too, with an API key that has the focus:read scope or as a signed-in member. summary and runs take from and to as YYYY-MM-DD, where to is the day after the last day. The default period is the last 30 days, and a period over 400 days is rejected. Changing settings with PUT on the settings path is for owners and admins signed in to CostGraph. Send enabled and, optionally, policy. Without policy, the current policy is kept.