Skip to content
Back to Cloud Services

Cloudflare Workers AI Inference

Run LLMs and image models at the edge with Workers AI — Llama, Mistral, Stable Diffusion, no GPU management.

CloudflareIntermediate

Why

Workers AI runs open models at Cloudflare edge locations — no GPU provisioning, pay per token, and latency stays low globally. Includes Llama 3, Mistral, Whisper, and Stable Diffusion.

Cloudflare's network spans 330+ cities in 125+ countries, serving 20% of all internet traffic. Workers have 0ms cold starts (isolate-based) and the free tier covers 100k requests/day.

When

Use for chatbots, summarization, translation, image generation, and transcription where you need global low latency and don't require GPT-4/Claude quality. Avoid for highest-quality reasoning (use OpenAI/Anthropic).

How

1. Bind AI — [[ai]] binding = "AI" in wrangler.toml 2. List models — env.AI.models() or cf dashboard 3. Run text — env.AI.run("@cf/meta/llama-3-8b-instruct", { prompt, stream }) 4. Streaming — for await (const chunk of stream) { ... } 5. Image — env.AI.run("@cf/stabilityai/sd-xl-base-1.0", { prompt }) 6. Embeddings — env.AI.run("@cf/baai/bge-base-en-v1.5", { text }) for Vectorize 7. Whisper — env.AI.run("@cf/openai/whisper-tiny", { audio }) for transcription

Example

// wrangler.toml [[ai]] binding = "AI" // src/index.ts — streaming chat at the edge export default { async fetch(request: Request, env: Env): Promise<Response> { const { prompt } = await request.json(); const stream = await env.AI.run("@cf/meta/llama-3-8b-instruct", { messages: [{ role: "user", content: prompt }], stream: true, }); return new Response(stream, { headers: { "Content-Type": "text/event-stream" }, }); }, }; // Embeddings for Vectorize (semantic search) const embedding = await env.AI.run("@cf/baai/bge-base-en-v1.5", { text: ["how to deploy workers", "wrangler cli guide"], }); await env.VECTOR_INDEX.upsert([{ id: "doc1", values: embedding.data[0] }]);

Common Problems

  • ⚠Model not available in all regions — some models run in limited locations, check docs
  • ⚠Token limits per request — Llama 3 8B caps at 6000 tokens input, chunk long context
  • ⚠Cold start on first inference — model loads per location, first request slower

Best Practices

  • ✓Stream responses — SSE improves perceived latency, users see tokens immediately
  • ✓Use smaller models for classification — bge-small for embeddings, llama-3-8b for chat
  • ✓Cache common prompts in KV — same question returns cached answer, saves AI calls

FAQ

What is Cloudflare Workers AI Inference and what problem does it solve?

Run LLMs and image models at the edge with Workers AI — Llama, Mistral, Stable Diffusion, no GPU management. It lets developers deploy, store, and scale without self-managing infrastructure — a core reference for cloud service selection.

How do I get started with Cloudflare Workers AI Inference in 5 minutes?

Two steps to run it: (1) Bind AI — [[ai]] binding = "AI" in wrangler.toml; (2) List models — env.AI.models() or cf dashboard. Validate on the free tier first, then wire into production.

What is the #1 pitfall with Cloudflare Workers AI Inference?

The most frequent issue is: Model not available in all regions — some models run in limited locations, check docs. Run a pre-launch checklist item by item, and add policy/config scanning in CI.

What are the best practices for Cloudflare Workers AI Inference?

Best practices for Cloudflare Workers AI Inference include: Stream responses — SSE improves perceived latency, users see tokens immediately; Use smaller models for classification — bge-small for embeddings, llama-3-8b for chat; Cache common prompts in KV — same question returns cached answer, saves AI calls. The first one — "Stream responses — SSE improves perceived latency, users see tokens immediately" — should be a team default baked into your IaC templates.

How do I control cost and avoid bill surprises with Cloudflare Workers AI Inference?

Three ways to prevent overspend: (1) tag every resource with env/team and enable billing alarms; (2) set budget thresholds to trigger Slack/email alerts; (3) review idle resources in Cost Explorer regularly and clean them up. Most Cloudflare Workers AI Inference options offer a free tier — validate within free quota before scaling.