# API budget

> How BareCI keeps many agents on one repository within GitHub's API limits by answering from webhooks and budgeting its own calls per installation.

Source: https://bareci.com/docs/modules/api-budget

Many agents on one repository exhaust GitHub's API limits, mostly by polling CI, reviews and the merge queue. BareCI already receives every webhook, so it answers those questions itself, and every call it makes to GitHub goes through one budget per installation.

## Wait, don't poll

[Wait on a pull request](https://bareci.com/docs/agents/wait) holds one request on BareCI until its checks finish, it is ready, enters the merge queue, merges, closes or changes. `bareci wait`, `bareci ready --wait` and the MCP tool `change_get` with `until` all use it. A waiting agent costs GitHub nothing.

## Reads from BareCI's copy

The PR status (state, head, base, labels, mergeability, checks, the Gate's verdict, the merge queue), `ready` and `findings` come from BareCI's state for the pull request, kept current by webhooks. Every answer carries a `version` that increases only when something changed.

## BareCI's own calls, budgeted

Every GitHub call the Worker makes for an installation goes through that installation's budget:

- **Rate limits.** It reads `x-ratelimit-*` on every answer and keeps each resource's remaining calls and reset time.
- **Priorities.** The Gate's check runs and merge-queue calls may use the whole limit. Ordinary calls leave 10% for them, and reconciliation leaves 30%. A call that would cut into a reserve is refused with 429 and the seconds until the reset. A Gate call waits up to 10 seconds for the budget instead.
- **Secondary limits.** A secondary limit or `Retry-After` stops every call for the installation until it passes (60 seconds when GitHub gives no time).
- **ETags.** GET answers are kept with their ETag, and the next GET sends `If-None-Match`. GitHub answers an unchanged resource with 304, which does not count against the limit.
- **Coalescing.** Identical reads in flight at the same time share one call.
- **GraphQL batching.** Pull request reads arriving within 25 ms go out as one GraphQL query, up to 25 at a time.
- **Backoff.** Reads are retried twice on 5xx and network errors, with exponential backoff from 200 ms. Writes are never retried, so nothing happens twice.

An operator can read an installation's budget (each resource's limits, any block, and counters of calls, 304s, coalesced, batched, refused, waited and retried calls) at `GET /v1/admin/github-budget/{installation}`.

## Less CI churn

Runs for a superseded head are cancelled, only affected tests run, and the Gate runs before a pull request enters the merge queue, so queue runs rarely fail and retry.
