How to programmatically determine build queue times

What would be the best way to determine how long our builds are queued for? We want to make sure we don’t have a large back up of jobs but we don’t want to pay for build agents we aren’t using.

I believe the following API endpoint will do the job

curl https://circleci.com/api/v1/project/:username/:project?circle-token=:token&limit=20&offset=5&filter=queued

Then just subtract the queued at time from the current time.

Thank you. Do you know how accurate the queued_at time is? I’m trying to determine how long my builds are queued before they are started so I am taking the difference between the start_at and queued_at times for a subset of my builds and the times don’t seem to match up to reality. According to my numbers my builds are never queued for more then 3s however I have witnessed builds sitting in the queue for much longer then that.

I do not, sorry. I would assume it is when the Web hook is recieved from GitHub.