Know when a submitted LLM batch never completes

Updated 3 min read

On this page
  1. The problem
  2. What goes wrong silently
  3. Setup
  4. Recommended settings
  5. Related
  6. FAQ

Your submitter opens a run in Stallbell with the batch ID, your poller closes it when the batch ends, and Stallbell emails you if the batch fails or expires, or if nothing closes the run within a day.

The problem

A nightly job submits an OpenAI batch: a few hundred prompts to summarise the day's tickets. A poller collects the results and builds the morning report. One night the batch sits in in_progress until it expires, or the poller crashes on a changed response, and the report just doesn't appear. Nothing errored where anyone would see it.

This page is a ready-made setup for one scenario.

What goes wrong silently

What Stallbell catches
What happensWhat Stallbell seesAlert
The batch ends as failed, expired or cancelledA fail from the poller with the batch ID and statusFailed run
The results arrive but building the report throwsA fail from the poller with the errorFailed run
The poller dies, or the batch never endsA start with no finish within 1 dayStuck run
The submit call fails (bad key, quota, file error)A fail from the submitter with the errorFailed run
The nightly submit doesn't run at allNo check-in by midnight plus graceMissed run
The next batch completesA good finish after the incidentRecovered

Creates a monitor called llm-batch-poller with the settings below. You can change everything later.

Setup

Two scripts, two cron lines. The batch ID is the run_id, which is how Stallbell joins the start from one process to the finish from another.

Python · submit_batch.py
# submit_batch.py: runs from cron at 00:00. Submits the batch and opens a run in Stallbell.
import json
import pathlib
import urllib.request

from openai import OpenAI

PING = "https://stallbell.com/p/TOKEN"
STATE = pathlib.Path("/var/lib/nightly-report/batch_id")

def ping(path, **fields):
    req = urllib.request.Request(
        PING + path,
        data=json.dumps(fields).encode(),
        headers={"content-type": "application/json"},
        method="POST",
    )
    try:
        urllib.request.urlopen(req, timeout=10).read()
    except Exception as exc:  # never let monitoring break the job
        print(f"Stallbell ping failed: {exc}")

client = OpenAI()
try:
    upload = client.files.create(file=open("requests.jsonl", "rb"), purpose="batch")
    batch = client.batches.create(
        input_file_id=upload.id,
        endpoint="/v1/chat/completions",
        completion_window="24h",
    )
except Exception as exc:
    ping("/fail", msg=f"batch submit failed: {exc}"[:500])
    raise
STATE.write_text(batch.id)
ping("/start", run_id=batch.id, msg=f"submitted {batch.id}")
Python · poll_batch.py
# poll_batch.py: runs from cron every 15 minutes. Closes the Stallbell run when the batch ends.
import json
import pathlib
import urllib.request

from openai import OpenAI

PING = "https://stallbell.com/p/TOKEN"
STATE = pathlib.Path("/var/lib/nightly-report/batch_id")

def ping(path, **fields):
    req = urllib.request.Request(
        PING + path,
        data=json.dumps(fields).encode(),
        headers={"content-type": "application/json"},
        method="POST",
    )
    try:
        urllib.request.urlopen(req, timeout=10).read()
    except Exception as exc:  # never let monitoring break the job
        print(f"Stallbell ping failed: {exc}")

def build_report(output_file_id):
    ...  # your code: download the results and build the report

if not STATE.exists():
    raise SystemExit(0)  # no batch in flight
batch_id = STATE.read_text().strip()
batch = OpenAI().batches.retrieve(batch_id)

if batch.status == "completed":
    try:
        build_report(batch.output_file_id)
    except Exception as exc:
        ping("/fail", run_id=batch_id, msg=f"report failed: {exc}"[:500])
        raise
    counts = batch.request_counts
    ping("/ok", run_id=batch_id, msg=f"{counts.completed} of {counts.total} requests done, {counts.failed} failed")
    STATE.unlink()
elif batch.status in ("failed", "expired", "cancelled"):
    ping("/fail", run_id=batch_id, msg=f"batch {batch_id} {batch.status}")
    STATE.unlink()
# validating, in_progress, finalizing, cancelling: send nothing. Stuck after covers a batch that never ends.
crontab
0 0 * * *    cd /opt/nightly-report && /opt/nightly-report/.venv/bin/python submit_batch.py
*/15 * * * * cd /opt/nightly-report && /opt/nightly-report/.venv/bin/python poll_batch.py

The poller stays quiet while the batch is running. It reports once, when the batch reaches an end state. The msg shows under Recent runs, and a Failed run email carries it on its Failure line, for example "batch batch_abc123 expired".

Recommended settings
FieldValueWhy
KindAgentA label only. The batch is LLM work.
How it runsOn a cron scheduleThe batch is submitted at a set clock time each night.
Cron expression0 0 * * *Midnight every night. Change it to match when your submitter runs.
Schedule timezoneYour account timezoneFollows your account timezone. Pick your server's zone if its cron runs on a different one.
Grace period30 minutes30 minutes, in case the submitter starts late or retries an upload.
Stuck after1 day1 day, the most Stallbell allows. It matches the batch API's 24-hour completion window.

Expiry and Stuck after can overlap. OpenAI marks a batch expired when it hasn't finished within 24 hours. Stuck after is also 1 day, counted from the start ping. If the poller sees the expiry after the stuck deadline has passed, you get a Stuck run email and then a Failed run email for the same batch. Both mean the same thing: the report didn't get built.

FAQ

Does Stallbell check the batch status with OpenAI?

No. It never talks to your provider. Your poller reads the status and tells Stallbell when the batch ends. If the poller stops, Stuck after still fires.

Why doesn't the poller ping on every check?

A check-in while the batch is running would tell Stallbell nothing it can use, and progress pings reset the Stuck after clock. The poller only reports the end: done, failed, expired or cancelled.

Can I use this with another provider's batch API?

Yes. The Stallbell side is the same: start with the batch ID as run_id when you submit, then ok or fail with the same run_id when it ends. Swap the two SDK calls for your provider's.

What if my provider's batches can take longer than a day?

Then this pattern can't watch them end to end. Stuck after tops out at 1 day.