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:
| Code | Whose limit | What to do |
|---|---|---|
rate_limited | This key | Wait Retry-After seconds, then retry. |
org_rate_limited | The church, across all keys | Wait 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:
| Header | Meaning |
|---|---|
RateLimit-Limit | Requests allowed per minute. |
RateLimit-Remaining | Requests left right now. |
RateLimit-Reset | Seconds until the full allowance is available again. The allowance refills continuously, so it is not a fixed window. |
Retry-After | On 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:
| Code | HTTP | Meaning |
|---|---|---|
key_daily_cap_reached | 429 | This key spent its daily cap. |
key_monthly_cap_reached | 429 | This key spent its monthly cap. |
org_api_cap_reached | 429 | The church spent its monthly API cap, across all keys. |
monthly_teaching_limit_reached | 429 | This key submitted its monthly limit of teachings. |
plan_hours_exhausted | 403 | The 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 /usageshows a key’s own caps and its spend so far, in millionths of a US dollar (1000000is 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);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-Remainingand 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.
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.