Metrics
The E2B SDK's sandbox.getMetrics() and Sandbox.getMetrics(id) return CPU, memory, and disk samples for a sandbox, as on E2B. This page covers the two metrics endpoints, the fields of each sample, and how time ranges and buckets work.
Prerequisites
- The deployment is connected to an instance metrics store. See Instance metrics storage.
Without a store, both metrics endpoints return 501 with the capability name metrics.
Important
Metrics are for observation only and are not usage or billing data. Samples can be missing for some periods; see Notes.
Reading metrics
The following example reads the samples of a sandbox and prints the CPU and memory use of the most recent one:
const samples = await sandbox.getMetrics();
const latest = samples.at(-1);
console.log(latest?.cpuUsedPct, latest?.memUsed);The SDK calls one of two endpoints:
| Request | Returns |
|---|---|
GET /sandboxes/{id}/metrics | An array of samples for one sandbox, bucketed by time. Accepts start and end, in Unix seconds |
GET /sandboxes/metrics?sandbox_ids=... | The most recent sample of each sandbox, keyed by sandbox ID. See Batch endpoint |
Sample fields
| Field | Meaning |
|---|---|
cpuCount | The number of online CPUs in the guest |
cpuUsedPct | The busy share of all CPUs between two consecutive samples, from 0 to 100 |
memTotal, memUsed, memCache | MemTotal, MemTotal minus MemAvailable, and Cached from the guest's /proc/meminfo, in bytes |
diskTotal, diskUsed | Total capacity and used space (total minus available) of the guest's root file system /, in bytes |
timestamp, timestampUnix | The start of the bucket, in UTC RFC 3339 and in Unix seconds |
Time range and buckets
Sampling
- The node samples each running sandbox every 5 seconds.
- After a sandbox starts or resumes, samples begin with the second sampling, because the first one only establishes the CPU baseline.
Time range
- Without
startandend, the whole history of the sandbox within the retention period is returned. - If only one end is given, the other end is the start or end of the sandbox's history.
- The end is no later than the current time, and the start is no earlier than the beginning of the retention period.
startandendmust be non-negative integers. Ifstartis later thanend, the request returns 400.- Only buckets that have ended, at a time no later than the current time minus the query delay, are returned. Newer buckets are returned once their samples in the store are final, so samples from roughly the last 30 seconds are not returned yet. The deployment sets the query delay, 30 seconds by default; see E2B gateway configuration.
Bucket width
The bucket width depends on the query span, as in E2B:
| Span | Bucket width |
|---|---|
| Less than 1 hour | 5 seconds |
| Less than 6 hours | 30 seconds |
| Less than 12 hours | 1 minute |
| Less than 24 hours | 2 minutes |
| Less than 7 days | 5 minutes |
| 7 days or more | 15 minutes |
- Each field of a bucket takes the maximum of the samples in that bucket, and the timestamp is the start of the bucket.
- Only buckets in which all seven fields have values are returned.
- The first and last buckets are both full buckets, so they may include samples just outside
[start, end].
Pause, resume, and deletion
- After a pause and resume, it is still the same series. Each resume starts a new Instance, and the gateway merges the samples of all Instances by sandbox.
- There are no samples while the sandbox is paused.
- After a sandbox is deleted, its metrics can still be queried within the retention period.
Missing sandboxes and empty results
- A sandbox that does not exist, or that belongs to another deployment namespace, returns 404.
- A sandbox that exists but has no samples yet returns
[]. - Unlike on E2B, a sandbox whose Platform's guest predates the sampling interface is not sampled by the node, and queries return
[].
Batch endpoint
GET /sandboxes/metrics returns the latest sample of several sandboxes in one request:
sandbox_idsis comma-separated, with at most 100 IDs and no duplicates. If it is missing or empty, the request returns 400.- Each sandbox returns its latest sample up to "current time minus query delay".
- The latest sample is therefore usually 30 to 60 seconds old.
- A sandbox with no sample in the 30 seconds before that moment does not appear in the result. Neither do sandboxes that do not exist or belong to other deployments. The request still returns 200.
Notes
- When the store is unreachable, the guest does not respond in time, or the node restarts, samples for that period are missing.
- When the store is unavailable, both endpoints return 500 with the message
failed to fetch sandbox metrics. - Each gateway instance caches the query result for a single sandbox for 1 second.
- Team metrics are not provided. See Not supported.
Next steps
- Instance metrics storage: connect a metrics store to the deployment.
- Sandboxes and lifecycle: pause, resume, and delete sandboxes.
- E2B compatibility: the other SDK areas the gateway supports.
Volumes
A Volume keeps a sandbox's data across pauses and after the sandbox is deleted. This page covers creating Volumes by name, mounting one in a sandbox, deleting it, and what queries return.
Application ports
The E2B gateway forwards HTTP and WebSocket requests for {port}-{sandboxID}.<domain> to the same port on loopback inside the sandbox. This page shows how to reach a port, how to make it private, how requests reach the application, and what to expect from desktops and Chrome DevTools.