Skip to main content
AutoPilot for sandboxes works through two REST endpoints that the TypeScript package @costgraph/computesdk calls 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, stops, or fails to start, report it with POST /api/v1/tenant/sandboxes/events.
You can report sandboxes without routing them. CostGraph then tracks their cost, and they stay on the providers you chose. These endpoints are for code that holds its own provider keys. To have CostGraph start sandboxes on the accounts your team connected on Integrations, refer to Run sandboxes on your connected accounts.

Requirements

  • A CostGraph API key with the sandbox:route scope. Create it in Settings > 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 to https://api.costgraph.ai. Responses wrap their payload in data. Refer to the CostGraph API introduction for authentication and the tenant header.

Choose a provider

Send the size of the new sandbox and your providers in your preferred order. The first provider is the baseline.
The request takes the following fields. The response lists the order to try your providers in. The following example shows one candidate, with illustrative numbers.
Try the providers in order until one creates the sandbox. Send decision_id back with the sandbox’s started event. candidates holds each provider’s price, expected cost, and start figures, as described in Expected cost and start time. The routing endpoint reference lists every field and the full order within each policy.

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 for an empty providers list or invalid JSON, and 403 when the key lacks the sandbox:route scope. Give the call a short timeout, such as 300 ms. If it times out or fails, create the sandbox in your own provider order. To avoid a call before every sandbox, reuse an answer for a few minutes for the same size and provider list. When enforced_budget is true, your team has a budget set to stop sandboxes. Do not start a sandbox while you cannot reach the route endpoint. Refer to the enforce field of Create a budget.

Report sandboxes starting and stopping

Send up to 100 events in one call.
The response counts the recorded events and lists the rejected ones, as in {"data": {"accepted": 3, "rejected": []}}. Each event takes the following fields. CostGraph ignores a repeated started or stopped event for the same sandbox, and keeps a stop that arrives before its start for up to 2 days. An invalid event is listed in rejected with its index and problem, and the others are recorded. Too many requests answer 429. Send a failed event for each create a provider refuses, because failed starts lower the provider’s success rate. Send a stopped event for every sandbox you end. A sandbox without one shows as running until its run time reaches 24 hours.

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. If your team has a budget set to stop sandboxes, raise in the except branch instead of falling back.

Read settings and results

The Sandboxes tab reads the following endpoints. You can call them with an API key that has the focus:read scope, or as a signed-in member. These endpoints 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. Admins signed in to CostGraph can change settings with PUT /api/v1/tenant/sandboxes/autopilot/settings. Send enabled and, optionally, policy and ready_timeout_seconds. Fields you leave out keep their current values. ready_timeout_seconds applies only to sandboxes CostGraph starts. Refer to Readiness limit.