Background Runs, Webhooks & Batch Processing
Long or high-volume workloads should not hold a user HTTP request open indefinitely. Use provider/background jobs, your own queue, webhooks or batch processing according to latency and scale requirements.
type AiJob = {
id: string;
status: 'queued' | 'running' | 'completed' | 'failed' | 'cancelled';
providerRunId?: string;
idempotencyKey: string;
};
Webhook rule
Verify webhook signatures, deduplicate repeated delivery, store event IDs and make handlers idempotent. Never assume exactly-once delivery.
Batch rule
Batch processing suits offline classification, embeddings, evals and large document jobs where immediate latency is unnecessary. Store per-item correlation IDs so partial failures can be retried without replaying successful items.
Practice
- When is polling simpler than webhooks?
- Why must webhook handlers be idempotent?
- What makes a workload a good batch candidate?
- How would you retry only failed items from a large batch?