An open-source, faster Devin alternative
TypeScript
Docs: conceptual usage guide + cross-references
Igor Zalutski38be5a71087a
diff --git a/docs/api-reference/usage/get-usage.mdx b/docs/api-reference/usage/get-usage.mdxindex 4e9bbc2a..9e162c08 100644--- a/docs/api-reference/usage/get-usage.mdx+++ b/docs/api-reference/usage/get-usage.mdx@@ -3,9 +3,16 @@ title: 'Usage Aggregator'api: 'GET /api/usage'----Aggregate GB-second spend grouped by sandbox or by tag. Dollars are-not exposed — Stripe owns pricing, and this endpoint returns the same-physical quantities the invoice is computed from.+Aggregate spend grouped by sandbox or by tag. Dollars are not exposed+— Stripe owns pricing, and this endpoint returns the same physical+quantities the invoice is computed from.++<Note>+All `*GbSeconds` fields are **GiB-seconds** (binary, 2<sup>30</sup>+bytes/GiB), not decimal GB. For a sandbox provisioned at 1024 MiB+running for one second, `memoryGbSeconds` is 1. The wire name uses+`Gb` for backwards compatibility.+</Note><Note>Freshness: accurate to the minute for cleanly-shutdown sandboxes.@@ -14,6 +21,9 @@ restarts; until then, accrual keeps going. This is the same behavioras the Stripe pipeline.</Note>+For per-sandbox time-series drilldown (memory utilization over time,+1-minute resolution), see [`GET /api/sandboxes/:id/usage`](/api-reference/usage/get-sandbox-usage).+<ParamField query="groupBy" type="string" required>`sandbox` for top sandboxes, or `tag:<key>` for spend by tag value.Keys may contain `:` — everything after the first `:` is the tagdiff --git a/docs/docs.json b/docs/docs.jsonindex 5012e24b..faedda7b 100644--- a/docs/docs.json+++ b/docs/docs.json@@ -45,7 +45,8 @@"sandboxes/secrets","sandboxes/patches","sandboxes/preview-urls",- "sandboxes/elasticity"+ "sandboxes/elasticity",+ "sandboxes/usage"]},{diff --git a/docs/sandboxes/usage.mdx b/docs/sandboxes/usage.mdxnew file mode 100644index 00000000..73a6d112--- /dev/null+++ b/docs/sandboxes/usage.mdx@@ -0,0 +1,130 @@+---+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" without+running your own collector.++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.++## What you get back++```json+{+ "sandboxId": "sb-abc",+ "alias": "my-agent",+ "from": "2026-05-27T00:00:00Z",+ "to": "2026-05-27T01:00:00Z",+ "totals": {+ "memoryAllocatedGbSeconds": 3600,+ "memoryUsedGbSeconds": 2180,+ "uptimeSeconds": 3600,+ "memoryAllocatedPeakMb": 1024,+ "memoryUsedPeakMb": 742+ },+ "points": [+ { "ts": "2026-05-27T00:00:00Z", "memoryAllocatedGbSeconds": 60, "memoryUsedGbSeconds": 35.86, "uptimeSeconds": 60, "allocatedMemoryMb": 1024, "usedMemoryMbAvg": 612, "usedMemoryMbPeak": 720 },+ { "ts": "2026-05-27T00:01:00Z", "memoryAllocatedGbSeconds": 60, "memoryUsedGbSeconds": 40.02, "uptimeSeconds": 60, "allocatedMemoryMb": 1024, "usedMemoryMbAvg": 683, "usedMemoryMbPeak": 742 }+ ]+}+```++`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.++## 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. Comes from `sandbox_scale_events` and is+ the same physical quantity the billing pipeline reports. If you+ resized the sandbox mid-window, the points reflect the+ time-weighted blend.+- **`memoryUsedGbSeconds`** — what the qemu process's cgroup actually+ reported as `memory.current`, sampled every 60s and integrated.+ This is the "headroom" signal — if it's much lower than the+ allocated number, 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.++## Calling it++<CodeGroup>++```typescript TypeScript+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) {+ // 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 window+curl -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'+```++</CodeGroup>++## 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.+- During collector outages (the worker dies or restarts), `uptimeSeconds`+ still reads 60 because a scale event is open. `usedMemoryMbAvg` will+ be 0 for those minutes — that's how you spot "we should have data+ but don't" on a chart.+- 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.++<Note>+v1 ships memory only. CPU mirrors the same shape (`cpuUsedCoreSeconds`,+`usedCpuPctAvg`, etc.) once the worker collector starts populating+cgroup `cpu.stat` — additive change, no shape break.+</Note>