The Agents SDK includes model providers for Workers AI and AI Gateway. Each provider is one createAI factory over the Worker's AI binding, or an account id and API token. There are two, one per framework:
| Import | Framework | Use it with |
|---|---|---|
agents/models/ai-sdk |
AI SDK ↗︎ v7 | generateText, streamText, Think, and any AI SDK consumer |
agents/models/pi-ai |
Pi AI ↗︎ | The Pi harness, and Pi AI's stream |
Both take the same options, route through the same gateway, and handle Workers AI the same way. The OpenCode harness has a third, agents/models/opencode, built on the AI SDK provider.
Name a model with a string, or pass a model object built by the vendor's own package:
ai("@cf/zai-org/glm-4.7-flash"); // Workers AI
ai("anthropic/claude-opus-5.5"); // AI Gateway catalog
ai("dynamic/support"); // AI Gateway dynamic route
ai(anthropic("claude-opus-5-5")); // the vendor's own model object- A Workers AI model is a
@cf/id. It runs throughenv.AI.run(). A compatibility layer turns each model's response into the standard OpenAI chat completions shape. - An AI Gateway catalog model is an
<author>/<model>id from the model catalog, such asanthropic/claude-opus-5.5oropenai/gpt-5.5. It also runs throughenv.AI.run(), which sends it to AI Gateway. The gateway picks a provider and bills the call. Use the catalog's spelling, not the vendor's:anthropic/claude-opus-5.5, notclaude-opus-5-5. - A dynamic route is
dynamic/<route>. The route you configured on the gateway picks the model. - A model object comes from the vendor's provider package, such as
anthropic("claude-opus-5-5"). The vendor's code builds the request and parses the response. The provider sends that request unchanged through the binding to AI Gateway's provider-native endpoint for that vendor.
The providers do not use the deprecated Universal Endpoint.
- No vendor keys. AI Gateway holds third-party credentials, through Unified Billing or a key you store on the gateway. Unified Billing requires authentication on the gateway.
- Gateway options. Caching, logging, metadata, timeouts, retries, and BYOK key aliases are options on the provider, the model, or the call. The
defaultgateway is created the first time you use it. - Fallback. List models to try in order if the first fails before it produces output. Mix Workers AI ids, catalog ids, dynamic routes, and model objects.
- Stream resume. Through the binding, a stream from a catalog model or a dynamic route resumes after a dropped connection. You do not need to configure anything.
- Gateway metadata on every result. Each response carries the gateway log id, cache status, run id, and the model that actually answered.
createAI from agents/models/ai-sdk replaces workers-ai-provider and ai-gateway-provider. For AI Search, use ai-search-provider.
Inside a Worker, pass the AI binding, createAI({ binding: env.AI }), and add it to your Wrangler configuration:
{
"ai": {
"binding": "AI",
},
}[ai]
binding = "AI"To use a gateway in another account, or to call from outside a Worker, pass an account id and API token instead. Create the token with Workers AI Read and AI Gateway Run:
const ai = createAI({
accountId: "<ACCOUNT_ID>",
apiToken: env.CLOUDFLARE_API_TOKEN,
gateway: "my-gateway",
});