AI
env.AI lets your app call OpenAI and Anthropic chat models, plus Cloudflare Workers AI. By default calls are proxied through Cloudrizz and billed to your plan's AI credits — no API keys to manage. Set your own provider key on the app and calls go direct, billed by the provider.
Chat completions
env.AI is an attachable resource. Ask your assistant to enable AI (it runs attach_resource) and the binding appears on your next deploy.The provider request body is passed straight through, so you use each provider's native shape — including the model field:
// api/ask.js
export default {
async fetch(request, env, ctx) {
const { question } = await request.json();
// OpenAI
const openai = await env.AI.openai.chat({
model: "gpt-4o",
messages: [{ role: "user", content: question }],
});
// Anthropic
const claude = await env.AI.anthropic.chat({
model: "claude-sonnet-4-6",
max_tokens: 1024,
messages: [{ role: "user", content: question }],
});
return Response.json({ openai, claude });
},
};env.AI.openai.chat(params)— OpenAI Chat Completions request body; returns the OpenAI response.env.AI.anthropic.chat(params)— Anthropic Messages request body; returns the Anthropic response.env.AI.run(model, input)— Cloudflare Workers AI (the native binding), for the models Workers AI hosts.
model the provider's API accepts. Cloudrizz doesn't pin or rewrite the model — you stay current with the provider.Managed credits vs your own key
Managed (default): with no provider key set, calls are proxied through Cloudrizz and charged against your plan's monthly AI credits. Nothing to configure.
Bring your own key: set OPENAI_API_KEY or ANTHROPIC_API_KEY as an app environment variable (via set_app_env) and that provider's calls go directly to the vendor on your own account — Cloudrizz doesn't bill them.
Errors & credits
A failed call throws an Error with status and code fields mirroring the provider's error. When you're on managed credits and the balance is relevant, the error also carries a balance field so you can surface “out of credits” cleanly. Free-tier apps have no managed credits — bring your own key, or see Limits & quotas.