To use Workers AI with the AI SDK ↗︎, use createAI from agents/models/ai-sdk. This page covers Workers AI models. For AI Gateway catalog models, fallback, gateway options, and the full option reference, refer to AI SDK.
The workers-ai-provider package is deprecated. Use createAI instead.
Install the Agents SDK and the AI SDK. createAI needs ai@^7 and @ai-sdk/provider@^4:
npm i agents ai @ai-sdk/provideryarn add agents ai @ai-sdk/providerpnpm add agents ai @ai-sdk/providerbun add agents ai @ai-sdk/providerAdd the AI binding to your Wrangler configuration:
{
"ai": {
"binding": "AI",
},
}[ai]
binding = "AI"Pass the id of any Workers AI model. Model ids start with @cf/ and autocomplete from @cloudflare/workers-types:
import { createAI } from "agents/models/ai-sdk";
const ai = createAI({ binding: env.AI });
const model = ai("@cf/zai-org/glm-4.7-flash");import { createAI } from "agents/models/ai-sdk";
import { generateText } from "ai";
export default {
async fetch(request, env) {
const ai = createAI({ binding: env.AI });
const { text } = await generateText({
model: ai("@cf/zai-org/glm-4.7-flash"),
prompt: "Write a 50-word essay about hello world.",
});
return new Response(text);
},
};import { createAI } from "agents/models/ai-sdk";
import { generateText } from "ai";
export default {
async fetch(request: Request, env: Env) {
const ai = createAI({ binding: env.AI });
const { text } = await generateText({
model: ai("@cf/zai-org/glm-4.7-flash"),
prompt: "Write a 50-word essay about hello world.",
});
return new Response(text);
},
};For longer responses, stream the output as the model generates it:
import { createAI } from "agents/models/ai-sdk";
import { streamText } from "ai";
export default {
async fetch(request, env) {
const ai = createAI({ binding: env.AI });
const result = streamText({
model: ai("@cf/zai-org/glm-4.7-flash"),
prompt: "Write a 50-word essay about hello world.",
});
return result.toTextStreamResponse();
},
};import { createAI } from "agents/models/ai-sdk";
import { streamText } from "ai";
export default {
async fetch(request: Request, env: Env) {
const ai = createAI({ binding: env.AI });
const result = streamText({
model: ai("@cf/zai-org/glm-4.7-flash"),
prompt: "Write a 50-word essay about hello world.",
});
return result.toTextStreamResponse();
},
};Pass a Zod schema to get a typed JSON response:
import { createAI } from "agents/models/ai-sdk";
import { generateText, Output } from "ai";
import { z } from "zod";
export default {
async fetch(request, env) {
const ai = createAI({ binding: env.AI });
const { output } = await generateText({
model: ai("@cf/zai-org/glm-4.7-flash"),
prompt: "Generate a lasagna recipe.",
output: Output.object({
schema: z.object({
recipe: z.object({
ingredients: z.array(z.string()),
description: z.string(),
}),
}),
}),
});
return Response.json(output);
},
};import { createAI } from "agents/models/ai-sdk";
import { generateText, Output } from "ai";
import { z } from "zod";
export default {
async fetch(request: Request, env: Env) {
const ai = createAI({ binding: env.AI });
const { output } = await generateText({
model: ai("@cf/zai-org/glm-4.7-flash"),
prompt: "Generate a lasagna recipe.",
output: Output.object({
schema: z.object({
recipe: z.object({
ingredients: z.array(z.string()),
description: z.string(),
}),
}),
}),
});
return Response.json(output);
},
};Embeddings, images, speech, transcription, and reranking come from the same provider:
import {
embedMany,
generateImage,
generateSpeech,
rerank,
transcribe,
} from "ai";
const { embeddings } = await embedMany({
model: ai.embedding("@cf/baai/bge-base-en-v1.5"),
values: ["hello", "world"],
});
const { image } = await generateImage({
model: ai.image("@cf/black-forest-labs/flux-1-schnell"),
prompt: "A cyberpunk lizard",
});
const { audio } = await generateSpeech({
model: ai.speech("@cf/deepgram/aura-1"),
text: "Hello from Cloudflare.",
});
const { text } = await transcribe({
model: ai.transcription("@cf/openai/whisper-large-v3-turbo"),
audio: audio.uint8Array,
});
const { rerankedDocuments } = await rerank({
model: ai.reranking("@cf/baai/bge-reranker-base"),
documents: ["a cyberpunk lizard", "a rainy Tuesday"],
query: "Which one is cooler?",
});For model-specific settings, refer to Use other modalities.
Requests go through AI Gateway. The gateway defaults to default. To use another gateway, set its id:
const ai = createAI({ binding: env.AI, id: "my-gateway" });The same provider also calls models from the AI Gateway catalog, such as anthropic/claude-opus-5.5. Refer to AI SDK for catalog models, caching, fallback, and the other gateway options.