The sandbox metrics allows you to get information about the sandbox's CPU and memory usage.
Getting the metrics of a sandbox returns an array of timestamped metrics containing CPU, memory and disk usage information. The metrics are collected every 5 seconds.
```js import { Sandbox } from '@e2b/code-interpreter'const sbx = await Sandbox.create() console.log('Sandbox created', sbx.sandboxId)
// Wait for a few seconds to collect some metrics await new Promise((resolve) => setTimeout(resolve, 10_000))
const metrics = await sbx.getMetrics() // $HighlightLine
// 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, // memTotal: 507592704, // diskUsed: 1514856448, // diskTotal: 2573185024 // }, // { // timestamp: 2025-07-28T08:04:10.000Z, // cpuUsedPct: 0.2, // cpuCount: 2, // memUsed: 33316864, // memTotal: 507592704, // diskUsed: 1514856448, // diskTotal: 2573185024 // } // ]
```python
from time import sleep
from e2b_code_interpreter import Sandbox
sbx = Sandbox()
print('Sandbox created', sbx.sandbox_id)
# Wait for a few seconds to collect some metrics
sleep(10)
metrics = sbx.get_metrics() # $HighlightLine
# You can also get the metrics by sandbox ID:
# metrics = Sandbox.get_metrics(sbx.sandbox_id)
print('Sandbox metrics', metrics)
# Sandbox metrics
# [
# SandboxMetric(
# cpu_count=2,
# cpu_used_pct=13.97,
# disk_total=2573185024,
# disk_used=1514856448,
# mem_total=507592704,
# mem_used=30588928,
# timestamp=datetime.datetime(2025, 7, 28, 8, 8, 15, tzinfo=tzutc()),
# additional_properties={},
# ),
# SandboxMetric(
# cpu_count=2,
# cpu_used_pct=0.1,
# disk_total=2573185024,
# disk_used=1514856448,
# mem_total=507592704,
# mem_used=31084544,
# timestamp=datetime.datetime(2025, 7, 28, 8, 8, 20, tzinfo=tzutc()),
# additional_properties={},
# ),
# ]
</CodeGroup>
<Note>
It may take a second or more to get the first metrics after the sandbox is created. Until the first metrics are collected from the sandbox, you will get an empty array.
</Note>