Installation

Install Batchwork and the AI SDK provider packages for the providers you batch with.

Install

Batchwork depends on ai (the Vercel AI SDK). The provider packages are optional peer dependencies — install only the ones you batch with.

npm install batchwork @ai-sdk/openai @ai-sdk/anthropic
pnpm add batchwork @ai-sdk/openai @ai-sdk/anthropic
bun add batchwork @ai-sdk/openai @ai-sdk/anthropic
yarn add batchwork @ai-sdk/openai @ai-sdk/anthropic

If you only batch with OpenAI, you only need @ai-sdk/openai — and vice versa.

Credentials

Batchwork reads provider credentials from the standard environment variables:

OPENAI_API_KEY=sk-…
ANTHROPIC_API_KEY=sk-ant-…

Or pass them explicitly per call:

await batch({
  model: "openai/gpt-5.5",
  apiKey: process.env.MY_OPENAI_KEY,
  requests,
});

baseURL and extra headers are accepted the same way for self-hosted gateways or beta flags.

Requirements

  • Node 20+, Bun, or any edge runtime with fetch, FormData, and Web Crypto.
  • The AI SDK v6 (ai@^6) and provider packages v3 (@ai-sdk/openai@^3, @ai-sdk/anthropic@^3).

Errors

All thrown errors extend BatchworkError, with two typed subclasses you may want to catch:

  • MissingDependencyError — you batched a provider whose @ai-sdk/* package isn't installed. The message tells you exactly what to npm install.
  • UnsupportedProviderError — the model resolved to a provider without a batch adapter (i.e. not one of the seven supported providers).

Provider error responses include the method, URL, and status code, but not the upstream response body. This keeps prompts, provider diagnostics, and other batch data out of route responses and logs by default.

import { batch, MissingDependencyError } from "batchwork";

try {
  await batch({ model: "anthropic/claude-opus-4-8", requests });
} catch (error) {
  if (error instanceof MissingDependencyError) {
    // e.g. run `npm install @ai-sdk/anthropic`
  }
}

On this page