Follow one user question through several possible worker attempts. Keep the saved reply stable, recover abandoned work, and make an explicit decision when a timeout leaves the model provider’s outcome unknown.
The user presses Send once
One requested reply must not become extra saved replies when recovery repeats work.
The PDF from the queue guide is now searchable. The user asks, “What are the key decisions in this document?” and presses Send once. We create reply task A7: produce one response to that message. The browser may disconnect and a worker may crash, but those failures do not mean the user asked another question.
For this constructed example, the work runs in the background and the completed answer is saved before the chat displays it. Background execution lets accepted work outlive the connection; it is not a requirement for every chat product. We are protecting the stored reply, rather than making model output deterministic or proving its answer correct.
Name the promise precisely: at most one saved response per reply task. That leaves room for no response if every allowed attempt fails, and for repeated model computation during recovery. A user who intentionally asks for another answer creates new logical work. Those distinctions tell us which identities and safeguards the design needs.
Follow one successful reply
A task is the work owed to the user; an attempt is one effort to finish it.
The API saves the user message, creates A7 and records a recoverable queue handoff. As in the upload guide, accepted work cannot depend only on an API process remembering to publish. The dispatcher can publish again after a failure, but every delivery still identifies A7.
A worker receives the task and calls the LLM provider. The returned text is a candidate answer. It becomes the saved response only when our database accepts it. Saving the response and marking A7 complete happen together, so a reconnecting browser cannot observe “complete” without the corresponding reply.
The worker acknowledges queue progress after that durable save. The browser can fetch the saved response later; queue acknowledgment does not wait for the screen to display it. Keep user message, task, delivery, attempt, candidate answer and saved response distinct. We can now move a crash between these steps and identify the missing evidence without changing what the user requested.
Why the same work can return
Receiving a delivery and recording successful completion are separate events.
Suppose the provider returns text, then the worker crashes before saving it. A7 is still unfinished, so recovery tries again. The provider may do expensive work twice even though only one task exists. A queue can preserve unfinished work without preventing that repeated computation.
With SQS, receiving a message normally hides it for a visibility period. Successful processing is followed by deletion. If it is not deleted before the period expires, another worker can receive it. A worker can extend the period, but Standard queues can still deliver duplicates during visibility. Treat that timer as delivery coordination, not an exclusive correctness lock.