Cloudflare Workers AI
Status: Production Ready ✅
Last Updated: 2025-11-25
Dependencies: cloudflare-worker-base (for Worker setup)
Latest Versions: wrangler@4.50.0, @cloudflare/workers-types@4.20251125.0
Recent Updates (2025):
- April 2025 - Performance: Llama 3.3 70B 2-4x faster (speculative decoding, prefix caching), BGE embeddings 2x faster
- April 2025 - Breaking Changes: max_tokens now correctly defaults to 256 (was not respected), BGE pooling parameter (cls NOT backwards compatible with mean)
- 2025 - New Models (14): Mistral 3.1 24B (vision+tools), Gemma 3 12B (128K context), EmbeddingGemma 300M, Llama 4 Scout, GPT-OSS 120B/20B, Qwen models (QwQ 32B, Coder 32B), Leonardo image gen, Deepgram Aura 2, Whisper v3 Turbo, IBM Granite, Nova 3
- 2025 - Platform: Context windows API change (tokens not chars), unit-based pricing with per-model granularity, workers-ai-provider v2.0.0 (AI SDK v5), LoRA rank up to 32 (was 8), 100 adapters per account
- October 2025: Model deprecations (use Llama 4, GPT-OSS instead)
Quick Start (5 Minutes)
// 1. Add AI binding to wrangler.jsonc
{ "ai": { "binding": "AI" } }
// 2. Run model with streaming (recommended)
export default {
async fetch(request: Request, env: Env): Promise<Response> {
const stream = await env.AI.run('@cf/meta/llama-3.1-8b-instruct', {
messages: [{ role: 'user', content: 'Tell me a story' }],
stream: true, // Always stream for text generation!
});
return new Response(stream, {
headers: { 'content-type': 'text/event-stream' },
});
},
};
Why streaming? Prevents buffering in memory, faster time-to-first-token, avoids Worker timeout issues.
API Reference
env.AI.run(
model: string,
inputs: ModelInputs,
options?: { gateway?: { id: string; skipCache?: boolean } }
): Promise<ModelOutput | ReadableStream>
Model Selection Guide (Updated 2025)
Text Generation (LLMs)
| Model |
Best For |
Rate Limit |
Size |
Notes |
| 2025 Models |
|
|
|
|
@cf/meta/llama-4-scout-17b-16e-instruct |
Latest Llama, general purpose |
300/min |
17B |
NEW 2025 |
@cf/openai/gpt-oss-120b |
Largest open-source GPT |
300/min |
120B |
NEW 2025 |
@cf/openai/gpt-oss-20b |
Smaller open-source GPT |
300/min |
20B |
NEW 2025 |
@cf/google/gemma-3-12b-it |
128K context, 140+ languages |
300/min |
12B |
NEW 2025, vision |
@cf/mistralai/mistral-small-3.1-24b-instruct |
Vision + tool calling |
300/min |
24B |
NEW 2025 |
@cf/qwen/qwq-32b |
Reasoning, complex tasks |
300/min |
32B |
NEW 2025 |
@cf/qwen/qwen2.5-coder-32b-instruct |
Coding specialist |
300/min |
32B |
NEW 2025 |
@cf/qwen/qwen3-30b-a3b-fp8 |
Fast quantized |
300/min |
30B |
NEW 2025 |
@cf/ibm-granite/granite-4.0-h-micro |
Small, efficient |
300/min |
Micro |
NEW 2025 |
| Performance (2025) |
|
|
|
|
@cf/meta/llama-3.3-70b-instruct-fp8-fast |
2-4x faster (2025 update) |
300/min |
70B |
Speculative decoding |
@cf/meta/llama-3.1-8b-instruct-fp8-fast |
Fast 8B variant |
300/min |
8B |
- |
| Standard Models |
|
|
|
|
@cf/meta/llama-3.1-8b-instruct |
General purpose |
300/min |
8B |
- |
@cf/meta/llama-3.2-1b-instruct |
Ultra-fast, simple tasks |
300/min |
1B |
- |
@cf/deepseek-ai/deepseek-r1-distill-qwen-32b |
Coding, technical |
300/min |
32B |
- |
Text Embeddings (2x Faster - 2025)
| Model |
Dimensions |
Best For |
Rate Limit |
Notes |
@cf/google/embeddinggemma-300m |
768 |
Best-in-class RAG |
3000/min |
NEW 2025 |
@cf/baai/bge-base-en-v1.5 |
768 |
General RAG (2x faster) |
3000/min |
pooling: "cls" recommended |
@cf/baai/bge-large-en-v1.5 |
1024 |
High accuracy (2x faster) |
1500/min |
pooling: "cls" recommended |
@cf/baai/bge-small-en-v1.5 |
384 |
Fast, low storage (2x faster) |
3000/min |
pooling: "cls" recommended |
@cf/qwen/qwen3-embedding-0.6b |
768 |
Qwen embeddings |
3000/min |
NEW 2025 |
CRITICAL (2025): BGE models now support pooling: "cls" parameter (recommended) but NOT backwards compatible with pooling: "mean" (default).
Image Generation
| Model |
Best For |
Rate Limit |
Notes |
@cf/black-forest-labs/flux-1-schnell |
High quality, photorealistic |
720/min |
- |
@cf/leonardo/lucid-origin |
Leonardo AI style |
720/min |
NEW 2025 |
@cf/leonardo/phoenix-1.0 |
Leonardo AI variant |
720/min |
NEW 2025 |
@cf/stabilityai/stable-diffusion-xl-base-1.0 |
General purpose |
720/min |
- |
Vision Models
| Model |
Best For |
Rate Limit |
Notes |
@cf/meta/llama-3.2-11b-vision-instruct |
Image understanding |
720/min |
- |
@cf/google/gemma-3-12b-it |
Vision + text (128K context) |
300/min |
NEW 2025 |
Audio Models (2025)
| Model |
Type |
Rate Limit |
Notes |
@cf/deepgram/aura-2-en |
Text-to-speech (English) |
720/min |
NEW 2025 |
@cf/deepgram/aura-2-es |
Text-to-speech (Spanish) |
720/min |
NEW 2025 |
@cf/deepgram/nova-3 |
Speech-to-text (+ WebSocket) |
720/min |
NEW 2025 |
@cf/openai/whisper-large-v3-turbo |
Speech-to-text (faster) |
720/min |
NEW 2025 |
Common Patterns
RAG (Retrieval Augmented Generation)
// 1. Generate embeddings
const embeddings = await env.AI.run('@cf/baai/bge-base-en-v1.5', { text: [userQuery] });
// 2. Search Vectorize
const matches = await env.VECTORIZE.query(embeddings.data[0], { topK: 3 });
const context = matches.matches.map((m) => m.metadata.text).join('\n\n');
// 3. Generate with context
const response = await env.AI.run('@cf/meta/llama-3.1-8b-instruct', {
messages: [
{ role: 'system', content: `Answer using this context:\n${context}` },
{ role: 'user', content: userQuery },
],
stream: true,
});
Structured Output with Zod
import { z } from 'zod';
const Schema = z.object({ name: z.string(), items: z.array(z.string()) });
const response = await env.AI.run('@cf/meta/llama-3.1-8b-instruct', {
messages: [{
role: 'user',
content: `Generate JSON matching: ${JSON.stringify(Schema.shape)}`
}],
});
const validated = Schema.parse(JSON.parse(response.response));
AI Gateway Integration
Provides caching, logging, cost tracking, and analytics for AI requests.
const response = await env.AI.run(
'@cf/meta/llama-3.1-8b-instruct',
{ prompt: 'Hello' },
{ gateway: { id: 'my-gateway', skipCache: false } }
);
// Access logs and send feedback
const gateway = env.AI.gateway('my-gateway');
await gateway.patchLog(env.AI.aiGatewayLogId, {
feedback: { rating: 1, comment: 'Great response' },
});
Benefits: Cost tracking, caching (reduces duplicate inference), logging, rate limiting, analytics.
Rate Limits & Pricing (Updated 2025)
Rate Limits (per minute)
| Task Type |
Default Limit |
Notes |
| Text Generation |
300/min |
Some fast models: 400-1500/min |
| Text Embeddings |
3000/min |
BGE-large: 1500/min |
| Image Generation |
720/min |
All image models |
| Vision Models |
720/min |
Image understanding |
| Audio (TTS/STT) |
720/min |
Deepgram, Whisper |
| Translation |
720/min |
M2M100, Opus MT |
| Classification |
2000/min |
Text classification |
Pricing (Unit-Based, Billed in Neurons - 2025)
Free Tier:
- 10,000 neurons per day
- Resets daily at 00:00 UTC
Paid Tier ($0.011 per 1,000 neurons):
- 10,000 neurons/day included
- Unlimited usage above free allocation
2025 Model Costs (per 1M tokens):
| Model |
Input |
Output |
Notes |
| 2025 Models |
|
|
|
| Llama 4 Scout 17B |
$0.270 |
$0.850 |
NEW 2025 |
| GPT-OSS 120B |
$0.350 |
$0.750 |
NEW 2025 |
| GPT-OSS 20B |
$0.200 |
$0.300 |
NEW 2025 |
| Gemma 3 12B |
$0.345 |
$0.556 |
NEW 2025 |
| Mistral 3.1 24B |
$0.351 |
$0.555 |
NEW 2025 |
| Qwen QwQ 32B |
$0.660 |
$1.000 |
NEW 2025 |
| Qwen Coder 32B |
$0.660 |
$1.000 |
NEW 2025 |
| IBM Granite Micro |
$0.017 |
$0.112 |
NEW 2025 |
| EmbeddingGemma 300M |
$0.012 |
N/A |
NEW 2025 |
| Qwen3 Embedding 0.6B |
$0.012 |
N/A |
NEW 2025 |
| Performance (2025) |
|
|
|
| Llama 3.3 70B Fast |
$0.293 |
$2.253 |
2-4x faster |
| Llama 3.1 8B FP8 Fast |
$0.045 |
$0.384 |
Fast variant |
| Standard Models |
|
|
|
| Llama 3.2 1B |
$0.027 |
$0.201 |
- |
| Llama 3.1 8B |
$0.282 |
$0.827 |
- |
| Deepseek R1 32B |
$0.497 |
$4.881 |
- |
| BGE-base (2x faster) |
$0.067 |
N/A |
2025 speedup |
| BGE-large (2x faster) |
$0.204 |
N/A |
2025 speedup |
| Image Models (2025) |
|
|
|
| Flux 1 Schnell |
$0.0000528 per 512x512 tile |
- |
|
| Leonardo Lucid |
$0.006996 per 512x512 tile |
NEW 2025 |
|
| Leonardo Phoenix |
$0.005830 per 512x512 tile |
NEW 2025 |
|
| Audio Models (2025) |
|
|
|
| Deepgram Aura 2 |
$0.030 per 1k chars |
NEW 2025 |
|
| Deepgram Nova 3 |
$0.0052 per audio min |
NEW 2025 |
|
| Whisper v3 Turbo |
$0.0005 per audio min |
NEW 2025 |
|
Error Handling with Retry
async function runAIWithRetry(
env: Env,
model: string,
inputs: any,
maxRetries = 3
): Promise<any> {
let lastError: Error;
for (let i = 0; i < maxRetries; i++) {
try {
return await env.AI.run(model, inputs);
} catch (error) {
lastError = error as Error;
// Rate limit - retry with exponential backoff
if (lastError.message.toLowerCase().includes('rate limit')) {
await new Promise((resolve) => setTimeout(resolve, Math.pow(2, i) * 1000));
continue;
}
throw error; // Other errors - fail immediately
}
}
throw lastError!;
}
OpenAI Compatibility
import OpenAI from 'openai';
const openai = new OpenAI({
apiKey: env.CLOUDFLARE_API_KEY,
baseURL: `https://api.cloudflare.com/client/v4/accounts/${env.ACCOUNT_ID}/ai/v1`,
});
// Chat completions
await openai.chat.completions.create({
model: '@cf/meta/llama-3.1-8b-instruct',
messages: [{ role: 'user', content: 'Hello!' }],
});
Endpoints: /v1/chat/completions, /v1/embeddings
Vercel AI SDK Integration (workers-ai-provider v2.0.0)
import { createWorkersAI } from 'workers-ai-provider'; // v2.0.0 with AI SDK v5
import { generateText, streamText } from 'ai';
const workersai = createWorkersAI({ binding: env.AI });
// Generate or stream
await generateText({
model: workersai('@cf/meta/llama-3.1-8b-instruct'),
prompt: 'Write a poem',
});
References
1---2name: cloudflare-workers-ai-33description: Run LLMs and AI models on Cloudflare's global GPU network with Workers AI. Includes Llama 4, Gemma 3, Mistral 3.1, Flux image generation, BGE embeddings (2x faster, 2025), streaming support, and AI Gateway for cost tracking. Use when: implementing LLM inference, generating images, building RAG with embeddings, streaming AI responses, using AI Gateway, troubleshooting max_tokens defaults (breaking change 2025), BGE pooling parameter (not backwards compatible), or handling AI_ERROR, rate limits, model deprecations, token limits. Keywords: workers ai, cloudflare ai, ai bindings, llm workers, @cf/meta/llama-4-scout, @cf/google/gemma-3-12b-it, @cf/mistralai/mistral-small-3.1-24b-instruct, @cf/openai/gpt-oss-120b, workers ai models, ai inference, cloudflare llm, ai streaming, text generation ai, ai embeddings, bge pooling cls mean, image generation ai, workers ai rag, ai gateway, llama workers, flux image generation, deepgram aura, leonardo image generation, vision models ai, ai chat completion, AI_ERROR, rate limi4---5
6# Cloudflare Workers AI
7
8**Status**: Production Ready ✅
9**Last Updated**: 2025-11-25
10**Dependencies**: cloudflare-worker-base (for Worker setup)
11**Latest Versions**: wrangler@4.50.0, @cloudflare/workers-types@4.20251125.0
12
13**Recent Updates (2025)**:
14- **April 2025 - Performance**: Llama 3.3 70B 2-4x faster (speculative decoding, prefix caching), BGE embeddings 2x faster
15- **April 2025 - Breaking Changes**: max_tokens now correctly defaults to 256 (was not respected), BGE pooling parameter (cls NOT backwards compatible with mean)
16- **2025 - New Models (14)**: Mistral 3.1 24B (vision+tools), Gemma 3 12B (128K context), EmbeddingGemma 300M, Llama 4 Scout, GPT-OSS 120B/20B, Qwen models (QwQ 32B, Coder 32B), Leonardo image gen, Deepgram Aura 2, Whisper v3 Turbo, IBM Granite, Nova 3
17- **2025 - Platform**: Context windows API change (tokens not chars), unit-based pricing with per-model granularity, workers-ai-provider v2.0.0 (AI SDK v5), LoRA rank up to 32 (was 8), 100 adapters per account
18- **October 2025**: Model deprecations (use Llama 4, GPT-OSS instead)
19
20---
21
22## Quick Start (5 Minutes)
23
24```typescript
25// 1. Add AI binding to wrangler.jsonc
26{ "ai": { "binding": "AI" } }
27
28// 2. Run model with streaming (recommended)
29export default {
30 async fetch(request: Request, env: Env): Promise<Response> {
31 const stream = await env.AI.run('@cf/meta/llama-3.1-8b-instruct', {
32 messages: [{ role: 'user', content: 'Tell me a story' }],
33 stream: true, // Always stream for text generation!
34 });
35
36 return new Response(stream, {
37 headers: { 'content-type': 'text/event-stream' },
38 });
39 },
40};
41```
42
43**Why streaming?** Prevents buffering in memory, faster time-to-first-token, avoids Worker timeout issues.
44
45---
46
47## API Reference
48
49```typescript
50env.AI.run(
51 model: string,
52 inputs: ModelInputs,
53 options?: { gateway?: { id: string; skipCache?: boolean } }
54): Promise<ModelOutput | ReadableStream>
55```
56
57---
58
59## Model Selection Guide (Updated 2025)
60
61### Text Generation (LLMs)
62
63| Model | Best For | Rate Limit | Size | Notes |
64|-------|----------|------------|------|-------|
65| **2025 Models** |
66| `@cf/meta/llama-4-scout-17b-16e-instruct` | Latest Llama, general purpose | 300/min | 17B | NEW 2025 |
67| `@cf/openai/gpt-oss-120b` | Largest open-source GPT | 300/min | 120B | NEW 2025 |
68| `@cf/openai/gpt-oss-20b` | Smaller open-source GPT | 300/min | 20B | NEW 2025 |
69| `@cf/google/gemma-3-12b-it` | 128K context, 140+ languages | 300/min | 12B | NEW 2025, vision |
70| `@cf/mistralai/mistral-small-3.1-24b-instruct` | Vision + tool calling | 300/min | 24B | NEW 2025 |
71| `@cf/qwen/qwq-32b` | Reasoning, complex tasks | 300/min | 32B | NEW 2025 |
72| `@cf/qwen/qwen2.5-coder-32b-instruct` | Coding specialist | 300/min | 32B | NEW 2025 |
73| `@cf/qwen/qwen3-30b-a3b-fp8` | Fast quantized | 300/min | 30B | NEW 2025 |
74| `@cf/ibm-granite/granite-4.0-h-micro` | Small, efficient | 300/min | Micro | NEW 2025 |
75| **Performance (2025)** |
76| `@cf/meta/llama-3.3-70b-instruct-fp8-fast` | 2-4x faster (2025 update) | 300/min | 70B | Speculative decoding |
77| `@cf/meta/llama-3.1-8b-instruct-fp8-fast` | Fast 8B variant | 300/min | 8B | - |
78| **Standard Models** |
79| `@cf/meta/llama-3.1-8b-instruct` | General purpose | 300/min | 8B | - |
80| `@cf/meta/llama-3.2-1b-instruct` | Ultra-fast, simple tasks | 300/min | 1B | - |
81| `@cf/deepseek-ai/deepseek-r1-distill-qwen-32b` | Coding, technical | 300/min | 32B | - |
82
83### Text Embeddings (2x Faster - 2025)
84
85| Model | Dimensions | Best For | Rate Limit | Notes |
86|-------|-----------|----------|------------|-------|
87| `@cf/google/embeddinggemma-300m` | 768 | Best-in-class RAG | 3000/min | **NEW 2025** |
88| `@cf/baai/bge-base-en-v1.5` | 768 | General RAG (2x faster) | 3000/min | **pooling: "cls"** recommended |
89| `@cf/baai/bge-large-en-v1.5` | 1024 | High accuracy (2x faster) | 1500/min | **pooling: "cls"** recommended |
90| `@cf/baai/bge-small-en-v1.5` | 384 | Fast, low storage (2x faster) | 3000/min | **pooling: "cls"** recommended |
91| `@cf/qwen/qwen3-embedding-0.6b` | 768 | Qwen embeddings | 3000/min | NEW 2025 |
92
93**CRITICAL (2025)**: BGE models now support `pooling: "cls"` parameter (recommended) but NOT backwards compatible with `pooling: "mean"` (default).
94
95### Image Generation
96
97| Model | Best For | Rate Limit | Notes |
98|-------|----------|------------|-------|
99| `@cf/black-forest-labs/flux-1-schnell` | High quality, photorealistic | 720/min | - |
100| `@cf/leonardo/lucid-origin` | Leonardo AI style | 720/min | NEW 2025 |
101| `@cf/leonardo/phoenix-1.0` | Leonardo AI variant | 720/min | NEW 2025 |
102| `@cf/stabilityai/stable-diffusion-xl-base-1.0` | General purpose | 720/min | - |
103
104### Vision Models
105
106| Model | Best For | Rate Limit | Notes |
107|-------|----------|------------|-------|
108| `@cf/meta/llama-3.2-11b-vision-instruct` | Image understanding | 720/min | - |
109| `@cf/google/gemma-3-12b-it` | Vision + text (128K context) | 300/min | NEW 2025 |
110
111### Audio Models (2025)
112
113| Model | Type | Rate Limit | Notes |
114|-------|------|------------|-------|
115| `@cf/deepgram/aura-2-en` | Text-to-speech (English) | 720/min | NEW 2025 |
116| `@cf/deepgram/aura-2-es` | Text-to-speech (Spanish) | 720/min | NEW 2025 |
117| `@cf/deepgram/nova-3` | Speech-to-text (+ WebSocket) | 720/min | NEW 2025 |
118| `@cf/openai/whisper-large-v3-turbo` | Speech-to-text (faster) | 720/min | NEW 2025 |
119
120---
121
122## Common Patterns
123
124### RAG (Retrieval Augmented Generation)
125
126```typescript
127// 1. Generate embeddings
128const embeddings = await env.AI.run('@cf/baai/bge-base-en-v1.5', { text: [userQuery] });
129
130// 2. Search Vectorize
131const matches = await env.VECTORIZE.query(embeddings.data[0], { topK: 3 });
132const context = matches.matches.map((m) => m.metadata.text).join('\n\n');
133
134// 3. Generate with context
135const response = await env.AI.run('@cf/meta/llama-3.1-8b-instruct', {
136 messages: [
137 { role: 'system', content: `Answer using this context:\n${context}` },
138 { role: 'user', content: userQuery },
139 ],
140 stream: true,
141});
142```
143
144---
145
146### Structured Output with Zod
147
148```typescript
149import { z } from 'zod';
150
151const Schema = z.object({ name: z.string(), items: z.array(z.string()) });
152
153const response = await env.AI.run('@cf/meta/llama-3.1-8b-instruct', {
154 messages: [{
155 role: 'user',
156 content: `Generate JSON matching: ${JSON.stringify(Schema.shape)}`
157 }],
158});
159
160const validated = Schema.parse(JSON.parse(response.response));
161```
162
163---
164
165## AI Gateway Integration
166
167Provides caching, logging, cost tracking, and analytics for AI requests.
168
169```typescript
170const response = await env.AI.run(
171 '@cf/meta/llama-3.1-8b-instruct',
172 { prompt: 'Hello' },
173 { gateway: { id: 'my-gateway', skipCache: false } }
174);
175
176// Access logs and send feedback
177const gateway = env.AI.gateway('my-gateway');
178await gateway.patchLog(env.AI.aiGatewayLogId, {
179 feedback: { rating: 1, comment: 'Great response' },
180});
181```
182
183**Benefits:** Cost tracking, caching (reduces duplicate inference), logging, rate limiting, analytics.
184
185---
186
187## Rate Limits & Pricing (Updated 2025)
188
189### Rate Limits (per minute)
190
191| Task Type | Default Limit | Notes |
192|-----------|---------------|-------|
193| **Text Generation** | 300/min | Some fast models: 400-1500/min |
194| **Text Embeddings** | 3000/min | BGE-large: 1500/min |
195| **Image Generation** | 720/min | All image models |
196| **Vision Models** | 720/min | Image understanding |
197| **Audio (TTS/STT)** | 720/min | Deepgram, Whisper |
198| **Translation** | 720/min | M2M100, Opus MT |
199| **Classification** | 2000/min | Text classification |
200
201### Pricing (Unit-Based, Billed in Neurons - 2025)
202
203**Free Tier:**
204- 10,000 neurons per day
205- Resets daily at 00:00 UTC
206
207**Paid Tier ($0.011 per 1,000 neurons):**
208- 10,000 neurons/day included
209- Unlimited usage above free allocation
210
211**2025 Model Costs (per 1M tokens):**
212
213| Model | Input | Output | Notes |
214|-------|-------|--------|-------|
215| **2025 Models** |
216| Llama 4 Scout 17B | $0.270 | $0.850 | NEW 2025 |
217| GPT-OSS 120B | $0.350 | $0.750 | NEW 2025 |
218| GPT-OSS 20B | $0.200 | $0.300 | NEW 2025 |
219| Gemma 3 12B | $0.345 | $0.556 | NEW 2025 |
220| Mistral 3.1 24B | $0.351 | $0.555 | NEW 2025 |
221| Qwen QwQ 32B | $0.660 | $1.000 | NEW 2025 |
222| Qwen Coder 32B | $0.660 | $1.000 | NEW 2025 |
223| IBM Granite Micro | $0.017 | $0.112 | NEW 2025 |
224| EmbeddingGemma 300M | $0.012 | N/A | NEW 2025 |
225| Qwen3 Embedding 0.6B | $0.012 | N/A | NEW 2025 |
226| **Performance (2025)** |
227| Llama 3.3 70B Fast | $0.293 | $2.253 | 2-4x faster |
228| Llama 3.1 8B FP8 Fast | $0.045 | $0.384 | Fast variant |
229| **Standard Models** |
230| Llama 3.2 1B | $0.027 | $0.201 | - |
231| Llama 3.1 8B | $0.282 | $0.827 | - |
232| Deepseek R1 32B | $0.497 | $4.881 | - |
233| BGE-base (2x faster) | $0.067 | N/A | 2025 speedup |
234| BGE-large (2x faster) | $0.204 | N/A | 2025 speedup |
235| **Image Models (2025)** |
236| Flux 1 Schnell | $0.0000528 per 512x512 tile | - |
237| Leonardo Lucid | $0.006996 per 512x512 tile | NEW 2025 |
238| Leonardo Phoenix | $0.005830 per 512x512 tile | NEW 2025 |
239| **Audio Models (2025)** |
240| Deepgram Aura 2 | $0.030 per 1k chars | NEW 2025 |
241| Deepgram Nova 3 | $0.0052 per audio min | NEW 2025 |
242| Whisper v3 Turbo | $0.0005 per audio min | NEW 2025 |
243
244---
245
246## Error Handling with Retry
247
248```typescript
249async function runAIWithRetry(
250 env: Env,
251 model: string,
252 inputs: any,
253 maxRetries = 3
254): Promise<any> {
255 let lastError: Error;
256
257 for (let i = 0; i < maxRetries; i++) {
258 try {
259 return await env.AI.run(model, inputs);
260 } catch (error) {
261 lastError = error as Error;
262
263 // Rate limit - retry with exponential backoff
264 if (lastError.message.toLowerCase().includes('rate limit')) {
265 await new Promise((resolve) => setTimeout(resolve, Math.pow(2, i) * 1000));
266 continue;
267 }
268
269 throw error; // Other errors - fail immediately
270 }
271 }
272
273 throw lastError!;
274}
275```
276
277---
278
279## OpenAI Compatibility
280
281```typescript
282import OpenAI from 'openai';
283
284const openai = new OpenAI({
285 apiKey: env.CLOUDFLARE_API_KEY,
286 baseURL: `https://api.cloudflare.com/client/v4/accounts/${env.ACCOUNT_ID}/ai/v1`,
287});
288
289// Chat completions
290await openai.chat.completions.create({
291 model: '@cf/meta/llama-3.1-8b-instruct',
292 messages: [{ role: 'user', content: 'Hello!' }],
293});
294```
295
296**Endpoints:** `/v1/chat/completions`, `/v1/embeddings`
297
298---
299
300## Vercel AI SDK Integration (workers-ai-provider v2.0.0)
301
302```typescript
303import { createWorkersAI } from 'workers-ai-provider'; // v2.0.0 with AI SDK v5
304import { generateText, streamText } from 'ai';
305
306const workersai = createWorkersAI({ binding: env.AI });
307
308// Generate or stream
309await generateText({
310 model: workersai('@cf/meta/llama-3.1-8b-instruct'),
311 prompt: 'Write a poem',
312});
313```
314
315---
316
317## References
318
319- [Workers AI Docs](https://developers.cloudflare.com/workers-ai/)
320- [Models Catalog](https://developers.cloudflare.com/workers-ai/models/)
321- [AI Gateway](https://developers.cloudflare.com/ai-gateway/)
322- [Pricing](https://developers.cloudflare.com/workers-ai/platform/pricing/)
323- [Changelog](https://developers.cloudflare.com/workers-ai/changelog/)
324- [LoRA Adapters](https://developers.cloudflare.com/workers-ai/features/fine-tunes/loras/)
325- **MCP Tool**: Use `mcp__cloudflare-docs__search_cloudflare_documentation` for latest docs