An open-source, faster Devin alternative
TypeScript
Docs: tighten the usage guide
Igor Zalutski16ac2a78307f
diff --git a/docs/sandboxes/usage.mdx b/docs/sandboxes/usage.mdxindex eba900f4..0bfa26b8 100644--- a/docs/sandboxes/usage.mdx+++ b/docs/sandboxes/usage.mdx@@ -3,22 +3,18 @@ title: "Usage Monitoring"description: "Inspect a sandbox's memory allocation and actual utilization over time"----Every sandbox emits resource samples while it runs. The platform-exposes them as a per-minute time series plus envelope totals — so-you can answer "is this sandbox actually using the memory I'm paying-for?" or "show me memory pressure over the last hour."+[`GET /api/sandboxes/:id/usage`](/api-reference/usage/get-sandbox-usage)+returns a per-minute time series of a sandbox's memory — both the+tier you provisioned and what the process actually consumed —+together with envelope totals. Use it to answer "am I over-paying for+this sandbox" or "when did memory spike?" For cross-sandbox+leaderboards (rank by spend, group by tag) see the aggregator at+[`GET /api/usage`](/api-reference/usage/get-usage) instead.-The API surface is one endpoint: [`GET /api/sandboxes/:id/usage`](/api-reference/usage/get-sandbox-usage).-A separate aggregator at [`GET /api/usage`](/api-reference/usage/get-usage)-serves the cross-sandbox leaderboard question — use that one when you-want to rank by spend across many sandboxes.+## Example-## What you can build--The shape of the response lets you reconstruct a familiar memory-chart: allocated tier as a step line (it only changes when you-resize), measured usage as a smooth curve underneath. The gap between-them is the headroom you're paying for but not using.+Allocated tier as a step line (it changes only on resize), measured+usage as a smooth curve. The gap between them is paid-for headroom.<svg viewBox="0 0 800 380" xmlns="http://www.w3.org/2000/svg" style={{width: "100%", height: "auto", maxWidth: "800px", display: "block", margin: "1.5em auto"}} role="img" aria-label="Example chart: a sandbox's allocated and used memory over 24 hours, showing a mid-day resize event, a load-test peak, and a brief stop"><g stroke="#e5e7eb" strokeWidth="1">@@ -82,14 +78,11 @@ them is the headroom you're paying for but not using.</g></svg>-The whole chart above is just two arrays out of `points[]`:-`{ts, allocatedMemoryMb}` and `{ts, usedMemoryMbAvg}`. The annotations-on it — resize, peak, downtime — fall out of the same data without a-separate API call: a resize is a step in `allocatedMemoryMb`, the-peak is the bucket where `usedMemoryMbPeak` is highest, and downtime-is any stretch of `uptimeSeconds: 0`.+Everything on the chart — the resize step, the peak, the brief gap —+falls out of `points[]` directly: `allocatedMemoryMb` is the step,+`usedMemoryMbPeak` finds the peak, `uptimeSeconds: 0` marks downtime.-## What you get back+## Response shape```json{@@ -111,27 +104,23 @@ is any stretch of `uptimeSeconds: 0`.}```-`points[]` is a sequence of 1-minute buckets. `totals` is the-envelope sum — server-computed, guaranteed to equal `SUM(points[].field)`-for each additive field.+`totals` is the server-side sum of `points[]` — summing any+additive field across the array reproduces the matching `totals.*`+exactly. Full field semantics live in the+[API reference](/api-reference/usage/get-sandbox-usage).## Allocated vs used-The two memory metrics answer different questions, and the gap-between them is usually where the interesting signal lives.--- **`memoryAllocatedGbSeconds`** — what the sandbox's plan promised,- integrated over time. This is the same physical quantity the bill- is computed from. If you resized the sandbox mid-window, the- points reflect the time-weighted blend.-- **`memoryUsedGbSeconds`** — what the sandbox process actually- consumed, sampled every 60s and integrated. This is the headroom- signal — if it's much lower than the allocated number, you're- over-provisioned.+- **`memoryAllocatedGbSeconds`** — the tier you provisioned, integrated+ over time. Same physical quantity the bill is computed from.+ Resize mid-window shows up as a time-weighted blend in the affected+ bucket.+- **`memoryUsedGbSeconds`** — measured resident memory, sampled every+ 60s and integrated. The headroom signal: low ratio of used over+ allocated means you're over-provisioned.-Ratios are stable: both are in **GiB-seconds** (binary,-2<sup>30</sup> bytes/GiB), so `used / allocated` gives you a clean-utilization fraction.+Both are in **GiB-seconds** (binary, 2<sup>30</sup> bytes/GiB), so+`used / allocated` gives a clean utilization fraction.## Calling it@@ -142,31 +131,17 @@ import { Usage } from "@opencomputer/sdk";const usage = new Usage("https://app.opencomputer.dev", process.env.OC_API_KEY!);-// Default window = last 1 hour.-const recent = await usage.forSandbox("sb-abc");--console.log(- `Used ${recent.totals.memoryUsedGbSeconds.toFixed(1)} GiB·s of ` +- `${recent.totals.memoryAllocatedGbSeconds.toFixed(1)} allocated ` +- `(${(100 * recent.totals.memoryUsedGbSeconds / recent.totals.memoryAllocatedGbSeconds).toFixed(0)}% utilization)`,-);--// Or pass an explicit window — up to 30 days.-const day = await usage.forSandbox("sb-abc", {- from: new Date(Date.now() - 24 * 3600 * 1000).toISOString(),- to: new Date().toISOString(),-});-for (const p of day.points) {+// Default window = last 1 hour. Pass `from`/`to` for up to 30 days.+const r = await usage.forSandbox("sb-abc");+const pct = 100 * r.totals.memoryUsedGbSeconds / r.totals.memoryAllocatedGbSeconds;+console.log(`${pct.toFixed(0)}% utilization`);++for (const p of r.points) {// p.ts, p.usedMemoryMbAvg, p.allocatedMemoryMb — ready to chart}``````bash cURL-# Default: last hour-curl -s "$OC_API_URL/api/sandboxes/sb-abc/usage" \- -H "X-API-Key: $OC_API_KEY" | jq--# Explicit windowcurl -s "$OC_API_URL/api/sandboxes/sb-abc/usage?from=2026-05-27T00:00:00Z&to=2026-05-27T01:00:00Z" \-H "X-API-Key: $OC_API_KEY" | jq '.totals'```@@ -175,26 +150,11 @@ curl -s "$OC_API_URL/api/sandboxes/sb-abc/usage?from=2026-05-27T00:00:00Z&to=202## Bucket semantics-- Buckets are **1 minute, UTC-aligned**, not aligned to `from`. The- first and last buckets are clamped to the original window, so a- mid-minute `from` doesn't over-report.-- Minutes where the sandbox isn't running emit zero-points- (`uptimeSeconds: 0`, all integrals `0`) rather than nulls — charts- read continuous x-axes without phantom interpolation.-- If a sampling gap appears (e.g., the platform missed a measurement- window), `uptimeSeconds` still reads 60 because the sandbox was- running, but `usedMemoryMbAvg` reads 0 for those minutes — visible- as a brief flat-zero stretch under an otherwise continuous- allocated line.-- Window max is **30 days**. For longer-range questions, use the- aggregator at `/api/usage` and sum across sandboxes there.--## When to reach for the aggregator instead--If your question is "which of my sandboxes is the heaviest" or "show-me spend grouped by tag," [`GET /api/usage`](/api-reference/usage/get-usage)-is the right tool. It serves cross-sandbox aggregates with-filter/sort/cursor and doesn't materialize point-level data — much-cheaper at scale. The per-sandbox endpoint here is the drilldown-after you've picked one to look at.-+- Buckets are **1 minute, UTC-aligned**; boundary buckets clamp to+ the original window so a mid-minute `from` doesn't over-report.+- Minutes where the sandbox isn't running emit zero-points — gaps+ read as continuous flat zero on a chart, not nulls.+- If a measurement is missed, `uptimeSeconds` still reads 60 (the+ sandbox was running) but `usedMemoryMbAvg` reads 0 — visible as a+ brief dip under an otherwise continuous allocated line.+- Window max is **30 days**.