Following on from my last post about picking an image model, this one is about the part I underestimated most: the job layer underneath.
Everyone plans for the happy path. You send a prompt, you get an image. The honest version of that sentence is: you send a prompt, you get a job ID, and then you spend the next three weeks discovering all the ways a job can fail.
The mental model shift
A synchronous API call has two outcomes: success or error. You handle both with a try/catch and move on.
An async generation job has at least six states, and if your code only models two of them you will ship bugs:
queued → running → succeeded → failed_retryable → failed_permanent → cancelled → lost That last one, lost, is the one nobody plans for. A job can be accepted by the provider and then simply never report again — the worker died, the queue message expired, the webhook never fired. If your UI's spinner is waiting on a status that will never arrive, the user is stuck forever and you'll never see an error in your logs.
The fix is boring and mandatory: every job gets a deadline. When you create a job, write down the time by which it must have reached a terminal state. A sweeper marks anything past that as lost and surfaces it as a retryable failure. Without this, your "success rate" metric silently includes jobs that never finished.
Retries need to know why they failed
Lumping all failures into one retry bucket is the second mistake. The failures I've actually seen fall into distinct classes that want opposite treatment:
| Failure | Retry? | Why |
|---|---|---|
| Rate limit (429) | Yes, with backoff | Nothing wrong with the request |
| Timeout / 5xx | Yes, once or twice | Often transient infrastructure |
| Content policy rejection | No | Same prompt will be rejected again |
| Malformed prompt | No | Deterministic |
| Insufficient credits | No (billing problem) | Retrying burns nothing but time |
| Provider outage | Yes, longer backoff — or switch provider | Transient but not quick |
Retrying a content-policy rejection is the classic own-goal: you burn your rate limit, you annoy the queue, and the user waits twice as long to get the same rejection. Classify first, retry only what deserves it.
Idempotency is not optional
If you accept a client request, charge a credit, and then the client retries because the response was slow, you've charged twice for one intent.
The pattern that fixed this for me: the client generates a UUID per intent (not per request), and the server treats that UUID as the idempotency key. Same key, same job — return the existing job rather than creating a new one. Store the key for at least as long as a user might plausibly retry.
One subtlety: the key has to cover the whole request, prompt included. If the client retries with a slightly different prompt but the same key, you should still return the original job, not silently generate something the user didn't ask for twice.
Backpressure: the part that decides your bill
Here's the failure mode that actually costs money. A user opens three browser tabs, or a script loops, or a retry storm hits. Suddenly you have 400 concurrent generations, all of which will be billed.
Two separate limits are needed and they're often conflated:
- Per-user concurrency — protects fair use. Typically 1–3 in flight.
- Global concurrency — protects your wallet and your provider relationship. This is the one you set conservatively.
Queue above the limit rather than rejecting. A user who wants 20 images should wait, not get an error. But give them a visible queue position, because "nothing is happening" is the worst UX in a product that takes time.
The asset problem after success
Generation is a job. What comes out is a file, and files outlive jobs.
If your job table stores a provider URL, that URL will eventually expire. When it does, the user's gallery fills with broken images and you have no way to regenerate them without re-charging.
So the moment a job succeeds, copy the asset to your own storage and store your key. Treat the provider URL as a temporary handle with a lifetime measured in hours, not a permanent address. This is also what lets you switch providers later without orphaning history.
What I'd build first, in order
- Job table with explicit states including
lost, and a deadline column. - Classified errors — at minimum a boolean
retryablederived from the provider's error type. - Idempotency key on the create endpoint.
- Per-user and global concurrency limits with queueing.
- Asset copy-on-success into your own bucket.
None of that is AI-specific. All of it is what separates "a demo" from "a product that bills correctly."
If you're running multi-model generation — especially where one user session touches image, video and audio providers with different retry semantics — the variation between providers is the real cost. I ended up centralizing job orchestration and credit accounting in one place, which is what Dreamega does under the hood — one job model and one balance across all the models, rather than five separate integrations with five different ideas about what "failed" means.
Curious what others do about the lost state — do you sweep on a timer, or rely on provider webhooks plus a heartbeat?

