To call AI Gateway from the AI SDK ↗︎, use createAI from agents/models/ai-sdk. It calls models from the model catalog, dynamic routes, and a vendor's own AI SDK models through your gateway. This page covers the AI Gateway features. For the full reference, refer to AI SDK.
The ai-gateway-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/providerIn a Worker, add the AI binding to your Wrangler configuration:
{
"ai": {
"binding": "AI",
},
}[ai]
binding = "AI"Pass an <author>/<model> id from the model catalog. You do not need a vendor package or a vendor key. AI Gateway bills the request through Unified Billing, or uses a key stored on the gateway:
import { createAI } from "agents/models/ai-sdk";
import { streamText } from "ai";
export default {
async fetch(request, env) {
const ai = createAI({ binding: env.AI, id: "my-gateway" });
const result = streamText({
model: ai("anthropic/claude-opus-5.5"),
messages: [{ role: "user", content: "Write a short story" }],
});
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, id: "my-gateway" });
const result = streamText({
model: ai("anthropic/claude-opus-5.5"),
messages: [{ role: "user", content: "Write a short story" }],
});
return result.toTextStreamResponse();
},
};Use the catalog's spelling of the id, such as anthropic/claude-opus-5.5 or openai/gpt-5.5. id sets the gateway and defaults to default. A dynamic route on that gateway is dynamic/<route>, for example ai("dynamic/support").
Pass a model from the vendor's own AI SDK package. The request goes unchanged to the vendor's provider-native endpoint on your gateway. AI Gateway holds the credential, so the vendor's apiKey is a placeholder:
import { createAnthropic } from "@ai-sdk/anthropic";
import { createAI } from "agents/models/ai-sdk";
const ai = createAI({ binding: env.AI, id: "my-gateway" });
const anthropic = createAnthropic({ apiKey: "cloudflare" });
const model = ai(anthropic("claude-opus-5-5"));Use the vendor's own model id here, such as claude-opus-5-5. To send a key you hold instead, refer to Unified billing and BYOK.
fallback lists models to try in order if the primary model fails before it produces any output. Mix catalog ids, dynamic routes, Workers AI ids, and model objects:
const model = ai(anthropic("claude-opus-5-5"), {
fallback: ["openai/gpt-5.5", "@cf/zai-org/glm-4.7-flash"],
});Set caching, logging, metadata, timeouts, and retries on the provider, on a model, or per call under providerOptions.cloudflare:
const ai = createAI({
binding: env.AI,
id: "my-gateway",
cacheTtl: 60,
metadata: { app: "support-bot" },
});
await generateText({
model: ai("openai/gpt-5.5"),
prompt: "Summarize this ticket",
providerOptions: {
cloudflare: { skipCache: true, metadata: { userId: "u_123" } },
},
});For every option, refer to Set options.
To use a gateway in another account, or to call from outside a Worker, pass accountId and apiToken instead of binding:
const ai = createAI({
accountId: "<ACCOUNT_ID>",
apiToken: env.CLOUDFLARE_API_TOKEN,
gateway: "my-gateway",
});The call forms and options are the same. Create an API token with the Workers AI Read permission for model ids and AI Gateway Run for model objects.