01Overview
Some of my AI jobs take minutes to hours. One run collects 90 to 315 answers from ChatGPT, Claude and Gemini, over 12 minutes to two hours.
The platform I run them on, Cloudflare Workers, isn't built for jobs that long. The server there only works when something sends it a request, such as a browser loading a page. The server does the work and sends back a response. Cloudflare stops any work still going about 30 seconds after that response.
The usual fix is a job queue, but I kept the run going without one. That took three versions. This page covers what failed in the first two, why I didn't add a queue, and how the third works.
Cloudflare Workers · D1 (SQLite) · KV · TypeScript · Claude, OpenAI and Gemini APIs
02Why the first two versions failed in production
A run asks its questions in batches, a few at a time. In the first version, the server chained the batches itself. When it finished one batch, it sent itself a request that said "start the next batch". No browser had to stay open.
Three runs on that version stalled between March and May 2026. It was also slow: a 120-question run spent about 35 minutes asking its questions.
At the end of July 2026 I rebuilt it to be faster. The rebuild still had the server calling itself. It worked every time on my machine, but its first four tries in production all failed. None of the limits behind those failures exist in local development.
The failures had two causes:
- A deleted lock still looked set. To stop two requests working on one run at once, each batch set a lock when it started and deleted it when it finished. The locks lived in Cloudflare KV, a key-value store that can serve an old value for about 60 seconds. One batch deleted its lock, but the next still read it as set and gave up.
- Each batch outlasted the 30 seconds Cloudflare allows after a response. The server sent each "start the next batch" request after it had already responded to its own caller. Cloudflare allows work to continue after a response, through a feature called
waitUntil, but only for about 30 seconds in production. A batch of Claude questions takes 40 to 70 seconds, so Cloudflare stopped each one partway through.
I then tried four workarounds, in this order:
- A watchdog. Sixty seconds after a batch finished, a check looked for batches that had died. It freed their questions and restarted the chain.
- Calls through the site's public address. The server's calls to itself shared the lifetime of the request that started them, so they died together. Calling the public address made each call a separate request.
- A cron job, a task Cloudflare runs on a schedule. Every five minutes it restarted any run that had gone quiet.
- Respond first, work after. Each request took its batch, responded at once and did the work after its response.
None of them worked. Four test runs in production showed the same thing: Cloudflare stops work about 30 seconds after a response, whichever request it runs in. The restarted batches were killed the same way, in a loop.
Those failures all led to the same conclusion: long work only survives while whoever asked for it is still connected, waiting for the response. A request in that state is an open request.
That conclusion also explained why the first version had worked at all. The run's page in the browser kept asking the server for progress and waiting for each response. Those were open requests, and they had been carrying the work all along, not the server's requests to itself.
03Queues, Durable Objects or Batches: why I built it myself
Before rebuilding again, I weighed three ready-made options:
- Cloudflare Queues, the platform's job queue, would have solved the problem. It also meant more infrastructure and a harder setup on my own machine, when the browser's requests could already carry the work.
- Durable Objects, another Cloudflare service that can hold long work, would have solved it too, at the same cost.
- Anthropic's Batches API answers questions in bulk, far cheaper per question. A batch can take 24 hours to come back, though, and I watch a run live. It's the right tool for overnight bulk re-runs.
I built the third version on open requests instead, in August 2026. The browser's requests now carry the work by design, not by accident. When no browser is open, a cron job sends the same requests.
A job queue normally provides two guarantees. Without one, I had to build both:
- Each piece of work goes to exactly one request. If a piece has to be redone, redoing it causes no harm.
- A request can be cut off before its work is finished, by Cloudflare or by a dropped connection. The next request notices the unfinished work and takes it over.
04Every piece of work now rides on an open request
Cloudflare puts no time limit on a request while whoever sent it is still connected. All the work therefore happens inside open requests.
In a run, it works in rounds:
- The run's page in the browser sends the server a request.
- The server sends itself three requests in parallel, one per assistant. I call them lanes.
- Each lane takes a batch of questions, puts them to its assistant and saves the answers.
- The server responds to the browser only when all three lanes are done.
05One SQL statement gives each question to exactly one lane
Step 3 of a round carries a risk, because rounds can overlap. Three things can start a round:
- the run's page in the browser;
- the cron job;
- a request sent again after a failure.
Nothing stops two of them from starting a round at the same moment. Two lanes for the same assistant would then be running together. Without protection, both could take the same questions and ask them twice.
The protection is in the database. Each question for each assistant is a row in a jobs table, marked "pending" until a lane takes it. A lane takes its batch with one SQL statement:
UPDATE jobs SET status = 'processing'
WHERE id IN (
SELECT id FROM jobs
WHERE run_id = ? AND worker = ? AND status = 'pending'
LIMIT ?
)
RETURNING id
The statement finds pending rows and marks them "processing" in a single step. The database runs it for one lane at a time, so two lanes can never get the same row. A batch holds six questions for Claude, and three for ChatGPT and Gemini.
The move from one phase of a run to the next is another example. A run first asks its questions, then processes the answers. When the last lane finishes, several requests may try to start the second phase. The update that changes the phase only works while the run is still in the first phase. The first request to run it succeeds, and the others find nothing to change.
The locks that failed in section 02 are still in the code, but nothing depends on them now. Each lane still sets a lock in KV that expires after 90 seconds. Rounds skip the lock check, though, because an old value in KV could wrongly block a lane. The SQL statement alone prevents doubled questions.
06Dead lanes and rate limits slow a run, but don't fail it
Marking a question "processing" creates a second risk. If the lane that took it dies mid-batch, the question stays marked as taken, and no other lane will ask it. The run recovers from a dead lane in four ways:
- A lane that finds no waiting questions frees the stuck ones. A lane sometimes finds no "pending" questions left for its assistant. Any question still marked "processing" must then be stuck. The lane resets those to "pending". A lane in the next round then takes them in its batch, like any other waiting question.
- A cron job keeps a run going when no browser is open. Normally the run's page in the browser sends the requests. If I close that page, nothing sends them. The cron job therefore fires every five minutes and starts rounds for any unfinished run. Cloudflare lets a cron job work for about 15 minutes, so it runs rounds back to back for that long.
- A run fails only after an hour without progress. Runs take from 12 minutes to over two hours, so total time can't tell a slow run from a dead one. The server therefore looks at when a run last made progress. After an hour with none, the cron job stops driving the run, and the server marks it as failed.
- The later phases restart where they stopped. After the questions, the run processes the answers one row at a time and records each row it finishes. A restart therefore continues at the first unfinished row, not from the beginning.
Rate limits are the other thing that can stop a lane. Each provider limits how much an account can send per minute. Claude's lane waits about 162 milliseconds between questions to stay under its limit, a figure I calculated from measured usage. ChatGPT and Gemini, by contrast, don't need a wait. If a provider still refuses a question, the lane puts it back to "pending" and pauses for 30 seconds.
07From 35 minutes to 6, with no failed runs since August
Sections 04 to 06 describe the third version, which shipped on 3 August 2026. Compared with the first version, it is faster and no run has failed:
- A 120-question run took about 35 minutes to ask its questions in the first version, one batch at a time. With three lanes in parallel, it now takes 5 to 6.
- A run with no browser open used to stall silently and fail after an hour. The cron job now completes it.
Since the third version shipped, there have been 21 runs. 16 completed and I cancelled 5 by hand. None failed. Across the completed runs, 24 of 2,955 answers failed, or 0.8%.
One run did stall, in the phase after its questions were all answered, and I finished it by hand. I made that phase restartable on 26 August, as section 06 describes. No run has stalled since.
08A known flaw: the same question asked twice
The reset of stuck questions in section 06 has a flaw. It assumes every question marked "processing" is stuck. A question that a live lane is asking right now is marked the same way, so the reset frees it too.
That can happen when two things drive rounds at once, usually the browser and the cron job. Near the end of a run, one round's lane finds no waiting questions. It then frees the questions the other round's lane is still asking, and they are asked again. Nothing caps this. Each further overlap while an answer is pending adds another ask.
The server keeps only the latest answer, so no figure changes. I still pay for every answer, though.
The fix is small: record when each row is taken, and reset only rows taken more than about 10 minutes ago. Shipping it is the risky part:
- Rows in flight at release would have no recorded time. They could stay stuck until the run failed an hour later.
- Code deployed before the database change would break every run.
I judged the duplicates cheaper than either risk, so I left the flaw in. I haven't measured how often they happen.
09What I'd change next
- Ship the fix for the flaw. It needs a release with no run in flight, with the database change going out first.
- Count how often the recovery steps run. The server only writes resets, cron-started rounds and rate-limit pauses to a log that expires after an hour. Storing them in the database would show how often each one happens.
- Reconsider a queue if runs go unattended. If large unwatched runs became normal, Queues would be worth the extra infrastructure.
How I built this
I build with AI coding agents. I set the design and make the trade-offs.