Appearance
Sandbox Metrics
The SDK provides functionality to monitor sandbox resource usage, including CPU, memory, and disk consumption. Metrics are collected every 5 seconds.
Retrieving Metrics
From an Active Sandbox Instance
javascript
import { Sandbox } from '@e2b/code-interpreter'
async function main() {
const sbx = await Sandbox.create()
// Wait for metrics to be collected
await new Promise(resolve => setTimeout(resolve, 6000))
const metrics = await sbx.getMetrics()
// You can also get the metrics by sandbox ID:
// const metrics = await Sandbox.getMetrics(sbx.sandboxId)
console.log('Sandbox metrics:', metrics)
// Sandbox metrics:
// [
// {
// timestamp: 2025-07-28T08:04:05.000Z,
// cpuUsedPct: 20.33,
// cpuCount: 2,
// memUsed: 32681984, // in bytes
// memTotal: 507592704, // in bytes
// diskUsed: 1514856448, // in bytes
// diskTotal: 2573185024 // in bytes
// },
// {
// timestamp: 2025-07-28T08:04:10.000Z,
// cpuUsedPct: 0.2,
// cpuCount: 2,
// memUsed: 33316864, // in bytes
// memTotal: 507592704, // in bytes
// diskUsed: 1514856448, // in bytes
// diskTotal: 2573185024 // in bytes
// }
// ]
await sbx.kill()
}
main()Using a Sandbox ID
javascript
import { Sandbox } from '@e2b/code-interpreter'
async function main() {
const sandboxId = 'your-sandbox-id'
const metrics = await Sandbox.getMetrics(sandboxId)
console.log(metrics)
}
main()Metrics Data Structure
Metrics are returned as an array, where each element contains resource usage for a single sample:
| Field | Type | Description |
|---|---|---|
timestamp | Date | Sample timestamp |
cpuUsedPct | number | CPU usage percentage |
cpuCount | number | Number of CPU cores |
memUsed | number | Used memory (bytes) |
memTotal | number | Total memory (bytes) |
diskUsed | number | Used disk space (bytes) |
diskTotal | number | Total disk space (bytes) |
Important Notes
First Metrics Delay: It may take 1 second or longer after sandbox creation for the first batch of metrics to be collected. Before that, getMetrics() returns an empty array. It is recommended to wait at least 5–6 seconds after creating a sandbox before retrieving metrics.
Best Practices
- Wait for First Sample: Wait at least 6 seconds after creating a sandbox before retrieving metrics.
- Poll Regularly: Retrieve metrics every 5 seconds or at longer intervals.
- Resource Alerts: Set thresholds to monitor resource usage and detect issues promptly.
- Performance Optimization: Use metrics data to optimize code and resource configuration.