Once text is chunked, each chunk has to become a numerical embedding, and calling the embedding API once per chunk inside a tight loop adds a network round trip to every row and runs into provider rate limits quickly. Processing chunks in batches is the fix: a starting range of 100 to 500 inputs per request works with APIs such as OpenAI text-embedding-3-small, or with a local model through sentence-transformers.
What you need
- Chunked documents with a flag for chunks that have no embedding yet
- An embedding API key or a local embedding model
- A note of the provider batch and rate limits
Step 1: Collect the chunks that still need embeddings
Query for chunks with no embedding stored, in order, and take the next batch. Do not re-embed chunks that already have a vector.
Step 2: Send one batch per request
Send 100 to 500 inputs in a single request and store each returned vector against its chunk id. Check the provider batch and rate limits and tune from there.
Step 3: Check the counts match and retry only failures
Count the stored vectors against the chunks sent. Retry only the failed batch, not the whole run, and log which chunks failed.
Worked example: synthetic batch embedding run
The table below is a synthetic example. It shows the expected result of a batch run; no API was called.
| Batch | Chunks sent | Vectors stored |
|---|---|---|
| Batch 1 | 100 | 100 |
| Batch 2 | 100 | 98 |
| Batch 2 retry | 2 | 2 |
| Total | 200 | 200 |
Expected result (UNTESTED). 200 chunks are embedded in 2 batch requests plus a 2-chunk retry, with no chunk embedded twice. This check was not run live; the table shows the expected result for the synthetic example.
Limits
- Batching cuts HTTP calls, but a failed batch still needs a retry; log failures per batch.
- A local model through sentence-transformers avoids API rate limits but needs local compute.
- Always cache embeddings; recomputing them on every request wastes money and adds latency.
- Batch size is a starting range; check the provider limits for your model before raising it.
