Text

Batch thousands of chat and text completions with batch() — author each request like the AI SDK's generateText, correlated by customId.

Submit a text batch

batch() is the core entry point: pass any AI SDK language model and a list of requests authored like generateText, and it returns a BatchJob handle immediately. Each request produces one text completion, correlated by customId.

import { batch } from "batchwork";
import { anthropic } from "@ai-sdk/anthropic";

const job = await batch({
  model: anthropic("claude-opus-4-8"),
  requests: [
    { customId: "doc-1", prompt: "Summarize…", temperature: 0 },
    {
      customId: "doc-2",
      system: "You are terse.",
      messages: [{ role: "user", content: "Translate…" }],
      maxOutputTokens: 256,
    },
  ],
});

Text batches are supported by every provider Batchwork supports. Everything on the job handle works the same — wait(), poll(), results(), collect(), and cancel() — as does rehydration with getBatch / getBatchResults / cancelBatch.

Request shape

Each request mirrors generateText (minus model), plus a customId used to correlate its result. Provide prompt or messages.

FieldNotes
customIdCorrelates the result. Auto-generated as request-<index> if omitted; must be unique within a batch.
prompt / messagesProvide one. messages is the AI SDK ModelMessage[].
systemSystem prompt.
tools / toolChoiceTool definitions, exactly as in generateText.
maxOutputTokensMaximum tokens to generate. Required by Anthropic.
temperature, topP, topKSampling controls.
presencePenalty, frequencyPenaltyPenalties.
stopSequences, seedStop sequences and seed.
providerOptionsProvider-specific options (e.g. reasoning / thinking config).

Batch-level options — defaults, metadata, and limits — apply to every batch function, including this one.

OpenAI request shapes

OpenAI exposes several request shapes, and Batchwork mirrors whichever the model implies:

ModelBatch endpoint
openai.chat("…")/v1/chat/completions
openai("…") / openai.responses("…")/v1/responses
openai.completion("…")/v1/completions

A "openai/…" string defaults to chat completions — the most widely supported batch endpoint.

Results

Text batches reuse the normalized BatchResult. The generated text lands on result.text; read the full provider payload — tool calls, structured output, finish reasons — from result.response.

for await (const result of job.results()) {
  if (result.status === "succeeded") {
    save(result.customId, result.text, result.usage);
  } else if (result.status === "errored") {
    console.error(result.customId, result.error?.message);
  }
}

usage is normalized to { inputTokens, outputTokens, totalTokens }, billed at the batch rate (~50% off).

How it's built

Like batch.images() and batch.embeddings(), batch() derives each provider request body by running the AI SDK's generateText() through a capturing fetch that records the serialized body and aborts before any network call. Every request field generateText accepts — system, messages, tools, multimodal content, providerOptions — carries through unchanged.

Batchwork serializes the request only; it does not run generateText's response side, so there is no automatic multi-step tool execution and no generateObject-style parsing. You get the raw model output back — read tool calls or structured output from result.response yourself.

On this page