智能体
developers.cloudflare.com ·
@Cloudflare Developers
Cloudflare Agents SDK 新增 createAI,统一 Workers AI 与 AI Gateway 接入
Cloudflare 在 Agents SDK 中推出 createAI 函数,统一原先 workers-ai-provider 和 ai-gateway-provider 的能力,支持 Workers AI 模型、AI Gateway 目录模型、动态路由和厂商模型对象,并可通过 binding 或账号 ID 加 API token 调用;旧包已冻结并标记弃用,官方给出迁移对照表与示例代码。
AI推荐理由 Cloudflare 官方 changelog 给出明确的 API 变更:Agents SDK 新增 createAI,统一 workers-ai-provider 与 ai-gateway-provider,旧包冻结弃用,并附迁移对照表和代码示例,对使用 Workers AI/AI Gateway 的开发者有直接迁移价值;局限是仅为厂商文档更新,无性能或效果数据。
78
正文
The Agents SDK has a new [`createAI`](https://developers.cloudflare.com/agents/models/) function for the [AI SDK](https://developers.cloudflare.com/agents/models/ai-sdk/) and [Pi AI](https://developers.cloudflare.com/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.
```
npm i agents
```
```
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", {
fallback: ["@cf/zai-org/glm-4.7-flash"],
}),
prompt: "Write a haiku about Durable Objects",
});
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", {
fallback: ["@cf/zai-org/glm-4.7-flash"],
}),
prompt: "Write a haiku about Durable Objects",
});
return result.toTextStreamResponse();
},
};
```
With `createAI` you get:
* **Every model by id.** Workers AI `@cf/` ids, any model in the [AI Gateway catalog](https://developers.cloudflare.com/ai/models/), and [dynamic routes](https://developers.cloudflare.com/ai-gateway/features/dynamic-routing/) as `dynamic/`.
* **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](https://developers.cloudflare.com/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`](https://developers.cloudflare.com/ai-search/agent-sdks/ai-sdk/). For more information, refer to [Models](https://developers.cloudflare.com/agents/models/).