Skip to content

fix(platform): treat an embedding 429 as a rate limit, pace and retry it - #4071

Merged
larryro merged 2 commits into
mainfrom
fix/embedding-rate-limit
Oct 2, 2026
Merged

larryro merged 2 commits into
mainfrom
fix/embedding-rate-limit

Conversation

@larryro

@larryro larryro commented Oct 2, 2026

Copy link
Copy Markdown
Contributor

A 300-page crawl on Alibaba Cloud Model Studio ended on its first 429 — Allocated quota exceeded, please increase your quota limit — recorded as [credit], unretried, with the dialog telling the admin to check the account's balance. The account was fine: that 429 is Throttling.AllocationQuota, DashScope's tokens-per-minute limit, which its OpenAI-compatible mode sends under OpenAI's billing code insufficient_quota. CREDIT_REFUSAL_CODES held that code, so isCreditRefusal → credit → isRetryable false → the scan died. And nothing paced what three in-flight batches of up to 25 texts send per minute, so a fast crawl bursts past 1,000,000 TPM.

What changed

  • Classification (core/knowledge/embedding.ts): the bare insufficient_quota code no longer means the account; the wording does (OpenAI's "exceeded your current quota … plan and billing" still reads as credit). A 429 outside the billing wording is a new class, throttled, and is retryable. Carried through: the crawl reason [throttled] (scan_scheduling.ts), the dialog hint scanDetail.throttled (en/de/fr: the next scan continues on its own; pace requests under Data residency), the query path (EMBEDDING_UPSTREAM_ERROR with its own prose), document indexing (embedding_upstream, the job retries).
  • Lane-wide pause, minute-scale backoff: a batch refused with 429 pauses its whole lane (every slot, every waiting batch) and comes back after 10 s, 20 s, 40 s, 60 s, 60 s — six attempts, retry-after honoured as before — instead of 1 s / 2 s / 4 s for three. A search query keeps the short schedule and skips the pause: a refusal in seconds serves a chat turn better than a vector after a minute.
  • Pacing: two optional embedding.json keys, maxTokensPerMinute and maxRequestsPerMinute, kept like the other operator-only settings (schema, write schema, KNOWLEDGE_EMBEDDING_KEPT_KEYS, CLI pass-through). The lane meters the last minute (tokens estimated from characters, as the timeout already does) and holds a batch until enough has aged out; a single request larger than the limit goes when the minute is empty. The lane's memory outlives its queue, which a crawl drops between pages. maxConcurrentRequests is untouched.
  • Docs: data-residency (en/de/fr) with the two keys and DashScope's limits as the example; CLI install page key lists; REST reference class list; crawling guide Error row names the rate limit; manual automation row.

Proof

  • embedding.test.ts: the same insufficient_quota code told apart by its words; the billing refusal still unretried; a limited batch retried after 10 s with a second batch held by the lane meanwhile; a query answered at once during the pause; six attempts then give up; maxRequestsPerMinute holds the third request until the minute turns; maxTokensPerMinute holds the second batch and lets an oversized lone request through — 80 pass. crawl_action.vectors.test.ts: a throttled page ends the scan under [throttled].
  • Knowledge core + domain suites 780+ pass, shared schema test, scan-error.test.ts, locale parity, CLI config tests (51), tsc --noEmit, oxlint --type-aware, format:check, lint:links, lint:manual.
  • Not observed live against DashScope: the lane behaviour is proven with the fake clock. The dev stack's docs.tale.dev site is the natural check once the config carries maxTokensPerMinute: 1000000.

DashScope's tokens-per-minute limit (Throttling.AllocationQuota) reaches
the OpenAI-compatible client as code `insufficient_quota`, OpenAI's
billing code, so it read as a spent balance: unretried, the scan ended on
its first 429 and the dialog sent the admin to the account. The code no
longer decides; the wording does, and a 429 outside it is `throttled`.
A throttled batch pauses its whole lane and comes back on the minute
scale (10/20/40/60/60 s); a search query keeps the short schedule. Two
optional embedding.json keys, maxTokensPerMinute and
maxRequestsPerMinute, pace what a lane sends per minute so a crawl is
held under the provider's limit instead of refused.
Alibaba Cloud support confirmed that a model's RPM and TPM are enforced
per second as well, each divided by sixty: a minute's worth sent in one
second is refused while the minute is nearly empty, which is what three
in-flight batches of twenty-five chunks do. Under maxTokensPerMinute and
maxRequestsPerMinute the lane now also keeps every second under a
sixtieth of the limit, and sizes each request to fit that second, so no
single request can be the burst the per-second rule refuses.
@larryro

larryro commented Oct 2, 2026

Copy link
Copy Markdown
Contributor Author

Follow-up after Alibaba Cloud support confirmed the limits are enforced per second too (RPM ÷ 60, TPM ÷ 60): with the per-minute window alone, a docs.tale.dev scan at 6 pages/min uses ~4 % of 1,000,000 TPM and still got the 429, because three in-flight batches of 25 chunks put 50–75k estimated tokens into one second against a per-second share of 16,667.

So, under maxTokensPerMinute / maxRequestsPerMinute only (no change without them):

  • the lane keeps every one-second window under a sixtieth of each limit, on top of the minute window;
  • each request is sized to a second's share of tokens (splitBatches by estimated tokens, the provider's count cap still applying), so no single request can be the burst the per-second rule refuses; a text that alone exceeds it goes by itself.

Tests: three requests of 4 tokens under 600 TPM (10/s) — the third waits a second, not a minute; four texts under the same limit go as two requests a second apart; two requests a minute now go one second apart. embedding.test.ts 82 pass, knowledge suites 787 pass, tsc/oxlint/format green. Docs sentence added in en/de/fr.

@larryro
larryro merged commit 9b91fb6 into main Oct 2, 2026
@larryro
larryro deleted the fix/embedding-rate-limit branch October 2, 2026 07:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant