Sub topic 3
Resubmitting Only What Failed
Assume you know basic Python, JSON Lines, LLM API calls, and rate limits. A batch job can partially succeed, so retrying the entire input wastes time and money.
Find failed and missing requests
Read both the output file and the error file. Treat a request as successful only when its response has the expected status and content. Use custom_id to identify requests. Never use output line numbers because providers may return records in a different order.
import json
successful = set()
failed = set()
for line in output_text.splitlines():
record = json.loads(line)
if record.get("response", {}).get("status_code") == 200:
successful.add(record["custom_id"])
else:
failed.add(record["custom_id"])
for line in error_text.splitlines():
record = json.loads(line)
failed.add(record["custom_id"])
missing = set(all_inputs) - successful - failed
retry_ids = failed | missingRetry only transient failures and missing records. Fix permanent errors, such as invalid request formats, before resubmitting. Store the original input and error details so that each retry has an audit trail.
Test a small pilot batch
Before submitting thousands of requests, submit a representative sample. Include short and long inputs, unusual characters, expected edge cases, and requests using every endpoint or model option.
Check that:
- Every
custom_idis unique and traceable. - Successful responses have the expected schema.
- Errors appear in the files you expect.
- Token usage and cost match your estimate.
- The completion window fits your operational needs.
Respect limits and split large workloads
Providers limit input-file size, request count, tokens, concurrency, or total batch capacity. Read the current provider limits before production use. Split work into chunks below the smallest applicable limit, leaving safety margin for estimation errors. Record the chunk number and its request IDs.
Compare provider batch APIs
Batch APIs differ in endpoint formats, supported models, completion windows, pricing, file limits, cancellation behavior, and output retention. Create an adapter around provider-specific submission and parsing code. Keep your internal record format consistent:
| Internal field | Purpose |
|---|---|
| `custom_id` | Correlates input, output, and retry |
| `status` | Tracks pending, succeeded, failed, or retryable |
| `attempt` | Limits repeated retries |
Design a restartable pipeline
Persist state after every durable step: input creation, upload, submission, polling, collection, and result validation. A restart should inspect stored state and continue from the last completed step rather than submit duplicate work.
Make operations idempotent, meaning repeating them produces the same final result. Derive custom_id from a stable document identifier and task version. Before submitting a retry, check whether that identifier already has a valid result. Use bounded retries and a dead-letter list for records requiring manual review.
Exercise
Why should a retry set include missing IDs as well as explicit errors?
Show answer
A missing record has no confirmed result. It may have been lost during processing or collection, so it must be investigated or safely resubmitted.