Skip to main content

Async LLM requests

The llm_requests queue lets a caller run a slow LLM call without holding a connection. The caller inserts a row, publishes the prompt, and polls the row for the result.

What are the helper functions?​

FunctionPurpose
requestLlmAsync(prompt, opts)Inserts the pending row, publishes, and returns {request_id} immediately.
getLlmRequestResult(id)One poll of the row. Returns null for an unknown id.
waitForLlmResult(id, {timeoutMs, pollIntervalMs})Polls until the row leaves pending. Defaults: 120 s timeout, 2 s interval.
processAsyncLlmRequest(payload)The queue consumer.

What does the result row hold?​

ColumnWhen set
statuspending, then succeeded or failed.
result_text, provider, modelOn success.
error_messageOn failure.
completed_atOn either.

The result updates are not best-effort. A swallowed failure would leave a caller polling a row that never completes.

What can go wrong?​

FailureResult
request_id or prompt missingPermanentError, discarded
Every provider failsRow marked failed, then the error is rethrown and the message requeues
Caller waits too longwaitForLlmResult throws. The row still completes later.