My embedding batches obeyed the request limit and still failed
My ingestion code could pass a rate-limit check and still send more work than the check had accounted for.
The wrapper accepted a list of texts. It joined them into one string for the limiter, then sent the original list to the embedding client:
self.rate_limiter.wait_if_needed("\n".join(texts))
return self.embeddings.embed_documents(texts)
That is one approval for a list containing several documents. Counting how often my wrapper was called wasn’t enough to describe the work inside the call.
After tracking which files were ingested, I needed to account for how much work their chunks produced.
Requests and tokens need separate totals
The token-accounting change adds a timestamp and token count to each recorded request. The limiter can then look at recent request counts and recent token usage.
Those limits can disagree. A few large documents can stay below a request allowance and exceed a token allowance. Many short documents can do the opposite.

A parcel being weighed, photographed by Jeuwre, CC BY-SA 4.0; unmodified.
Here is a deliberately small teaching example. These aren’t a provider’s published limits: allow three documents and ten tokens in a window, and suppose the next documents contain four, four, and four tokens.
| Step | Documents counted | Tokens counted | Decision |
|---|---|---|---|
| Admit first document | 1 | 4 | Fits. |
| Admit second document | 2 | 8 | Fits. |
| Consider third document | 3 | 12 | Token allowance would be exceeded. |
The request allowance alone would admit all three. The token total changes the decision for the third.
Moving the check inside the batch
The November 28 fix processes the pending texts individually for rate accounting. A usage tracker records the approvals, and batch generation considers the remaining request and token budgets before yielding a batch to the embedding client.
The wrapper now collects results across those yielded batches. That makes the list of texts part of the limiter’s work instead of hiding it behind one combined string.
The token counter is a separate callable. That separation is useful for explaining and checking the arithmetic: I can supply known counts instead of asking a service to tokenize every teaching example.
There are still assumptions to check
My limiter’s accounting unit has to match the work charged by the provider and SDK. A Python method call, a document, and a network request aren’t automatically the same unit. The change makes the application’s accounting explicit; it doesn’t establish every provider’s behavior.
There are also edge cases around text that cannot fit in the configured token allowance. Waiting doesn’t shrink that input. It needs an input-size decision as well as a clock.
The useful checks use a controlled clock and known token counts: fill one allowance while leaving another available, move the clock across a window boundary, and check what the next approval does. A successful small batch won’t exercise those conditions.