$npx -y skills add jackspace/ClaudeSkillz --skill cloudflare-workers-aiComplete knowledge domain for Cloudflare Workers AI - Run AI models on serverless GPUs across Cloudflare's global network. Use when: implementing AI inference on Workers, running LLM models, generating text/images with AI, configuring Workers AI bindings, implementing AI streamin
| 1 | # Cloudflare Workers AI - Complete Reference |
| 2 | |
| 3 | Production-ready knowledge domain for building AI-powered applications with Cloudflare Workers AI. |
| 4 | |
| 5 | **Status**: Production Ready ✅ |
| 6 | **Last Updated**: 2025-10-21 |
| 7 | **Dependencies**: cloudflare-worker-base (for Worker setup) |
| 8 | **Latest Versions**: wrangler@4.43.0, @cloudflare/workers-types@4.20251014.0 |
| 9 | |
| 10 | --- |
| 11 | |
| 12 | ## Table of Contents |
| 13 | |
| 14 | 1. [Quick Start (5 minutes)](#quick-start-5-minutes) |
| 15 | 2. [Workers AI API Reference](#workers-ai-api-reference) |
| 16 | 3. [Model Selection Guide](#model-selection-guide) |
| 17 | 4. [Common Patterns](#common-patterns) |
| 18 | 5. [AI Gateway Integration](#ai-gateway-integration) |
| 19 | 6. [Rate Limits & Pricing](#rate-limits--pricing) |
| 20 | 7. [Production Checklist](#production-checklist) |
| 21 | |
| 22 | --- |
| 23 | |
| 24 | ## Quick Start (5 minutes) |
| 25 | |
| 26 | ### 1. Add AI Binding |
| 27 | |
| 28 | **wrangler.jsonc:** |
| 29 | ```jsonc |
| 30 | { |
| 31 | "ai": { |
| 32 | "binding": "AI" |
| 33 | } |
| 34 | } |
| 35 | ``` |
| 36 | |
| 37 | ### 2. Run Your First Model |
| 38 | |
| 39 | ```typescript |
| 40 | export interface Env { |
| 41 | AI: Ai; |
| 42 | } |
| 43 | |
| 44 | export default { |
| 45 | async fetch(request: Request, env: Env): Promise<Response> { |
| 46 | const response = await env.AI.run('@cf/meta/llama-3.1-8b-instruct', { |
| 47 | prompt: 'What is Cloudflare?', |
| 48 | }); |
| 49 | |
| 50 | return Response.json(response); |
| 51 | }, |
| 52 | }; |
| 53 | ``` |
| 54 | |
| 55 | ### 3. Add Streaming (Recommended) |
| 56 | |
| 57 | ```typescript |
| 58 | const stream = await env.AI.run('@cf/meta/llama-3.1-8b-instruct', { |
| 59 | messages: [{ role: 'user', content: 'Tell me a story' }], |
| 60 | stream: true, // Always use streaming for text generation! |
| 61 | }); |
| 62 | |
| 63 | return new Response(stream, { |
| 64 | headers: { 'content-type': 'text/event-stream' }, |
| 65 | }); |
| 66 | ``` |
| 67 | |
| 68 | **Why streaming?** |
| 69 | - Prevents buffering large responses in memory |
| 70 | - Faster time-to-first-token |
| 71 | - Better user experience for long-form content |
| 72 | - Avoids Worker timeout issues |
| 73 | |
| 74 | --- |
| 75 | |
| 76 | ## Workers AI API Reference |
| 77 | |
| 78 | ### `env.AI.run()` |
| 79 | |
| 80 | Run an AI model inference. |
| 81 | |
| 82 | **Signature:** |
| 83 | ```typescript |
| 84 | async env.AI.run( |
| 85 | model: string, |
| 86 | inputs: ModelInputs, |
| 87 | options?: { gateway?: { id: string; skipCache?: boolean } } |
| 88 | ): Promise<ModelOutput | ReadableStream> |
| 89 | ``` |
| 90 | |
| 91 | **Parameters:** |
| 92 | |
| 93 | - `model` (string, required) - Model ID (e.g., `@cf/meta/llama-3.1-8b-instruct`) |
| 94 | - `inputs` (object, required) - Model-specific inputs |
| 95 | - `options` (object, optional) - Additional options |
| 96 | - `gateway` (object) - AI Gateway configuration |
| 97 | - `id` (string) - Gateway ID |
| 98 | - `skipCache` (boolean) - Skip AI Gateway cache |
| 99 | |
| 100 | **Returns:** |
| 101 | |
| 102 | - Non-streaming: `Promise<ModelOutput>` - JSON response |
| 103 | - Streaming: `ReadableStream` - Server-sent events stream |
| 104 | |
| 105 | --- |
| 106 | |
| 107 | ### Text Generation Models |
| 108 | |
| 109 | **Input Format:** |
| 110 | ```typescript |
| 111 | { |
| 112 | messages?: Array<{ role: 'system' | 'user' | 'assistant'; content: string }>; |
| 113 | prompt?: string; // Deprecated, use messages |
| 114 | stream?: boolean; // Default: false |
| 115 | max_tokens?: number; // Max tokens to generate |
| 116 | temperature?: number; // 0.0-1.0, default varies by model |
| 117 | top_p?: number; // 0.0-1.0 |
| 118 | top_k?: number; |
| 119 | } |
| 120 | ``` |
| 121 | |
| 122 | **Output Format (Non-Streaming):** |
| 123 | ```typescript |
| 124 | { |
| 125 | response: string; // Generated text |
| 126 | } |
| 127 | ``` |
| 128 | |
| 129 | **Example:** |
| 130 | ```typescript |
| 131 | const response = await env.AI.run('@cf/meta/llama-3.1-8b-instruct', { |
| 132 | messages: [ |
| 133 | { role: 'system', content: 'You are a helpful assistant.' }, |
| 134 | { role: 'user', content: 'What is TypeScript?' }, |
| 135 | ], |
| 136 | stream: false, |
| 137 | }); |
| 138 | |
| 139 | console.log(response.response); |
| 140 | ``` |
| 141 | |
| 142 | --- |
| 143 | |
| 144 | ### Text Embeddings Models |
| 145 | |
| 146 | **Input Format:** |
| 147 | ```typescript |
| 148 | { |
| 149 | text: string | string[]; // Single text or array of texts |
| 150 | } |
| 151 | ``` |
| 152 | |
| 153 | **Output Format:** |
| 154 | ```typescript |
| 155 | { |
| 156 | shape: number[]; // [batch_size, embedding_dimensions] |
| 157 | data: number[][]; // Array of embedding vectors |
| 158 | } |
| 159 | ``` |
| 160 | |
| 161 | **Example:** |
| 162 | ```typescript |
| 163 | const embeddings = await env.AI.run('@cf/baai/bge-base-en-v1.5', { |
| 164 | text: ['Hello world', 'Cloudflare Workers'], |
| 165 | }); |
| 166 | |
| 167 | console.log(embeddings.shape); // [2, 768] |
| 168 | console.log(embeddings.data[0]); // [0.123, -0.456, ...] |
| 169 | ``` |
| 170 | |
| 171 | --- |
| 172 | |
| 173 | ### Image Generation Models |
| 174 | |
| 175 | **Input Format:** |
| 176 | ```typescript |
| 177 | { |
| 178 | prompt: string; // Text description |
| 179 | num_steps?: number; |