1. Upload the input file
file_id. The same file id becomes the batch input handle.
2. Create the batch
batch_id immediately.
3. Poll status for control-plane recovery
Always keep your own polling path even when webhooks are enabled:lifecycle_statusfor a stable cross-job statuspolling_urlas the canonical status endpointcancel_urlwhen the batch is still cancellablenative_batch_idonly for provider correlation; continue polling with the gatewayidbilling.reservation_statusto distinguish held, settled, released, or failed billing state- webhook delivery summary fields to separate job execution from callback delivery
4. Recover owned jobs after restarts
Workers should list owned jobs during startup before creating replacements:id values. Provider-native ids are exposed for diagnostics, but they are not the durable handle for AI Stats polling or cancellation.
5. Cancel stale work when needed
If the batch is still pending or processing and your application no longer wants the result:6. Consume webhook deliveries
Gateway-managed async webhook payloads are normalized around:- the job id and job kind
lifecycle_status- sanitized webhook configuration
- delivery summary fields
- recent delivery attempts
- whether signing is enabled
- verify the signature
- process deliveries idempotently
- treat retries as normal
- fetch the latest batch status when the payload and local state disagree
webhook.secret, AI Stats signs each delivery with:
x-ai-stats-timestamp: Unix timestamp in secondsx-ai-stats-signature: hex HMAC-SHA256 of${timestamp}.${rawBody}using your webhook secretx-ai-stats-event-id: stable event id for this batch/eventx-ai-stats-event-type: event type such asbatch.progressorbatch.completedx-ai-stats-delivery-key: idempotency key, including progress bucket when applicablex-ai-stats-attemptandx-ai-stats-max-attempts: retry attempt metadata
x-ai-stats-delivery-key or the payload id before side effects. Return any 2xx status only after your durable state has been updated; non-2xx responses are retried with the same event id and an incremented attempt number.
Webhook subscriptions accept generic job.* events or matching batch.* events, including batch.progress / job.progress for request-count progress. Progress notifications are bucketed for idempotency and may be skipped when the provider has not reported usable request_counts yet. If you omit events, AI Stats subscribes the webhook to terminal job.completed, job.failed, job.cancelled, and job.expired notifications. If you provide events, at least one event must be valid for batch jobs; all-invalid or cross-kind-only lists such as ["video.completed"] are rejected instead of being broadened silently.
Webhook callback URLs must use HTTPS. Literal private, loopback, link-local, and wildcard hosts are rejected; http://localhost, http://127.0.0.1, and http://[::1] are accepted only for local development callbacks.
7. Fetch outputs and reconcile failures
Use the terminal batch object to decide the next step:- completed batches should move on to output retrieval and result ingestion
- failed and expired batches should capture both the batch terminal state and the webhook delivery state
- cancelled batches should stop downstream fan-out cleanly
- upstream batch execution failures
- cancellation requested by operators or automation
- webhook delivery failures after the batch itself already finished
8. What to monitor
- batch
lifecycle_status - provider and request correlation ids
- webhook delivery success and retry counts
- last delivery HTTP status
- last failure timestamp and message
- whether
cancel_urlwas still available when cancellation was requested