Founding churches get 50% off for 12 monthsFounding churches: 50% offApply as a founding churchApply

Rate limits and spend caps

Two kinds of limit protect a church: how fast you may send requests, and how much AI work a key may start. They fail differently, so handle them differently.

Request limits#

Each key has a requests-per-minute limit: 60 unless the church sets another, and up to 3,000. The church as a whole also has a ceiling across all its keys, so one busy integration cannot starve the others. When either is exceeded the answer is 429:

CodeWhose limitWhat to do
rate_limitedThis keyWait Retry-After seconds, then retry.
org_rate_limitedThe church, across all keysWait Retry-After seconds. Spread work out, or ask the church to review how its keys are used.

Rate limit headers#

Every authenticated response carries your allowance, for whichever of the key limit and the church ceiling is tighter:

HeaderMeaning
RateLimit-LimitRequests allowed per minute.
RateLimit-RemainingRequests left right now.
RateLimit-ResetSeconds until the full allowance is available again. The allowance refills continuously, so it is not a fixed window.
Retry-AfterOn a 429 only. Seconds until the next request would be accepted.
HTTP/1.1 429 Too Many Requests
Ratelimit-Limit: 3
Ratelimit-Remaining: 0
Ratelimit-Reset: 60
Retry-After: 20
Content-Type: application/json

{"status":false,"message":"This API key is sending requests too quickly. Retry in 20 seconds.","code":"rate_limited"}

Headers are case-insensitive. Your HTTP library may show them as RateLimit-Limit or ratelimit-limit.

Spend caps and limits#

Some work costs money: transcribing a recording, drafting a note, answering a question. A church controls that spend with caps, and they work differently from request limits:

CodeHTTPMeaning
key_daily_cap_reached429This key spent its daily cap.
key_monthly_cap_reached429This key spent its monthly cap.
org_api_cap_reached429The church spent its monthly API cap, across all keys.
monthly_teaching_limit_reached429This key submitted its monthly limit of teachings.
plan_hours_exhausted403The church used the hours included in its plan.
  • Caps only stop requests that would start more work: adding a teaching, an upload, a note or a question. Reading is never blocked by a cap.
  • They do not clear in seconds. Do not retry them in a loop: tell a person, or queue the work for later.
  • GET /usage shows a key’s own caps and its spend so far, in millionths of a US dollar (1000000 is one dollar).

Retrying correctly#

Retry rate limits and server errors. Do not retry anything else. Wait as long as Retry-After says when it is there, and back off exponentially with a little random jitter when it is not.

async function withRetry(call, { attempts = 5 } = {}) {
  for (let attempt = 1; ; attempt++) {
    try {
      return await call();
    } catch (error) {
      const retryable = error.status === 429 && (error.code === "rate_limited" || error.code === "org_rate_limited");
      const transient = error.status >= 500 || error.status === undefined;
      if (attempt >= attempts || !(retryable || transient)) throw error;
      const wait = retryable && error.retryAfter > 0 ? error.retryAfter * 1000 : Math.min(30_000, 500 * 2 ** attempt);
      await new Promise((resolve) => setTimeout(resolve, wait + Math.random() * 250));
    }
  }
}

const teachings = await withRetry(() => lectern("/teachings"));
console.log(teachings.meta.total);
Pair retries with idempotency

When you retry a request that starts work, send the same Idempotency-Key on every attempt. See Idempotency.

Staying under the limit#

  • Prefer webhooks to polling. One event replaces dozens of requests.
  • When you must poll, wait 10 seconds or more between checks of the same teaching, and stop when the status is final.
  • Read RateLimit-Remaining and slow down before it reaches zero.
  • Use one client with a small number of workers per key rather than many processes that each assume they own the allowance.
Uploads have their own limits

Starting uploads and asking for part URLs have limits of their own, on top of your request allowance. They answer 429 too, and the retry helper above handles them. Sending the file bytes to storage does not count toward any Lectern limit.

Next steps