Know when a submitted LLM batch never completes
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 happens | What Stallbell sees | Alert |
|---|---|---|
The batch ends as failed, expired or cancelled | A fail from the poller with the batch ID and status | Failed run |
| The results arrive but building the report throws | A fail from the poller with the error | Failed run |
| The poller dies, or the batch never ends | A start with no finish within 1 day | Stuck run |
| The submit call fails (bad key, quota, file error) | A fail from the submitter with the error | Failed run |
| The nightly submit doesn't run at all | No check-in by midnight plus grace | Missed run |
| The next batch completes | A good finish after the incident | Recovered |
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.
# 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}")# 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.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
| Field | Value | Why |
|---|---|---|
| Kind | Agent | A label only. The batch is LLM work. |
| How it runs | On a cron schedule | The batch is submitted at a set clock time each night. |
| Cron expression | 0 0 * * * | Midnight every night. Change it to match when your submitter runs. |
| Schedule timezone | Your account timezone | Follows your account timezone. Pick your server's zone if its cron runs on a different one. |
| Grace period | 30 minutes | 30 minutes, in case the submitter starts late or retries an upload. |
| Stuck after | 1 day | 1 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.
Related
- Docs: check-in fields
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.