AI Batch Infer
Workers AI async batch embeddings with queueRequest and poll
Submit a batch of Workers AI embedding inferences with queueRequest: true, then poll by request_id via the Asynchronous Batch API. Without an AI binding, routes return demo queued/poll responses.
Features
- Queue embedding batches (
POST /batch) - Poll results by
request_id(GET /batch/:requestId) - Configurable model via
MODELvar (default@cf/baai/bge-m3)
API Reference
POST /batch
Body
{
"texts": ["first phrase", "second phrase"]
}| Field | Required | Description |
|---|---|---|
texts | Yes | 1–20 non-empty strings, max 2000 chars each |
Example Request
curl -X POST "https://your-worker.workers.dev/batch" \
-H "Content-Type: application/json" \
-d '{"texts":["Cloudflare Workers","edge compute"]}'Success Response
{
"status": "queued",
"model": "@cf/baai/bge-m3",
"request_id": "000-000-000",
"mode": "live"
}Error Codes
400- Invalid body (INVALID_BODY)502- Batch submit failure (BATCH_ERROR)
GET /batch/:requestId
Poll status/results for a queued batch.
Example Request
curl "https://your-worker.workers.dev/batch/000-000-000"While processing, the API may return queued or running. When complete, the response includes per-item results.
Error Codes
400- Invalid id (INVALID_REQUEST_ID)502- Poll failure (BATCH_ERROR)
Use Cases
- Offline embedding of large document sets
- Batch summarization / classification without interactive latency
- Avoid capacity errors by queueing inference for eventual completion
Limitations
- Total batch payload must stay under ~10 MB (Workers AI limit)
- Needs a batch-capable model and account AI access for live mode
- Demo mode returns placeholder embeddings
Deployment
Confirm AI binding
wrangler.json declares ai.binding: "AI" and vars.MODEL (default @cf/baai/bge-m3).
Test your deployment
curl -X POST "https://your-worker.workers.dev/batch" \
-H "Content-Type: application/json" \
-d '{"texts":["hello","world"]}'Local Development
cd apps/experiments/ai-batch-infer
npm install
npm run devWorkers AI batch typically needs remote AI / account access.
Configuration
- Workers AI binding
AI - Var
MODEL— batch embedding model id
Cloudflare Features Used
- Workers - Edge compute runtime
- Workers AI - Inference at the edge
- Asynchronous Batch API -
queueRequest+ poll byrequest_id