jackspace/claudeskillz/cloudflare-workers-ai

Trust tier 3
  • community
Source check pending

Checking that the SKILL.md file is still in the repository.

Reference for Cloudflare Workers AI, running AI models on serverless GPUs through the env.AI.run() binding. Covers the AI binding in wrangler.jsonc, model selection (@cf/meta/llama, Flux and Stable Diffusion image generation, BGE embeddings, vision models), streaming responses, AI Gateway caching, RAG with Vectorize, Vercel AI SDK integration and OpenAI-compatible endpoints, plus rate limits, neuron pricing and a production checklist. Use when implementing AI inference in a Cloudflare Worker, generating text or images at the edge, configuring Workers AI bindings, adding AI streaming, wiring up AI Gateway, building embeddings or RAG on Workers, or debugging AI_ERROR, rate limit errors, model not found, model unavailable, token limit exceeded, neurons exceeded, quota exceeded or streaming failures.

Install

npx vskill@latest install jackspace/claudeskillz/cloudflare-workers-ai

SKILL.md

Cloudflare Workers AI - Complete Reference

Production-ready knowledge domain for building AI-powered applications with Cloudflare Workers AI.

Status: Production Ready ✅ Last Updated: 2025-10-21 Dependencies: cloudflare-worker-base (for Worker setup) Latest Versions: wrangler@4.43.0, @cloudflare/workers-types@4.20251014.0


Table of Contents

  1. Quick Start (5 minutes)
  2. Workers AI API Reference
  3. Model Selection Guide
  4. Common Patterns
  5. AI Gateway Integration
  6. Rate Limits & Pricing
  7. Production Checklist

Quick Start (5 minutes)

1. Add AI Binding

wrangler.jsonc:

jsonc
{
"ai": {
"binding": "AI"
}
}
2. Run Your First Model
typescript
export interface Env {
AI: Ai;
}
export default {
async fetch(request: Request, env: Env): Promise<Response> {
const response = await env.AI.run('@cf/meta/llama-3.1-8b-instruct', {
prompt: 'What is Cloudflare?',
});
return Response.json(response);
},
};
3. Add Streaming (Recommended)
typescript
const stream = await env.AI.run('@cf/meta/llama-3.1-8b-instruct', {
messages: [{ role: 'user', content: 'Tell me a story' }],
stream: true, // Always use streaming for text generation!
});
return new Response(stream, {
headers: { 'content-type': 'text/event-stream' },
});

Why streaming?

  • Prevents buffering large responses in memory
  • Faster time-to-first-token
  • Better user experience for long-form content
  • Avoids Worker timeout issues

Workers AI API Reference

env.AI.run()

Run an AI model inference.

Signature:

typescript
async env.AI.run(
model: string,
inputs: ModelInputs,
options?: { gateway?: { id: string; skipCache?: boolean } }
): Promise<ModelOutput | ReadableStream>

Parameters:

  • model (string, required) - Model ID (e.g., @cf/meta/llama-3.1-8b-instruct)
  • inputs (object, required) - Model-specific inputs
  • options (object, optional) - Additional options
  • gateway (object) - AI Gateway configuration
  • id (string) - Gateway ID
  • skipCache (boolean) - Skip AI Gateway cache

Returns:

  • Non-streaming: Promise<ModelOutput> - JSON response
  • Streaming: ReadableStream - Server-sent events stream

Text Generation Models

Input Format:

typescript
{
messages?: Array<{ role: 'system' | 'user' | 'assistant'; content: string }>;
prompt?: string; // Deprecated, use messages
stream?: boolean; // Default: false
max_tokens?: number; // Max tokens to generate
temperature?: number; // 0.0-1.0, default varies by model
top_p?: number; // 0.0-1.0
top_k?: number;
}

Output Format (Non-Streaming):

typescript
{
response: string; // Generated text
}

Example:

typescript
const response = await env.AI.run('@cf/meta/llama-3.1-8b-instruct', {
messages: [
{ role: 'system', content: 'You are a helpful assistant.' },
{ role: 'user', content: 'What is TypeScript?' },
],
stream: false,
});
console.log(response.response);

Text Embeddings Models

Input Format:

typescript
{
text: string | string[]; // Single text or array of texts
}

Output Format:

typescript
{
shape: number[]; // [batch_size, embedding_dimensions]
data: number[][]; // Array of embedding vectors
}

Example:

typescript
const embeddings = await env.AI.run('@cf/baai/bge-base-en-v1.5', {
text: ['Hello world', 'Cloudflare Workers'],
});
console.log(embeddings.shape); // [2, 768]
console.log(embeddings.data[0]); // [0.123, -0.456, ...]

Image Generation Models

Input Format:

typescript
{
prompt: string; // Text description
num_steps?: number; // Default: 20
guidance?: number; // CFG scale, default: 7.5
strength?: number; // For img2img, default: 1.0
image?: number[][]; // For img2img (base64 or array)
}

Output Format:

  • Binary image data (PNG/JPEG)

Example:

typescript
const imageStream = await env.AI.run('@cf/black-forest-labs/flux-1-schnell', {
prompt: 'A beautiful sunset over mountains',
});
return new Response(imageStream, {
headers: { 'content-type': 'image/png' },
});

Vision Models

Input Format:

typescript
{
messages: Array<{
role: 'user' | 'assistant';
content: Array<{ type: 'text' | 'image_url'; text?: string; image_url?: { url: string } }>;
}>;
}

Example:

typescript
const response = await env.AI.run('@cf/meta/llama-3.2-11b-vision-instruct', {
messages: [
{
role: 'user',
content: [
{ type: 'text', text: 'What is in this image?' },
{ type: 'image_url', image_url: { url: 'data:image/png;base64,iVBOR...' } },
],
},
],
});

Model Selection Guide

Text Generation (LLMs)
ModelBest ForRate LimitSize
@cf/meta/llama-3.1-8b-instructGeneral purpose, fast300/min8B
@cf/meta/llama-3.2-1b-instructUltra-fast, simple tasks300/min1B
@cf/qwen/qwen1.5-14b-chat-awqHigh quality, complex reasoning150/min14B
@cf/deepseek-ai/deepseek-r1-distill-qwen-32bCoding, technical content300/min32B
@hf/thebloke/mistral-7b-instruct-v0.1-awqFast, efficient400/min7B
Text Embeddings
ModelDimensionsBest ForRate Limit
@cf/baai/bge-base-en-v1.5768General purpose RAG3000/min
@cf/baai/bge-large-en-v1.51024High accuracy search1500/min
@cf/baai/bge-small-en-v1.5384Fast, low storage3000/min
Image Generation
ModelBest ForRate LimitSpeed
@cf/black-forest-labs/flux-1-schnellHigh quality, photorealistic720/minFast
@cf/stabilityai/stable-diffusion-xl-base-1.0General purpose720/minMedium
@cf/lykon/dreamshaper-8-lcmArtistic, stylized720/minFast
Vision Models
ModelBest ForRate Limit
@cf/meta/llama-3.2-11b-vision-instructImage understanding720/min
@cf/unum/uform-gen2-qwen-500mFast image captioning720/min

Common Patterns

Pattern 1: Chat Completion with History
typescript
app.post('/chat', async (c) => {
const { messages } = await c.req.json<{
messages: Array<{ role: string; content: string }>;
}>();
const response = await c.env.AI.run('@cf/meta/llama-3.1-8b-instruct', {
messages,
stream: true,
});
return new Response(response, {
headers: { 'content-type': 'text/event-stream' },
});
});

Pattern 2: RAG (Retrieval Augmented Generation)
typescript
// Step 1: Generate embeddings
const embeddings = await env.AI.run('@cf/baai/bge-base-en-v1.5', {
text: [userQuery],
});
const vector = embeddings.data[0];
// Step 2: Search Vectorize
const matches = await env.VECTORIZE.query(vector, { topK: 3 });
// Step 3: Build context from matches
const context = matches.matches.map((m) => m.metadata.text).join('\n\n');
// Step 4: Generate response 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,
});
return new Response(response, {
headers: { 'content-type': 'text/event-stream' },
});

Pattern 3: Structured Output with Zod
typescript
import { z } from 'zod';
const RecipeSchema = z.object({
name: z.string(),
ingredients: z.array(z.string()),
instructions: z.array(z.string()),
prepTime: z.number(),
});
app.post('/recipe', async (c) => {
const { dish } = await c.req.json<{ dish: string }>();
const response = await c.env.AI.run('@cf/meta/llama-3.1-8b-instruct', {
messages: [
{
role: 'user',
content: `Generate a recipe for ${dish}. Return ONLY valid JSON matching this schema: ${JSON.stringify(RecipeSchema.shape)}`,
},
],
});
// Parse and validate
const recipe = RecipeSchema.parse(JSON.parse(response.response));
return c.json(recipe);
});

Pattern 4: Image Generation + R2 Storage
typescript
app.post('/generate-image', async (c) => {
const { prompt } = await c.req.json<{ prompt: string }>();
// Generate image
const imageStream = await c.env.AI.run('@cf/black-forest-labs/flux-1-schnell', {
prompt,
});
const imageBytes = await new Response(imageStream).bytes();
// Store in R2
const key = `images/${Date.now()}.png`;
await c.env.BUCKET.put(key, imageBytes, {
httpMetadata: { contentType: 'image/png' },
});
return c.json({
success: true,
url: `https://your-domain.com/${key}`,
});
});

AI Gateway Integration

AI Gateway provides caching, logging, and analytics for AI requests.

Setup:

typescript
const response = await env.AI.run(
'@cf/meta/llama-3.1-8b-instruct',
{ prompt: 'Hello' },
{
gateway: {
id: 'my-gateway', // Your gateway ID
skipCache: false, // Use cache
},
}
);

Benefits:

  • ✅ Cost Tracking - Monitor neurons usage per request
  • ✅ Caching - Reduce duplicate inference costs
  • ✅ Logging - Debug and analyze AI requests
  • ✅ Rate Limiting - Additional layer of protection
  • ✅ Analytics - Request patterns and performance

Access Gateway Logs:

typescript
const gateway = env.AI.gateway('my-gateway');
const logId = env.AI.aiGatewayLogId;
// Send feedback
await gateway.patchLog(logId, {
feedback: { rating: 1, comment: 'Great response' },
});

Rate Limits & Pricing

Rate Limits (per minute)
Task TypeDefault LimitNotes
Text Generation300/minSome fast models: 400-1500/min
Text Embeddings3000/minBGE-large: 1500/min
Image Generation720/minAll image models
Vision Models720/minImage understanding
Translation720/minM2M100, Opus MT
Classification2000/minText classification
Speech Recognition720/minWhisper models
Pricing (Neurons-Based)

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

Example Costs:

ModelInput (1M tokens)Output (1M tokens)
Llama 3.2 1B$0.027$0.201
Llama 3.1 8B$0.088$0.606
BGE-base embeddings$0.005N/A
Flux image generation~$0.011/imageN/A

Production Checklist

Before Deploying
  • [ ] Enable AI Gateway for cost tracking and logging
  • [ ] Implement streaming for all text generation endpoints
  • [ ] Add rate limit retry with exponential backoff
  • [ ] Validate input length to prevent token limit errors
  • [ ] Set appropriate timeouts (Workers: 30s CPU default, 5m max)
  • [ ] Monitor neurons usage in Cloudflare dashboard
  • [ ] Test error handling for model unavailable, rate limits
  • [ ] Add input sanitization to prevent prompt injection
  • [ ] Configure CORS if using from browser
  • [ ] Plan for scale - upgrade to Paid plan if needed
Error Handling
typescript
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;
const message = lastError.message.toLowerCase();

This is the start of the file. Read the full SKILL.md on GitHub.

Security

  • Security scanPassed, 100 of 100Pattern checks on the skill's files.
  • Model reviewPassed

Quality evaluation

  • Task evaluationNot runNo evaluation yet. Run one in Skill Studio.

Source

Popularity

Repository stars
12
Repository forks
4
7-day trend score
13

Stars and forks are for the whole GitHub repository, not this skill alone. Refreshed 31 Mar 2026, 7 months ago.

Works with

Any agent vskill installs to: 56 targets in all. These 39 have a page on this site.

  • Claude Code
  • Cursor
  • GitHub Copilot
  • Windsurf
  • Codex
  • Gemini CLI
  • Cline
  • Amp
  • Roo Code
  • Goose
  • Continue
  • Kilo Code
  • Junie
  • Trae
  • OpenHands
  • Qwen Code
  • AI Agent
  • Tabnine Chat
  • Sourcegraph Cody
  • Amazon Q
  • Firebase Genkit
  • Sweep
  • Devin
  • Phind
  • Mentat
  • Claude Desktop
  • Aider
  • GPT Pilot
  • Plandex
  • bolt.new
  • v0
  • Replit Agent
  • Anse
  • TypingMind
  • BetterChatGPT
  • LibreChat
  • Warp AI
  • Pieces
  • Fig AI
Embed badge
vskill VERIFIED badge
[![vskill VERIFIED](https://verified-skill.com/api/v1/skills/jackspace/claudeskillz/cloudflare-workers-ai/badge)](https://verified-skill.com/skills/jackspace/claudeskillz/cloudflare-workers-ai)

Something wrong with this skill or its page? Tell us.