A crash after case 263 re-ran 3, 13 or 63 model calls for chunks of 10, 50 or 200 and tagged as many cases twice without item keys. Checkpoints, keys and skip limits.
An overnight job that cannot simply start again
Jenya runs the help desk at Belgavi's Book Store. The team wants an agent to re-read every open case from the last quarter, about 5,000 of them, and tag the ones where a damaged delivery was never replaced. Each case needs one model call and one write to the help-desk system. The first version is a loop over the cases. It works until the worker is restarted at case 2,630, and then there are two bad choices: start from the beginning, paying for 2,630 model calls again and tagging those cases twice, or guess where it stopped.
This is an old problem with a well-tested answer. Batch frameworks such as Spring Batch and the Jakarta Batch specification (which grew out of JSR 352) process items in chunks: read a group of items, process each, write the group, and commit a checkpoint in the same transaction as the write. A restart resumes from the last committed checkpoint. The agent changes two things about that answer: each item is now expensive and slow, and the write often goes to a system that is not in your transaction. This guide adapts chunk processing to those two facts, with a Python example that injects a crash and counts what is redone.
The chunk contract
Spring Batch's reference documentation describes chunk-oriented processing as reading items one at a time, building a chunk, and writing the chunk within a transaction boundary, with the step's execution context saved at each commit so a restarted job can resume. The pieces map directly onto the agent job:
| Batch concept | Agent job | Where it is stored |
|---|---|---|
| Reader | the list of case ids, in a stable order | query with ORDER BY case_id |
| Processor | the agent step: read the case, decide a tag | model call |
| Writer | tag the case in the help-desk system | external API |
| Checkpoint | index of the next unprocessed case | job database |
| Skip log | cases the agent could not read | job database |
The stable order is not optional. A restart resumes by position, so the reader must return the same sequence each time. Snapshot the case ids into a job table when the job starts, rather than re-querying a live table that new cases are being added to.
The write that does not roll back
In the example, SQLite holds the checkpoint and the skip log, and both commit together once per chunk. If the worker dies halfway through a chunk, SQLite rolls that chunk back and the restart begins at the chunk's first item. Inside the database that is exactly right.
The tag, however, was written to the help-desk system as each item was processed, and that system does not roll back with SQLite. Every item between the last checkpoint and the crash was tagged, and the restart will tag it again. Measured on 500 synthetic cases with a crash after item 263:
| Chunk size | Model calls redone | Cases tagged twice, no item key | Cases tagged twice, with item key |
|---|---|---|---|
| 10 | 3 | 3 | 0 |
| 50 | 13 | 13 | 0 |
| 200 | 63 | 63 | 0 |
The redone model calls are the cost of the chunk size: on average half a chunk per crash. The duplicate tags are a correctness bug, and the chunk size only changes how many. The fix is an item key sent with every external write, built from the job run, the case and the action, which the receiving system uses to ignore a repeat.
key = f'{run_id}:{case["id"]}:{action}'
if desk.tag(case['id'], action, key):
stats['effects_written'] += 1
else:
stats['duplicate_effects_prevented'] += 1The receiving system must actually honour the key, and for long enough. Many APIs accept an Idempotency-Key header but keep it for a limited period. If your job can be paused for a day, a key remembered for one hour is not protection. If the external system has no such feature, keep your own record of completed effects in the job database, written before the external call, and check it on restart; then the window for a duplicate shrinks to a crash between the call and the record, which you should detect by reading back the current tag.
Choosing a chunk size for model calls
Classic batch jobs use chunks of hundreds or thousands because each item costs microseconds and each commit costs milliseconds. With an agent, each item costs seconds and real money, so the balance shifts towards small chunks. A chunk of 10 at a few seconds per call commits every half minute or so, and a crash costs at most 10 calls. The extra commits are noise against the model latency.
Two limits pull the other way. If the external writer supports batch writes, larger chunks send fewer requests. And if the model provider gives a discount or higher limits for batched requests, you may want to submit a chunk as one provider batch; then the chunk is also the unit of resubmission, and the item key protects the write side while the batch id protects the submission.
Poison items and the skip limit
Some cases cannot be processed: an empty body, an attachment in a format the agent cannot read, a case deleted after the job started. A batch job that stops on the first such item never finishes; one that skips everything silently hides a broken processor. Batch frameworks bound this with a skip limit: skip and log up to N failures of named exception types, then fail the job.
In the example, every 97th case has an empty body, so 500 cases contain five. They are written to the skip log with the reason, in the same transaction as the checkpoint, so a restart does not log them twice. With a skip limit of 3 the job stops at the fourth with 4 skipped items exceed the skip limit of 3. That stop is useful: in a real job, a sudden rise in skips usually means a model or prompt change broke parsing, and you want the job to halt rather than skip half the quarter.
Distinguish skippable errors from retryable ones. A rate-limit response or a timeout from the model should be retried with backoff inside the item, and must not count towards the skip limit. Only errors that will fail again on the same input belong in the skip log.
What a restart looks like
attempt 1: items 0..262 processed, crash after item 263; last checkpoint at 250 (chunk 50)
attempt 2: resumes at 250; items 250..262 re-run the model; their tags arrive with the same keys and are ignored
result: 513 model calls for 500 cases, 495 cases tagged once, 5 cases in the skip logThe job record should show the attempts, so the cost of restarts is visible. A job that needs four attempts every night has an infrastructure problem the checkpoint is hiding.
Migrating from the loop
- Snapshot the case ids for the run into a job table with the run id.
- Move the loop body into a processor function that takes one case and returns a decision, with no side effects.
- Put the external write behind a writer that sends the item key, and confirm with the help-desk system's owners how long keys are honoured.
- Add the checkpoint and skip log in one transaction per chunk, starting with chunks of 10 to 25.
- Test the restart path on purpose: kill the worker mid-chunk in a staging run and compare the tag counts with the case count.
When this is too much machinery
If each case is independent and the queue already gives you at-least-once delivery with per-message acknowledgement, put one message per case on a queue and let workers acknowledge after the write. The queue is your checkpoint, and you still need the item key for redelivery. Chunked batch processing earns its place when you need a run as a unit: a known population, a report at the end, a skip limit across the run, and the ability to say "the 2026-11 review is complete". If the job is "tag new cases as they arrive", that is a stream, not a batch.
Verifying the job
The companion tests check the properties that matter: a crash re-does only the uncommitted part of a chunk (3, 13 or 63 calls); with item keys no case is tagged twice and without them the same number is; two crashes still finish with 495 cases tagged once; poison items are skipped and logged up to the limit; and the checkpoint and skip log roll back together. Add the same tests around your real writer, with the help-desk system replaced by a fake that records keys, and run a crash test before every change to the processor.