Skip to content

Models

Last updated View as MarkdownAgent setup

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.

How a model is chosen

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 through env.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 as anthropic/claude-opus-5.5 or openai/gpt-5.5. It also runs through env.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, not claude-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.

What every model gets

  • 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 default gateway 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.

Connect to Cloudflare

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

Choose a provider

AI SDK

A full AI SDK ProviderV4 for text, tools, structured output, embeddings, images, speech, transcription, and reranking.

Pi AI

Pi AI models for the Pi harness, Pi Durable, and any framework built on a Pi AI Models registry.

Was this helpful?