fix(embedding): split batches that exceed a provider's per-request token quota

Merged
LE
15d ago
+389-21

OpenRouter sends `qwen/qwen3-embedding-8b` to Nebius, which rejects any single request above roughly 50–65k tokens with HTTP 429 "You exceeded your tokens quota … Retry the request after 2 sec" and no `Retry-After` header. Retrying the same request never succeeds, so Vera spent 8 attempts × 3 requeues on each large batch and then failed the whole index. Since chunks are sorted by length, the last batches always hit this on repositories with large functions.

  • A 429 that names a token quota (Nebius `exceeded your tokens quota`, OpenAI `tokens per min`) on a multi-input request is returned at once, and `embed_batch_resilient` halves the batch, the same way it handles context-size errors. Single inputs keep the normal rate-limit retries and requeues.
  • When the header is missing, the retry delay is read from the body (`retry the request after N sec`, `try again in 1.5s` / `20ms`), still capped at 60 s.

Live check against OpenRouter, a scratch repo of 60 large Python functions (180 chunks), batch size 100:

  • v2.4.0: failed after 493 s with 41 token-quota 429s.
  • this branch: indexed in 112 s, 46 requests, 0 retries, 0 failed batches.

Devin Review


Summary by cubic

Fixes OpenRouter embedding batches that exceed a provider's per-request token quota, which previously caused repeated 429 retries and failed whole index builds.

  • Multi-input 429s naming an explicit token quota (exceeded your tokens quota) are returned at once and the batch is halved, like context-size errors. TPM limits and single inputs keep normal rate-limit retries.
  • When no Retry-After header is present, the retry delay is parsed from the error body (retry the request after N sec, try again in 1.5s / 20ms), capped at 60 s.
  • vera serve embeddings now use the resilient batcher, so API calls respect the provider's max batch size and split token-quota batches; single texts over the provider context limit now fail instead of being truncated. Providers that truncate input themselves, like local models, still do.
  • Adds v2.4.1 release notes covering the retry and batch-splitting changes.

Summary by CodeRabbit

  • Bug Fixes

    • Embedding requests now use delays reported in provider error messages when a valid Retry-After header is unavailable.
    • Batches exceeding a provider’s token quota or context limit are split into smaller requests. Single-input requests retain their retry behavior, and oversized individual inputs are not truncated during batch splitting.
    • Embedding results remain in the same order as the submitted inputs.
  • New Features

    • Added a resilient option for embedding raw text inputs without prefixes or truncation.
  • Documentation

    • Updated release notes with embedding retry and batch-splitting details.