Resumable AI Jobs

Ma Mahalakshmi V Updated 19 Sep 2026
8 min read ·Lesson 143 of 224

Designing Resumable AI Processing Jobs

Every previous lesson in this unit has assumed a job runs from start to finish without interruption. Real production jobs don't get that guarantee: a server restarts for a deployment, a process crashes on an unhandled exception, an operator needs to stop a job mid-run to fix a bug in the prompt template. A job that processes 100,000 records over six hours and has no way to resume after an interruption other than starting item one again is not production-ready, regardless of how well it handles concurrency, rate limits, and failures while it's running. This lesson covers how to design a job so that "restart" means "continue from where it left off," not "begin again."

What Makes a Job Resumable

A resumable job has three properties working together:

  1. Durable state. Every record's current status is stored somewhere that survives the process ending — a database or a file on disk, never only in memory.
  2. Idempotent restart logic. On startup, the job checks that durable state and skips anything already completed, using the idempotency techniques from Lesson 8 to make sure "checking status" and "acting on it" don't themselves race or duplicate work.
  3. Frequent, atomic checkpoints. Status updates are written durably often enough that a crash loses at most a small, bounded amount of work — not the entire job's progress up to that point.

None of these are exotic techniques individually. The discipline is making sure all three are actually in place before a job runs at production scale, rather than discovering their absence during an outage.

Checkpointing with a Durable Store

The simplest reliable checkpoint store for a moderate-scale job (tens of thousands to low millions of records) is a SQLite database file — it requires no separate server process, supports atomic transactions out of the box, and survives a process crash because the data is written to disk, not held in memory.

import sqlite3
from contextlib import contextmanager


def init_checkpoint_db(path: str) -> sqlite3.Connection:
    conn = sqlite3.connect(path)
    conn.execute(
        """
        CREATE TABLE IF NOT EXISTS job_records (
            record_id TEXT PRIMARY KEY,
            status TEXT NOT NULL,
            output TEXT,
            error TEXT,
            attempts INTEGER DEFAULT 0,
            updated_at TEXT DEFAULT CURRENT_TIMESTAMP
        )
        """
    )
    conn.commit()
    return conn


def upsert_checkpoint(conn: sqlite3.Connection, record_id: str, status: str,
                       output: str = None, error: str = None) -> None:
    conn.execute(
        """
        INSERT INTO job_records (record_id, status, output, error, attempts)
        VALUES (?, ?, ?, ?, 1)
        ON CONFLICT(record_id) DO UPDATE SET
            status = excluded.status,
            output = excluded.output,
            error = excluded.error,
            attempts = job_records.attempts + 1,
            updated_at = CURRENT_TIMESTAMP
        """,
        (record_id, status, output, error),
    )
    conn.commit()

This is the same upsert pattern from Lesson 8, applied here to the job's own progress tracking rather than to a business-level side effect — record_id is the UNIQUE/PRIMARY KEY column, so calling upsert_checkpoint any number of times for the same record always results in exactly one row reflecting its latest known state. Calling conn.commit() after every single update, rather than batching commits, is a deliberate tradeoff: it's slower than batching, but it guarantees that a crash immediately after a commit leaves the database reflecting a fully consistent, durable state — there is no window where several updates are sitting uncommitted in memory and would be lost together. For very high-throughput jobs where per-item commits become a bottleneck, batching commits every N items or every few seconds is a reasonable optimization, but it explicitly trades a small, bounded amount of possible rework (re-processing whatever was uncommitted at crash time) for higher throughput — a tradeoff worth making consciously, not by accident.

Determining What Still Needs to Run

The core of resumability is a query, run once at job startup, that partitions the full set of records into "already done" and "still needs work":

def load_incomplete_records(conn: sqlite3.Connection, all_record_ids: list[str]) -> list[str]:
    """Return the subset of record_ids that are not yet successfully completed."""
    placeholders = ",".join("?" for _ in all_record_ids)
    done_rows = conn.execute(
        f"""
        SELECT record_id FROM job_records
        WHERE record_id IN ({placeholders}) AND status = 'succeeded'
        """,
        all_record_ids,
    ).fetchall()
    done_ids = {row[0] for row in done_rows}
    return [rid for rid in all_record_ids if rid not in done_ids]

Notice this checks specifically for status = 'succeeded' — a record sitting at in_progress (from a run that crashed mid-item) or failed (from a previous attempt) is correctly treated as still needing work. This is an important subtlety: a naive resumability check that only asks "does a row exist for this record_id?" would incorrectly skip records that were started but never finished, silently leaving gaps in the output. Resumability depends on the status value, not merely on the presence of a row.

Handling the "In Progress" Ambiguity

There is one genuinely tricky case: a record whose last known status is in_progress when the job restarts. This means the previous run was in the middle of processing that record — calling the model, or writing its result — when the crash happened, and you cannot know from the checkpoint alone whether that in-flight work actually completed on the server side before the crash.

The safe default is to treat in_progress records as not-yet-done and retry them, relying on the idempotency work from Lesson 8 (upserts, idempotency keys) to make that retry harmless even in the rare case where the original attempt had actually succeeded moments before the crash:

def reset_stuck_in_progress_records(conn: sqlite3.Connection) -> int:
    """On startup, any record still marked in_progress belongs to a
    run that never finished cleanly. Reset it to pending so it gets retried."""
    cursor = conn.execute(
        "UPDATE job_records SET status = 'pending' WHERE status = 'in_progress'"
    )
    conn.commit()
    return cursor.rowcount

Running this once at the start of every job launch — before computing load_incomplete_records — ensures no record is permanently stuck in a state that neither the "done" filter nor a human notices. This is a direct, practical consequence of the idempotency design from Lesson 8: because retrying a record whose downstream write already succeeded is safe (an upsert just overwrites the same value), resetting all in_progress records to pending on every restart is the correct, low-risk default rather than something to be avoided.

A Job State Machine

Putting the statuses together, a record's lifecycle across a resumable job follows an explicit state machine, which is worth writing down rather than leaving implicit in scattered string literals:

pending → in_progress → succeeded
                      ↘ failed → pending (on retry) → in_progress → ...
from enum import Enum


class JobRecordStatus(str, Enum):
    PENDING = "pending"
    IN_PROGRESS = "in_progress"
    SUCCEEDED = "succeeded"
    FAILED = "failed"


VALID_TRANSITIONS = {
    JobRecordStatus.PENDING: {JobRecordStatus.IN_PROGRESS},
    JobRecordStatus.IN_PROGRESS: {JobRecordStatus.SUCCEEDED, JobRecordStatus.FAILED},
    JobRecordStatus.FAILED: {JobRecordStatus.PENDING},   # retry re-queues it
    JobRecordStatus.SUCCEEDED: set(),                     # terminal state
}


def transition(current: JobRecordStatus, target: JobRecordStatus) -> JobRecordStatus:
    if target not in VALID_TRANSITIONS[current]:
        raise ValueError(f"invalid transition: {current} -> {target}")
    return target

Encoding valid transitions explicitly, and raising on an invalid one, catches an entire class of bugs at the moment they'd occur — for example, code that accidentally tries to mark a record in_progress a second time without it ever having been reset to pending, which would indicate a bug in the pipeline's control flow rather than a legitimate state change.

Putting It Together: A Resumable Run Function

async def run_resumable_job(db_path: str, all_records: list, concurrency: int = 10):
    conn = init_checkpoint_db(db_path)
    reset_count = reset_stuck_in_progress_records(conn)
    if reset_count:
        print(f"reset {reset_count} stuck in_progress record(s) from a previous run")

    all_ids = [r.record_id for r in all_records]
    incomplete_ids = load_incomplete_records(conn, all_ids)
    incomplete_records = [r for r in all_records if r.record_id in set(incomplete_ids)]

    print(f"{len(all_records) - len(incomplete_records)} already done, "
          f"{len(incomplete_records)} remaining")

    client = AsyncOpenAI()
    queue: asyncio.Queue = asyncio.Queue()
    for r in incomplete_records:
        queue.put_nowait(r)

    async def worker(queue):
        while True:
            record = await queue.get()
            if record is None:
                queue.task_done()
                break
            upsert_checkpoint(conn, record.record_id, "in_progress")
            try:
                response = await client.responses.create(
                    model="gpt-5.6-terra", input=record.prompt
                )
                upsert_checkpoint(conn, record.record_id, "succeeded", output=response.output_text)
            except Exception as exc:
                upsert_checkpoint(conn, record.record_id, "failed", error=str(exc))
            queue.task_done()

    workers = [asyncio.create_task(worker(queue)) for _ in range(concurrency)]
    await queue.join()
    for _ in workers:
        queue.put_nowait(None)
    await asyncio.gather(*workers)
    conn.close()

If this function is interrupted at any point — the process is killed, the machine restarts — calling it again with the same db_path and the same all_records list picks up exactly where it left off: records already checkpointed as succeeded are excluded from incomplete_records before a single new request is made, and anything left in_progress from the interrupted run is reset and retried safely.

Testing Resumability Logic

The state-machine and record-selection logic can be tested entirely with an in-memory database and no network calls:

def test_resumable_job_skips_completed_and_retries_stuck_records():
    conn = init_checkpoint_db(":memory:")
    upsert_checkpoint(conn, "1", "succeeded", output="done")
    upsert_checkpoint(conn, "2", "in_progress")
    upsert_checkpoint(conn, "3", "failed", error="timeout")

    reset_count = reset_stuck_in_progress_records(conn)
    assert reset_count == 1

    incomplete = load_incomplete_records(conn, ["1", "2", "3", "4"])
    assert set(incomplete) == {"2", "3", "4"}  # "1" is done; others need work
    print("PASS: resumable job correctly identifies remaining work after a restart")


test_resumable_job_skips_completed_and_retries_stuck_records()

Common Mistakes

  • Keeping job state only in memory (a Python list or dict) with no durable checkpoint. This makes every crash equivalent to losing all progress, regardless of how far the job had gotten, and defeats the entire purpose of building resumability.
  • Treating "a row exists for this record" as equivalent to "this record is done." A row can exist with status pending, in_progress, or failed — only succeeded means the work doesn't need to happen again.
  • Not handling the in_progress state at restart at all, leaving records permanently stuck in a status that the "still needs work" query doesn't select and that never gets human attention either.

Best Practices

  • Persist status changes as they happen, not in a final batch at the end. A checkpoint written only when the whole job finishes provides no protection against the exact failure mode — a mid-job crash — that resumability exists to handle.
  • Reset in_progress records to pending at every job startup, and rely on idempotent operations (Lesson 8) to make the resulting retry safe even in the rare case where the original attempt had actually completed.
  • Define the record status state machine explicitly, including which transitions are valid, so that bugs in the pipeline's control flow surface as an immediate error rather than as silently inconsistent checkpoint data discovered much later.

0 Comments

Reviewed before they appear

No comments yet.

OpenAI SDK
Introduction to the OpenAI SDK Setting Up Python Creating an API Key Your First Call — client.responses.create() and response.output_text Understanding Billing, Credits, and What a Request Costs Why Responses Replaced Chat Completions Anatomy of a Request: model, input, and instructions Anatomy of a Response: The Typed output Array, Not Just Text Roles: User, Assistant, and Developer/System Choosing a Model, and Reading the Models Page Instead of Memorizing Names Instructions vs. Input Writing Prompts That Get Consistent Results Few-Shot Examples Reasoning Models and the reasoning Parameter Debugging a Prompt That Misbehaves Why Streaming Matters for User Experience stream=True and Iterating Over Events Handling the Event Types You Actually Care About Background Mode for Long-Running Jobs Project — Add Live Streaming to Your Chatbot The Problem With Parsing Free Text JSON Schema and Strict Mode Pydantic Models With the SDK's Parse Helpers Handling Refusals and Validation Failures Project — A Resume-to-JSON Extractor Working With input_image input_file, PDFs, and the Files API Image Generation Speech-to-Text and Text-to-Speech Project: A PDF Question-Answering Script What Function Calling Is Defining a Tool Schema The Full Loop Multiple Tools Errors, Timeouts, and Untrusted Arguments Project: A Weather Assistant Web Search File Search and Vector Stores Code Interpreter Remote MCP Servers and Connectors Project: A Research Assistant What an Embedding Is, Without the Maths Generating and Storing Embeddings Similarity Search From Scratch Hosted Vector Stores vs. Rolling Your Own A Small RAG App Over a Folder of Notes Agents vs. a Single API Call — When You Need One pip install openai Giving Agents Tools Handoffs and Multi-Agent Triage Guardrails and Approvals Tracing and Observing What Your Agent Did A Multi-Agent Support Desk Error Codes and What Each One Means Retries, Timeouts, and Backoff Rate Limits and Spend Limits Prompt Caching and Cost Optimisation The Batch API for Bulk Work Async Clients and Concurrency Moderation and Safety Best Practices Designing the App Backend With FastAPI Streaming to a Simple Frontend Deploying and a Cost/Safety Checklist Why Web Search Is Useful for Current Information Using the Web Search Tool with the Responses API Configuring Search Behavior for Application Use Cases Understanding Citations and Source Attribution Where to Go Next Building a Research Assistant with Web Search Combining Web Search with Structured Outputs Handling Conflicting or Low-Quality Web Sources Reducing Unsupported Claims with Grounded Generation Testing Freshness-Sensitive AI Answers Production Considerations for Web-Grounded Applications Understanding File Search and Retrieval-Augmented Generation Creating and Organizing Vector Stores Uploading Documents for Retrieval Connecting Vector Stores to Responses API Requests Designing Document Metadata and Filtering Strategies Building a PDF Question-Answering Application Improving Retrieval Quality With Better Document Preparation Handling Missing Evidence and Retrieval Failures Combining File Search With Web Search Building a Production Knowledge-Base Assistant What the Code Interpreter Tool Is Designed For Running Python-Based Analysis Through the OpenAI SDK Uploading Datasets for Analysis Analyzing CSV and Spreadsheet Data Generating Charts and Data Summaries Handling Generated Files and Downloadable Artifacts Building a Data-Analysis Assistant Combining Code Execution with Structured Outputs Validating Generated Calculations and Results Security and Sandbox Considerations for Code Execution Understanding Multimodal Input with the OpenAI SDK Sending Images to a Model Image Analysis from URLs and Uploaded Files Extracting Text and Information from Screenshots Building an Image-Question-Answering Application Combining Image Input with Structured Output Analyzing Multiple Images in One Request Handling Image Quality and Input Limitations Designing Multimodal Prompts for Reliable Results Building a Practical Vision-Powered Python Application Understanding Speech-to-Text and Text-to-Speech Workflows Transcribing Audio with the OpenAI SDK Working with Uploaded Audio Files Handling Timestamps and Transcription Metadata Building a Meeting Transcription Workflow Generating Spoken Responses from Text Handling Long Audio and Processing Failures Combining Audio with Text and Tool Calling Building an End-to-End Python Voice Application What Embeddings Are and When to Use Them Generating Embeddings With the OpenAI API Preparing Text for Embedding Comparing Vectors With Cosine Similarity Building a Simple Semantic Search Engine in Python Storing Embeddings in a Database Metadata Filtering for Semantic Search Chunking Strategies for Better Retrieval Evaluating Semantic Search Quality Building a Document Similarity Application When Batch Processing Makes Sense Designing Large-Volume AI Processing Pipelines Using Asynchronous Python with the OpenAI SDK Running Concurrent Requests Safely Controlling Concurrency and Avoiding Rate Limits Tracking Batch Job Progress Handling Partial Failures in Bulk Workloads Retrying Failed Items Without Duplicating Successful Work Designing Resumable AI Processing Jobs Building a Production Batch-Processing Pipeline Batch Processing Makes Sense Large-Scale AI Processing Pipelines Async Python with OpenAI SDK Safe Concurrent Requests Concurrency & Rate Limits Batch Progress Tracking Partial Failure Handling Safe Retry Handling Resumable AI Jobs Production Batch Pipeline System–User Data Separation Reusable App Instructions Prompt Templates & Variables Extraction & Classification Prompts Summarization & Transformation Prompts Explicit Output Requirements Prompt Version Management Prompt Testing & Evaluation Reusable Python Prompt Library API Key Security Secure API Key Storage Secure Secret Management Prompt Injection Prevention Trusted vs. Untrusted Content Tool Argument Validation Sensitive Data Handling Secure Logging AI Action Authorization Production AI Security Checklist Why AI Applications Need Evaluation Beyond Unit Tests Unit Testing OpenAI SDK Integration Code Mocking API Responses in Python Tests Testing Structured Outputs Against Schemas Testing Tool-Calling Workflows Building a Small Evaluation Dataset Measuring Accuracy, Consistency, and Failure Rates Regression Testing Prompts and Model Changes Human Evaluation Versus Automated Evaluation Creating a Repeatable Evaluation Pipeline AI Request Monitoring Token Cost Management Usage Metrics Design Reducing Model Calls Prompt & Context Optimization Model Selection & Optimization AI Caching Strategies Interactive Latency Optimization Usage Dashboards & Budget Alerts Performance & Cost Checklist Every API Call Starts Fresh Fixing API Statelessness Server-Side Conversation Memory Limits of Response Chaining What We're Building Conversation Memory Challenges Preparing an OpenAI SDK Application for Deployment Environment-Specific Configuration for Development and Production Deploying a Python AI Service with Docker Container Health Checks and Startup Configuration Managing Secrets in Cloud Deployments Background Workers for Long-Running AI Tasks Queues and Asynchronous Job Architectures Scaling AI Workloads Horizontally Monitoring Production Incidents and Failures Production Deployment Checklist for OpenAI SDK Applications Reusable OpenAI Service Classes AI Client Dependency Injection Typed AI Responses Python Configuration Management AI Request Decorators Centralized AI Error Handling Clean SDK Abstractions Reusable OpenAI Utilities Internal AI Python Libraries SDK Integration Maintenance Production AI Chatbot Document Q&A System Web Research Assistant Customer Support Agent AI Data Analysis Assistant Image Analysis App Meeting Transcription & Summary Semantic Document Search Multi-Tool AI Agent Production OpenAI SDK App Why "It Looked Fine When I Tested It" Isn't Enough Timing Note Status Note Pre-Decision Status Note Current Availability Note
Ask about this post
AI Ask about this post

Ask questions about Resumable AI Jobs and get answers drawn from it.

Signed-in readers only.