Discussions

Ask a Question
Back to all

How should an AI image generator handle long-running jobs on Cloudflare Workers?

I am building Photo2Ads, an AI product-ad studio:

https://photo2ads.com

The application uses TanStack Start, React 19, Cloudflare Workers, Cloudflare Workflows, D1, R2, and external image-generation providers.

The current workflow is:

  1. The user uploads a product photo.
  2. The server creates a pending generation record in D1.
  3. A Cloudflare Workflow consumes credits and submits the request to an AI provider.
  4. The Workflow polls the provider until the result is ready.
  5. The result is first exposed through the provider URL and then persisted to R2.
  6. The client polls the generation status and displays the result in the user's history.

I would like to hear how other developers handle the following edge cases:

  • Making provider retries idempotent.
  • Refunding credits when a provider fails after charging.
  • Separating provider completion from durable R2 storage.
  • Recovering unfinished jobs after a browser refresh.
  • Handling tasks that take several minutes or never return.

What database state machine and retry strategy would you recommend for this type of AI generation workflow on Cloudflare Workers?