Skip to main content

Cloudflare Workers AI logoCloudflare Workers AI

Access 27 Cloudflare Workers AI models through Mastra's model router. Authentication is handled automatically using the CLOUDFLARE_API_KEY environment variable. Configure CLOUDFLARE_ACCOUNT_ID as well.

Learn more in the Cloudflare Workers AI documentation.

.env
CLOUDFLARE_ACCOUNT_ID=your-account-id
CLOUDFLARE_API_KEY=your-api-key
src/mastra/agents/my-agent.ts
import { Agent } from "@mastra/core/agent";

const agent = new Agent({
id: "my-agent",
name: "My Agent",
instructions: "You are a helpful assistant",
model: "cloudflare-workers-ai/@cf/aisingapore/gemma-sea-lion-v4-27b-it"
});

// Generate a response
const response = await agent.generate("Hello!");

// Stream a response
const stream = await agent.stream("Tell me a story");
for await (const chunk of stream) {
console.log(chunk);
}
info

Mastra uses the OpenAI-compatible /chat/completions endpoint. Some provider-specific features may not be available. Check the Cloudflare Workers AI documentation for details.

Models
Direct link to Models

ModelContextToolsReasoningImageAudioVideoInput $/1MOutput $/1M
cloudflare-workers-ai/@cf/aisingapore/gemma-sea-lion-v4-27b-it128K$0.35$0.56
cloudflare-workers-ai/@cf/deepseek-ai/deepseek-r1-distill-qwen-32b80K$0.50$5
cloudflare-workers-ai/@cf/google/gemma-3-12b-it80K$0.34$0.56
cloudflare-workers-ai/@cf/google/gemma-4-26b-a4b-it256K$0.10$0.30
cloudflare-workers-ai/@cf/ibm-granite/granite-4.0-h-micro131K$0.02$0.11
cloudflare-workers-ai/@cf/meta/llama-2-7b-chat-fp164K$0.56$7
cloudflare-workers-ai/@cf/meta/llama-3-8b-instruct8K$0.28$0.83
cloudflare-workers-ai/@cf/meta/llama-3-8b-instruct-awq8K$0.12$0.27
cloudflare-workers-ai/@cf/meta/llama-3.1-8b-instruct-awq8K$0.12$0.27
cloudflare-workers-ai/@cf/meta/llama-3.1-8b-instruct-fp832K$0.15$0.29
cloudflare-workers-ai/@cf/meta/llama-3.2-11b-vision-instruct128K$0.05$0.68
cloudflare-workers-ai/@cf/meta/llama-3.2-1b-instruct60K$0.03$0.20
cloudflare-workers-ai/@cf/meta/llama-3.2-3b-instruct80K$0.05$0.34
cloudflare-workers-ai/@cf/meta/llama-3.3-70b-instruct-fp8-fast24K$0.29$2
cloudflare-workers-ai/@cf/meta/llama-4-scout-17b-16e-instruct131K$0.27$0.85
cloudflare-workers-ai/@cf/meta/llama-guard-3-8b131K$0.48$0.03
cloudflare-workers-ai/@cf/mistral/mistral-7b-instruct-v0.13K$0.11$0.19
cloudflare-workers-ai/@cf/mistralai/mistral-small-3.1-24b-instruct128K$0.35$0.56
cloudflare-workers-ai/@cf/moonshotai/kimi-k2.5256K$0.60$3
cloudflare-workers-ai/@cf/moonshotai/kimi-k2.6262K$0.95$4
cloudflare-workers-ai/@cf/nvidia/nemotron-3-120b-a12b256K$0.50$2
cloudflare-workers-ai/@cf/openai/gpt-oss-120b128K$0.35$0.75
cloudflare-workers-ai/@cf/openai/gpt-oss-20b128K$0.20$0.30
cloudflare-workers-ai/@cf/qwen/qwen2.5-coder-32b-instruct33K$0.66$1
cloudflare-workers-ai/@cf/qwen/qwen3-30b-a3b-fp833K$0.05$0.34
cloudflare-workers-ai/@cf/qwen/qwq-32b24K$0.66$1
cloudflare-workers-ai/@cf/zai-org/glm-4.7-flash131K$0.06$0.40
27 available models

Advanced configuration
Direct link to Advanced configuration

Custom headers
Direct link to Custom headers

src/mastra/agents/my-agent.ts
const agent = new Agent({
id: "custom-agent",
name: "custom-agent",
model: {
url: "https://api.cloudflare.com/client/v4/accounts/${CLOUDFLARE_ACCOUNT_ID}/ai/v1",
id: "cloudflare-workers-ai/@cf/aisingapore/gemma-sea-lion-v4-27b-it",
apiKey: process.env.CLOUDFLARE_API_KEY,
headers: {
"X-Custom-Header": "value"
}
}
});

Dynamic model selection
Direct link to Dynamic model selection

src/mastra/agents/my-agent.ts
const agent = new Agent({
id: "dynamic-agent",
name: "Dynamic Agent",
model: ({ requestContext }) => {
const useAdvanced = requestContext.task === "complex";
return useAdvanced
? "cloudflare-workers-ai/@cf/zai-org/glm-4.7-flash"
: "cloudflare-workers-ai/@cf/aisingapore/gemma-sea-lion-v4-27b-it";
}
});
On this page