@costgraph/computesdk calls them for
you. From Python or any other language, call them directly:
- Before you create a sandbox, ask CostGraph which of your providers to try
first with
POST /api/v1/tenant/sandboxes/route. - When a sandbox starts or stops, report it with
POST /api/v1/tenant/sandboxes/events.
Requirements
- A CostGraph API key with the
sandbox:routescope. Create it in Settings > Personal API Keys. - Your provider names as ComputeSDK spells them, such as
e2b,daytona,modal, andvercel. CostGraph prices providers by these names.
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 response lists the order to try your providers in.
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.cheapestputs providers with a price for the size first, lowestexpected_costfirst, then the others. A provider whosestartup_p50_msis more than twice the baseline’s goes after the other priced providers. A missing start time never holds a provider back.fastestputs providers whoseexpected_costis no more than the baseline’s first, fastest start first, then lowerexpected_cost. Providers without a start time follow them byexpected_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.balancedsets a limit at the baseline’s start time plus 100ms or 10%, whichever is larger. A provider whoseexpected_costis no more than the baseline’s and whose start time is within the limit scores the baseline’sexpected_costminus its own, multiplied by the limit minus itsstartup_p50_ms. Providers that score above zero come first, highest score first, then faster start, then lowerexpected_cost. The other priced providers follow by start time, with those without one last, then byexpected_cost. Providers without a price come last. The baseline stays first when no provider scores above zero.
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 answers200 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.{"data": {"accepted": 2}}. Each event takes the following fields.
CostGraph records events with the following rules:
- A
startedevent is priced at the list price for its size at that moment. Without a size in the event, CostGraph uses the size from itsdecision_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
startedor a secondstoppedfor 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
atolder than 24 hours counts as 24 hours ago. Anatin the future counts as the time CostGraph receives the event. - One invalid event rejects the whole request with
400. Keys and values inmetadatacannot contain control characters. - Too many requests answer
429. Send events in batches of up to 100 and retry shortly.
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 therequests 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.
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 thefocus: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.