Skip to main content
TASK HANDLING

Handle task states and errors correctly

A stable integration distinguishes active states, terminal states, idempotency conflicts, and temporary service errors. Progress=100 alone is not success.

Last updated

Task states

StatusTerminalAction
queuedNoContinue polling
in_progressNoContinue polling
completedYesStop and read metadata.url
failedYesStop and record the error

Idempotent creation

  • Repeating the same request body with the same user and key returns or resumes the same task.
  • Using the same key with a different request body returns 409.
  • After a timeout, disconnect, or 5xx response, do not immediately retry with a new key.
  • Keep the original key and query the task or contact support to avoid duplicate work and charges.
  • After an explicit 400, 401, 403, or 429, fix the problem and retry with the same key.

HTTP errors

HTTPMeaningAction
400Invalid fields, model, or parametersUse message to fix the request
401Missing, invalid, disabled, or expired keyUse a valid API key
402Insufficient balance or quotaTop up or adjust quota
403Model, account, group, or source restrictionCheck permissions and IP rules
404Task not foundConfirm the task ID
409Idempotency conflictCompare the original request body and key
429Rate or concurrency limitWait for Retry-After and retry
5xxTemporary server failureRetry safely with the idempotency contract
Error responsejson
{
  "code": "invalid_request",
  "message": "prompt is required",
  "data": null
}

Log safety

Log only HTTP status, response body, request time, public task ID, and request ID. Never log a full API key, Authorization header, or a media URL containing credentials.

Node.js polling example
Handle terminal states in server code.
Security guidance
Review key isolation, limits, and material review.