Skip to main content

How to Get a Job's Resource Usage with the API v3

Use the v3 API to get a job's resource usage

Overview

The API v3 endpoint GET /api/v3/jobs/{id}/resource-usage returns the CPU, memory, and network usage recorded for a job. Use it to check whether a job is sized correctly, for example to confirm that a job is hitting its memory limit before you move it to a larger resource class.

Before you begin

  • A personal API token. Send it as Authorization: Bearer <token>.

  • The job's UUID. V3 identifies jobs by UUID, not by job number.

  • A job that ran on CircleCI cloud with the Docker executor. API v3 is not available on CircleCI server.

Steps

  1. Find the job ID. List the jobs in a workflow with filter[workflow_id]:

     curl -g 'https://circleci.com/api/v3/jobs?filter[workflow_id]=<workflow-id>' \    --header "Authorization: Bearer $CIRCLE_TOKEN"

    Read the id of the job from data[].id. If you only have a project, see "Retrieve Build Data via the API" to walk from project to run to workflow to job.

  2. Request the resource usage for that job:

     curl 'https://circleci.com/api/v3/jobs/<job-id>/resource-usage' \    --header "Authorization: Bearer $CIRCLE_TOKEN"

Example response

{   "data": {
"id": "<job-id>",
"attributes": {
"resource_class": {
"name": "medium",
"cpu_count": 2,
"memory_limit_bytes": 4294967296
},
"parallel_executions": [
{
"execution": 0,
"interval_ms": 1000,
"cpu_cores": [0.4, 1.8, 1.9],
"memory_bytes": [524288000, 1610612736, 2147483648],
"network_rx_bytes": 104857600,
"network_tx_bytes": 2097152
}
]
}
}
}

Field

Meaning

resource_class

The resource class the job ran on, with its cpu_count and memory_limit_bytes. May be absent.

parallel_executions

One entry per parallel execution, numbered from 0.

interval_ms

Time between samples.

cpu_cores, memory_bytes

Ordered samples, one per interval. Samples carry no timestamps. The first sample is the first collection after the execution started.

network_rx_bytes, network_tx_bytes

Network bytes received and sent.

To judge sizing, compare the highest memory_bytes and cpu_cores values with memory_limit_bytes and cpu_count. Memory close to the limit, or CPU pinned at cpu_count, suggests the job needs a larger resource class.

Limitations

  • Docker only. Machine and runner jobs return 200 with an empty parallel_executions list.

  • 90-day retention. Recordings expire after 90 days.

  • Missing is not "did not run". An execution that recorded nothing is left out of the list instead of returned empty.

Errors

Branch on the HTTP status code, not on the message.

Code

Meaning

400

Invalid input, such as an id that is not a UUID.

401

The token is missing or invalid.

403

Your token cannot access this job.

404

No job exists with that ID. A job number or slug is not a valid ID. See "CircleCI API v3 Returns 404 Not Found".

429

Rate limited. Wait the number of seconds in the Retry-After header, then back off.

Additional Resources

Did this answer your question?