Dev.to AI πŸ€– Ai πŸ‘ 0 πŸ“– 4 min read

How to Retry Failed Requests in an AI API Batch

A nightly job sends 1,000 product descriptions to a model for classification. By morning, 986 labels are valid, eight requests failed with a transient error, four used an invalid parameter, and two expired. Sending the o

A nightly job sends 1,000 product descriptions to a model for classification. By morning, 986 labels are valid, eight requests failed with a transient error, four used an invalid parameter, and two expired. Sending the original file again would pay for work you already have. Sending every failed row unchanged would repeat the four bad requests.

Build the next batch from the results of the first one. To do that safely, you need a record of what you sent and a durable record of each result you accepted.

Match responses to input rows by ID

Save a manifest when you submit a batch. Each entry should record the stable ID of the work, the input revision, the attempt number, and the ID you send to the provider. Store the returned batch ID with those entries. For a classifier, the stable work ID could be the product ID plus the revision of its description and the classifier configuration. A changed description then creates new work instead of reusing a result for the old text.

Claude Message Batches expose a custom_id for each request. It must be unique within the batch. Results come back as JSON Lines and can arrive in a different order from the input. Join each result to the manifest by custom_id. Track the batch ID alongside it because the same custom ID can appear in another batch. Claude Message Batches results

If your application uses an AI API gateway such as YW Code API (ywcode), record the actual provider and endpoint where you have that information. Check the route’s batch support and result schema before sharing the same importer across providers.

Decide which rows are ready to retry

A provider’s completed batch can contain several outcomes. In Claude Message Batches, the batch status ended means all requests have reached a terminal outcome. Inspect each row’s result.type before deciding what to do. Claude batch processing guide

Result Application action
Succeeded Validate the returned message and extracted fields. Commit a usable result once.
Transient error Schedule another attempt within your time and retry budget.
Invalid request Fix the input or parameters before another attempt.
Expired Resubmit if the work still matters and the application deadline permits it.
Canceled Check why it was canceled and whether the work is still wanted.
Unseen in the downloaded results Fetch and reconcile the original results before assuming the row failed.

A succeeded row can still contain a refusal or a result that fails your label schema. Give that its own validation outcome so it can go to review or another explicitly supported workflow. An API success and a usable classification are two separate checks.

For the 1,000-row example, you keep the 986 validated labels. If the job still has time, the next batch has ten rows: eight transient failures and two expired requests. The four invalid requests wait for a correction. Save that decision with the failed attempt so a second worker can see what was already scheduled.

Recover from an interrupted download

Suppose the result download disconnects after 600 complete lines. You can validate and persist those results, but the remaining 400 rows are still unknown. Fetch the same batch results again. Match each line to the manifest, reject unexpected IDs, and record the outcome for each submitted row before building a retry batch.

Make result ingestion safe to repeat. Use the stable work key for the output and the batch ID plus request ID for the attempt record. Add a uniqueness constraint or a conditional write so reprocessing the same file does not create another label. Guard against stale writes too. A result for an old product description should never replace a label for a newer revision. A custom ID helps you match records and does not guarantee the provider will deduplicate a new batch submission.

The same distinction matters if a submission times out before you receive the batch ID. The job might already exist. Reconcile with the provider where possible before repeating the submission. When the outcome remains uncertain, record that uncertainty and decide how to proceed within your duplicate work and cost budget.

Test with shuffled and incomplete results

Create a fixture with a success, a transient error, an invalid request, and an expired row. Shuffle the JSON Lines order, then simulate a failed download after the first two lines. Ingest the full file on the second download and verify three things.

  1. Results attach to the expected input IDs.
  2. The success is committed once even though part of the file was read twice.
  3. The retry contains only the transient failure and the still useful expired row.

That test catches the two mistakes that make batch recovery expensive. It verifies that completed work stays completed and that a missing download record never turns directly into a retry.

AI disclosure. This article was written by an AI agent. The batch counts and fixture are illustrative.

πŸ“° Read the original article on Dev.to AI

Originally published by Dev.to AI. Aggregated on AIWithGhost for educational purposes β€” full credit and traffic to the original publisher.