Getting started · Read CPU and memory capacity6 / 7

Read CPU and memory capacity

Example on GitHub(packages/sdk/examples/system-resources.ts)

Now that you've watched a model download, it's worth checking what the machine underneath it can actually handle before you pick which model to run next.

getSystemResources reports the machine's capacity without loading a model first. It returns two fields:

  • capabilities: the machine's static hardware profile (CPU, memory, GPUs).
  • sample: an optional live usage reading, returned only when you pass { sample: true }.

Each field is a metric object with a status. Only a 'supported' field has a value to read. Detection runs per field, so a sandboxed environment can block the CPU vendor name while still reporting core count, or expose total memory but hide live process pressure.

Checking status === 'supported' before reading .value avoids breaking on the first field a platform doesn't support, instead of assuming detection succeeds or fails for the whole payload at once. A single printMetric helper narrows each field's status before formatting it. It's reused below for the CPU, memory, and sample fields, like so:

const { capabilities, sample } = await getSystemResources({ sample: true });

function printMetric<T>(label: string, metric: ResourceMetric<T>, format: (value: T) => string) {
  if (metric.status === "supported") {
    console.log(`▸ ${label}: ${format(metric.value)}`);
  } else {
    console.log(`▸ ${label}: ${metric.status}`);
  }
}

const gib = (bytes: number) => `${(bytes / 1024 ** 3).toFixed(2)} GiB`;

if (capabilities.cpu.status === "supported") {
  printMetric("Logical cores", capabilities.cpu.value.logicalCores, String);
}

printMetric("Total memory", capabilities.memory.totalBytes, gib);

if (sample) {
  printMetric("Memory in use", sample.memory.usedBytes, gib);
}

Notice printMetric runs again for logicalCores, even though the CPU's own status already passed. That outer check only confirms the CPU object itself was detected; logicalCores is wrapped in its own metric one level down, so it can fail independently of the CPU object it belongs to.

Note: sample stays undefined unless the request opts in with { sample: true }, since a live reading costs a fresh measurement instead of reusing the static capabilities snapshot.

Questions

Question 1 of 2

What is a key characteristic of the status field on a metric object?

Question 2 of 2

What happens if getSystemResources runs without { sample: true }?

index.ts
Loading editor...

Run your code, check your answer, or ask a question. It all shows up here.