Process N documents through the same multi-agent graph with resume-after-crash semantics. A Java/Spring AI cookbook recipe showing the pattern every batch LLM pipeline needs but most demos skip.
for each doc:
┌─────────┐ ┌─────┐
│ analyze │ ──▶ │ tag │ ← graph (per-doc)
└─────────┘ └─────┘
│ │
└──→ summary, tags written to state
│
└──→ progress.txt (last-processed index)
For each document the loop runs a small analyze → tag graph, then persists the index to disk. On restart the loop skips documents already done.
mvn -pl 05-batch-document-processor exec:javaThen simulate a crash:
# In one terminal:
mvn -pl 05-batch-document-processor exec:java
# After 3-4 documents: Ctrl-C
# Re-run — it resumes from where it stopped
mvn -pl 05-batch-document-processor exec:javaThe marker file lives at 05-batch-document-processor/target/batch-progress.txt. Delete it to re-run from scratch.
Almost every real LLM batch workload — embedding 100k product descriptions, summarising a year of support tickets, tagging an article archive — eventually crashes. Without resume semantics, you start over and re-pay for the LLM calls that already succeeded.
The pattern shown here is dead simple:
- Assign each unit of work a stable
runId— here"doc-" + index - Mark progress only after a unit succeeds — atomic file write
- Read the marker on startup to skip done work
That's it. No queue, no orchestrator, no Airflow.
| Scope | What it protects |
|---|---|
| Batch loop (this recipe) | Documents already fully processed are not re-processed after a crash |
Intra-graph (CheckpointStore) |
Pauses inside one document's graph survive crashes — e.g. an ApprovalGate waiting for a human |
For the intra-graph layer, attach a CheckpointStore to the AgentGraph and pass the same runId through RunOptions:
AgentGraph graph = AgentGraph.builder()
.addNode(...)
.checkpointStore(new InMemoryCheckpointStore()) // or JDBC / Redis
.build();
graph.invoke(AgentContext.of(doc), RunOptions.ofRunId("doc-" + i));See the Checkpointing docs for JdbcCheckpointStore and human-approval flows.
| Concept | Where in the code |
|---|---|
| Stable run ids | graph.invoke(ctx, RunOptions.ofRunId("doc-" + i)) — same id ⇒ same checkpoint key, idempotent re-runs |
| Atomic progress marker | writeResumeIndex() writes after every success; on restart readResumeIndex() reads it |
| Per-doc state isolation | Each AgentContext is fresh — no leakage between documents |
| Resume semantics from a flat file | No external state store needed; suitable for dev, testing, simple cron jobs |
| Concern | How to address |
|---|---|
| Concurrent workers | Replace the flat file with a row in processed_docs (doc_id, processed_at) and use SELECT … FOR UPDATE SKIP LOCKED |
| Partial-failure visibility | Attach a RunLogStore — query failed runIds for triage |
| Retry strategy | Add .retryPolicy(RetryPolicy.exponential(3, Duration.ofMillis(200))) to the graph — transient failures self-heal |
| Cost cap | Add a per-run BudgetPolicy — a single rogue document can't burn the whole batch's budget |