Topic 4 of 4 · 5 min read
Submitting, polling and collecting batch results
Goal: Design a pipeline that resubmits only failed or expired items of a batch and never processes a result twice.
A good batch pipeline treats every result as an event that may arrive late, twice, or not at all: it keys everything by custom_id, stores each result once, and resubmits only the items that failed or expired. Rerunning the whole batch is the expensive mistake to avoid.
Check yourself · 1 of 3
A batch ends with 39,000 successes and 1,000 failures. What is the cheapest correct next step?
Why it matters
At 40,000 requests, some will fail. A request can be invalid, hit a provider error, or expire when the batch does not finish within 24 hours. Claude reports a result per request, with a type such as succeeded, errored, canceled or expired. If your code assumes all-or-nothing, a handful of failures costs you the entire run.
How to build it
- Record before you submit. Write each
custom_idand its input to your database with statuspendingand the batch id. This is your source of truth. - Poll on a schedule. Store the batch id, check status with a delay, and stop at ended or expired.
- Process each result idempotently. Use
custom_idas a unique key. Insert the result with "if not exists" semantics, so reading the same results twice, after a crash or a retry, changes nothing. - Sort the outcomes. Succeeded: store and mark
done. Errored: read the error. Invalid request means fix the input; a provider error is worth a retry. Expired or canceled: retry as is. - Resubmit the remainder. Build a new batch from the items still
pendingorretry, with their originalcustom_idvalues. Cap the number of attempts, then park the item for a human.
Before and after
Weak: after a batch ends with 38,900 successes and 1,100 errors, the script saves the successes, logs "some failed", and the engineer reruns all 40,000 requests next night. Cost doubles and the successes may overwrite reviewed data.
for r in results:
save(r.custom_id, r.result) # overwrites on every rerun
Better: save with a guard, and rebuild the next batch from the unfinished rows.
for r in results:
if r.result.type == "succeeded":
insert_if_absent(r.custom_id, r.result.message)
retry_ids = db.ids_where(status__in=["pending", "retry"], attempts__lt=3)
What goes wrong
- Results fetched after the retention window. Download and store results soon after the batch ends; providers delete them eventually, so check the retention period in the docs.
- Retrying invalid requests forever. An input that is too long fails every time. Classify errors before you retry.
- Reusing ids across batches by accident. If two batches both use
ticket-1, your insert guard silently drops the newer answer. Include a job or version prefix when the same input is reprocessed on purpose.
Examples
Status table
CREATE TABLE batch_items (
custom_id TEXT PRIMARY KEY,
batch_id TEXT,
status TEXT, -- pending, done, retry, failed
attempts INT DEFAULT 0,
result JSONB
);
The primary key on custom_id is the idempotency guard: a second insert of the same id is rejected.
A 40,000-item run
Batch ends with 38,900 succeeded, 700 errored for invalid input, 400 provider errors and 0 expired. Store the successes. Send the 700 to an input fix list. Resubmit the 400 as a new batch of 400, not 40,000, with attempts incremented.
Key points
- Record every custom id and input as pending before you submit.
- Handle each result by custom id with an insert-if-absent guard, so reading results twice is harmless.
- Sort outcomes: store successes, fix invalid inputs, retry provider errors and expired items.
- Resubmit only unfinished items, with a cap on attempts, and send the rest to a human.
- Download and store results promptly, since providers keep them only for a limited time.
Common mistakes
- Rerunning the whole batch after partial failure and paying for the finished items again.
- Overwriting stored results on every read instead of inserting only if absent.
- Retrying invalid-input errors forever, since they fail the same way each time.
- Not saving results soon after the batch ends, then finding them gone.
Check yourself · 2 of 3
Your result-collection script crashes halfway and is run again on the same batch. How do you avoid saving results twice?
Check yourself · 3 of 3
A request errored because its prompt exceeded the model's context limit. What should the pipeline do?
Practice items, not counted towards your mastery.
Start here
- Batch processing (Claude) (opens in a new tab)DocsFree
Request format, custom_id, the 24-hour window, size limits, result handling and the batch discount.
- Batch API (OpenAI) (opens in a new tab)DocsFree
The same pattern with a JSONL input file, a status object and separate output and error files.
- Batch API (Gemini) (opens in a new tab)DocsFree
Gemini's asynchronous batch mode for large jobs, with its own submission and result steps.