Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 28 additions & 35 deletions src/components/ai-gateway/code-example-selector.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -382,58 +382,54 @@ export function CodeExample({

export const modelOptions = [
{
model: "gpt-5.2",
model: "gpt-6-luna",
provider: "openai",
aiSDK: {
id: "openai/gpt-6-luna",
pkg: "@ai-sdk/openai",
providerFactory: "createOpenAI",
providerUsage: "openai.chat",
provider: "openai",
},
},
{
model: "claude-4-5-sonnet",
model: "claude-opus-5-5",
provider: "anthropic",
aiSDK: {
id: "anthropic/claude-opus-5.5",
pkg: "@ai-sdk/anthropic",
providerFactory: "createAnthropic",
providerUsage: "anthropic",
provider: "anthropic",
},
},
{
model: "gemini-2.5-pro",
model: "gemini-4-argon",
provider: "google",
aiSDK: {
providerFactory: "createGoogle",
providerUsage: "google",
id: "google/gemini-4-argon",
pkg: "@ai-sdk/google",
providerFactory: "createGoogleGenerativeAI",
provider: "google",
},
},
{
model: "grok-4",
model: "grok-4.7",
provider: "grok",
aiSDK: {
id: "xai/grok-4.7",
pkg: "@ai-sdk/xai",
providerFactory: "createXai",
providerUsage: "xai",
provider: "xai",
},
},
{
model: "customer-support",
provider: "dynamic",
aiSDK: {
providerFactory: "createUnified",
providerUsage: "unified",
provider: "unified",
},
aiSDK: { id: "dynamic/customer-support" },
},
{
model: "@cf/meta/llama-3.3-70b-instruct-fp8-fast",
provider: "workers-ai",
aiSDK: {
providerFactory: "createUnified",
providerUsage: "unified",
provider: "unified",
},
aiSDK: { id: "@cf/meta/llama-3.3-70b-instruct-fp8-fast" },
},
];
export const code = `import OpenAI from "openai";
Expand All @@ -450,38 +446,35 @@ const response = await client.chat.completions.create({
messages: [{ role: "user", content: "Hello, world!" }],
});`;

export const aiSDKUnifiedCode = `import { createAiGateway } from 'ai-gateway-provider';
import { createUnified } from 'ai-gateway-provider/providers/unified';
export const aiSDKUnifiedCode = `import { createAI } from "agents/models/ai-sdk";
import { generateText } from "ai";

const aigateway = createAiGateway({
const ai = createAI({
accountId: "{CLOUDFLARE_ACCOUNT_ID}",
gateway: '{GATEWAY_NAME}',
apiKey: '{CF_AIG_TOKEN}',
apiToken: "{CF_API_TOKEN}",
gateway: "{GATEWAY_NAME}",
});

const unified = createUnified({apikey});

const { text } = await generateText({
model: aigateway(unified('{provider}/{model}')),
prompt: 'What is Cloudflare?',
model: ai("{id}"),
prompt: "What is Cloudflare?",
});`;

export const aiSDKNativeCode = `import { createAiGateway } from 'ai-gateway-provider';
import { {providerFactory} } from 'ai-gateway-provider/providers/{provider}';
export const aiSDKNativeCode = `import { createAI } from "agents/models/ai-sdk";
import { {providerFactory} } from "{pkg}";
import { generateText } from "ai";

const aigateway = createAiGateway({
const ai = createAI({
accountId: "{CLOUDFLARE_ACCOUNT_ID}",
gateway: '{GATEWAY_NAME}',
apiKey: '{CF_AIG_TOKEN}',
apiToken: "{CF_API_TOKEN}",
gateway: "{GATEWAY_NAME}",
});

const {provider} = {providerFactory}({apikey});

const { text } = await generateText({
model: aigateway({providerUsage}('{model}')),
prompt: 'What is Cloudflare?',
model: ai({provider}("{model}"){vendorKey}),
prompt: "What is Cloudflare?",
});`;

export const curlCode = `curl -X POST https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/compat/chat/completions \\
Expand Down
133 changes: 63 additions & 70 deletions src/components/ai-gateway/code-examples.astro
Original file line number Diff line number Diff line change
Expand Up @@ -103,81 +103,74 @@ const { forceClient, forceAPI } = props.parse(Astro.props);
<Code
lang="javascript"
title=""
code={aiSDKUnifiedCode
.replaceAll("{provider}", option.provider)
.replaceAll("{apikey}", "")
.replace("{model}", option.model)}
code={aiSDKUnifiedCode.replace("{id}", option.aiSDK.id)}
/>
</CodeExample>
))}
{(!forceClient || forceClient === "aisdk") &&
modelOptions.map((option) => (
<CodeExample
client:load
provider={option.provider}
clientType="aisdk"
keyType="in-request"
apiType="unified"
forceClientType={forceClient}
forceApiType={forceAPI}
>
<Code
lang="javascript"
title=""
code={aiSDKUnifiedCode
.replaceAll("{provider}", option.provider)
.replaceAll("{apikey}", "{ apiKey: '{API_KEY}' }")
.replace("{model}", option.model)}
/>
</CodeExample>
))}

{(!forceClient || forceClient === "aisdk") &&
modelOptions.map((option) => (
<CodeExample
client:load
provider={option.provider}
clientType="aisdk"
keyType="stored"
apiType="native"
forceClientType={forceClient}
forceApiType={forceAPI}
>
<Code
lang="javascript"
title=""
code={aiSDKNativeCode
.replaceAll("{provider}", option.aiSDK.provider)
.replaceAll("{providerFactory}", option.aiSDK.providerFactory)
.replaceAll("{providerUsage}", option.aiSDK.providerUsage)
.replaceAll("{apikey}", "")
.replace("{model}", option.model)}
/>
</CodeExample>
))}
modelOptions
.filter((option) => option.aiSDK.pkg !== undefined)
.flatMap((option) =>
(["unified", "native"] as const).map((apiType) => ({
option,
apiType,
})),
)
.map(({ option, apiType }) => (
<CodeExample
client:load
provider={option.provider}
clientType="aisdk"
keyType="in-request"
apiType={apiType}
forceClientType={forceClient}
forceApiType={forceAPI}
>
<Code
lang="javascript"
title=""
code={aiSDKNativeCode
.replaceAll("{provider}", option.aiSDK.provider ?? "")
.replaceAll(
"{providerFactory}",
option.aiSDK.providerFactory ?? "",
)
.replace("{pkg}", option.aiSDK.pkg ?? "")
.replace("{apikey}", '{ apiKey: "{API_KEY}" }')
.replace("{vendorKey}", ', { vendorKey: "request" }')
.replace("{model}", option.model)}
/>
</CodeExample>
))}
{(!forceClient || forceClient === "aisdk") &&
modelOptions.map((option) => (
<CodeExample
client:load
provider={option.provider}
clientType="aisdk"
keyType="in-request"
apiType="native"
forceClientType={forceClient}
forceApiType={forceAPI}
>
<Code
lang="javascript"
title=""
code={aiSDKNativeCode
.replaceAll("{provider}", option.aiSDK.provider)
.replaceAll("{providerFactory}", option.aiSDK.providerFactory)
.replaceAll("{providerUsage}", option.aiSDK.providerUsage)
.replaceAll("{apikey}", "{ apiKey: '{API_KEY}' }")
.replace("{model}", option.model)}
/>
</CodeExample>
))}
modelOptions
.filter((option) => option.aiSDK.pkg !== undefined)
.map((option) => (
<CodeExample
client:load
provider={option.provider}
clientType="aisdk"
keyType="stored"
apiType="native"
forceClientType={forceClient}
forceApiType={forceAPI}
>
<Code
lang="javascript"
title=""
code={aiSDKNativeCode
.replaceAll("{provider}", option.aiSDK.provider ?? "")
.replaceAll(
"{providerFactory}",
option.aiSDK.providerFactory ?? "",
)
.replace("{pkg}", option.aiSDK.pkg ?? "")
.replace("{apikey}", '{ apiKey: "cloudflare" }')
.replace("{vendorKey}", "")
.replace("{model}", option.model)}
/>
</CodeExample>
))}

{(!forceClient || forceClient === "curl") &&
modelOptions.map((option) => (
Expand Down
61 changes: 61 additions & 0 deletions src/content/changelog/agents/2026-10-21-createai.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
---
title: createAI replaces workers-ai-provider and ai-gateway-provider
description: The Agents SDK has a new createAI function for the AI SDK and Pi AI that unifies workers-ai-provider and ai-gateway-provider. Both packages are now feature frozen and marked as deprecated.
products:
- agents
- workers-ai
- ai-gateway
date: 2026-10-21
---

import { TypeScriptExample } from "~/components";

The Agents SDK has a new [`createAI`](/agents/models/) function for the [AI SDK](/agents/models/ai-sdk/) and [Pi AI](/agents/models/pi-ai/). It unifies what `workers-ai-provider` and `ai-gateway-provider` did: Workers AI models, AI Gateway catalog models, dynamic routes, and vendor models, through the `AI` binding or an account id and API token.

`workers-ai-provider` and `ai-gateway-provider` are now feature frozen and marked as deprecated. Please migrate at your convenience.

```sh
npm i agents
```

<TypeScriptExample>

```ts
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", {
fallback: ["@cf/zai-org/glm-4.7-flash"],
}),
prompt: "Write a haiku about Durable Objects",
});

return result.toTextStreamResponse();
},
};
```

</TypeScriptExample>

With `createAI` you get:

- **Every model by id.** Workers AI `@cf/` ids, any model in the [AI Gateway catalog](/ai/models/), and [dynamic routes](/ai-gateway/features/dynamic-routing/) as `dynamic/<route>`.
- **Vendor model objects.** Pass a model from the vendor's own package, such as `ai(anthropic("claude-opus-5-5"))`. It goes unchanged to AI Gateway's [provider-native endpoint](/ai-gateway/usage/providers/).
- **The binding, or an account id and API token.** Use a gateway in another account, or call from outside a Worker.
- **Gateway options, fallback, and BYOK.** Caching, logging, metadata, retries, `byokAlias`, `zdr`, and `vendorKey`.
- **Stream resume.** Through the binding, a catalog or dynamic route stream that drops mid-answer resumes.
- **Other model types.** Embeddings, images, speech, transcription, and reranking in the AI SDK provider, and images in Pi AI.

| Before | After |
| ------------------------------------------- | ----------------------------------------------------- |
| `createWorkersAI({ binding })` | `createAI({ binding })` |
| `createWorkersAI({ accountId, apiKey })` | `createAI({ accountId, apiToken })` |
| `createAiGateway(...)` | `createAI(...)`, with `fallback` for a list of models |
| `createAISearch` from `workers-ai-provider` | `createAISearchNamespace` from `ai-search-provider` |

For AI Search, use [`ai-search-provider`](/ai-search/agent-sdks/ai-sdk/). For more information, refer to [Models](/agents/models/).
8 changes: 4 additions & 4 deletions src/content/docs/agent-memory/get-started.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ cd memory-agent

Install the dependencies used by this guide:

<PackageManagers pkg="agents ai workers-ai-provider" />
<PackageManagers pkg="agents ai @ai-sdk/provider" />

## 2. Create a namespace

Expand Down Expand Up @@ -183,7 +183,7 @@ Change the `agents` import and add the AI SDK imports. Keep the `Session` import
import { Agent, getAgentByName, routeAgentRequest } from "agents";
import { convertToModelMessages, generateText, stepCountIs } from "ai";
import type { UIMessage } from "ai";
import { createWorkersAI } from "workers-ai-provider";
import { createAI } from "agents/models/ai-sdk";
```

</TypeScriptExample>
Expand Down Expand Up @@ -218,9 +218,9 @@ export class ChatAgent extends Agent<Env, ChatAgentState> {

await this.scheduleIngest();

const workersai = createWorkersAI({ binding: this.env.AI });
const ai = createAI({ binding: this.env.AI });
const result = await generateText({
model: workersai("@cf/zai-org/glm-4.7-flash"),
model: ai("@cf/zai-org/glm-4.7-flash"),
system: await this.session.freezeSystemPrompt(),
messages: await convertToModelMessages(
(await this.session.getHistory()) as UIMessage[],
Expand Down
Loading
Loading