Skip to content

Vercel AI SDK

Last updated View as MarkdownAgent setup

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.

Setup

Install the Agents SDK and the AI SDK. createAI needs ai@^7 and @ai-sdk/provider@^4:

npm i agents ai @ai-sdk/provider

Add the AI binding to your Wrangler configuration:

{
	"ai": {
		"binding": "AI",
	},
}
[ai]
binding = "AI"

Choose a model

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");

Generate text

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);
	},
};

Stream 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();
	},
};

Generate structured output

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);
	},
};

Use other model types

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.

Use AI Gateway

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.

Was this helpful?