Cloudflare AI · Streaming Explorer
Test your Cloudflare Worker that uses the OpenAI-compatible SDK with server-sent event streaming. Enter your Worker URL, pick a model, type a prompt — watch tokens arrive in real time.
SSE Streaming
Llama 3.1 · 8B
OpenAI-Compatible
Runs client-side
Tokens Received
0
This session
Requests Sent
0
This session
Avg Speed
—
Tokens / second
Last Latency
—
Time to first token
Worker Configuration
Endpoint & Model
Point to your deployed Cloudflare Worker URL
Llama 3.1 · 8B
Llama 3.2 · 3B
Mistral 7B
Gemma 7B
Qwen 1.5 · 7B
Custom…
Range: 32 – 4096
Live Stream
SSE Token Stream
Ready · No active stream
Quick Prompts
Worker Code Reference
Source Code
Your Cloudflare Worker TypeScript implementation
worker.ts
wrangler.toml
Deploy Steps
// Cloudflare Worker — SSE Streaming with OpenAI SDK import OpenAI from "openai"; export interface Env { CLOUDFLARE_API_KEY: string; CLOUDFLARE_ACCOUNT_ID: string; } export default { async fetch(request: Request, env: Env): Promise<Response> { // Parse optional body for dynamic prompts let userMessage = "Write a short sci-fi greeting"; let model = "@cf/meta/llama-3.1-8b-instruct"; if (request.method === "POST") { const body = await request.json().catch(() => ({})); if (body.prompt) userMessage = body.prompt; if (body.model) model = body.model; } const openai = new OpenAI({ apiKey: env.CLOUDFLARE_API_KEY, baseURL: `https://api.cloudflare.com/client/v4/accounts/${env.CLOUDFLARE_ACCOUNT_ID}/ai/v1`, }); const stream = await openai.chat.completions.create({ model, messages: [{ role: "user", content: userMessage }], stream: true, }); const encoder = new TextEncoder(); const readable = new ReadableStream({ async start(controller) { try { for await (const chunk of stream) { const token = chunk.choices?.[0]?.delta?.content ?? ""; if (token) controller.enqueue(encoder.encode(token)); } controller.close(); } catch (err) { controller.error(err); } }, }); return new Response(readable, { headers: { "Content-Type": "text/plain; charset=utf-8", "Cache-Control": "no-cache", "Access-Control-Allow-Origin": "*", // ← add for browser access }, }); }, };
# wrangler.toml name = "acf-ai-worker" main = "src/index.ts" compatibility_date = "2024-11-01" compatibility_flags = ["nodejs_compat"] # Secrets (set via CLI — never commit these) # wrangler secret put CLOUDFLARE_API_KEY # wrangler secret put CLOUDFLARE_ACCOUNT_ID [vars] # No plaintext env vars needed for this worker # Optional: route binding # [[routes]] # pattern = "ai.yourdomain.com/*" # zone_name = "yourdomain.com"
# ── Prerequisites ────────────────────────────────────── # 1. Node.js 18+ and npm installed # 2. Cloudflare account with Workers AI access # ── Install Wrangler & deps ───────────────────────────── $ npm install -g wrangler $ npm install openai # ── Authenticate ─────────────────────────────────────── $ wrangler login # ── Add secrets (Workers AI credentials) ─────────────── $ wrangler secret put CLOUDFLARE_API_KEY # → paste your Cloudflare AI API key $ wrangler secret put CLOUDFLARE_ACCOUNT_ID # → paste your Cloudflare Account ID # ── Dev locally ──────────────────────────────────────── $ wrangler dev # Worker runs at http://localhost:8787 # ── Deploy to production ──────────────────────────────── $ wrangler deploy # → https://acf-ai-worker.<subdomain>.workers.dev # ── Test the stream with curl ─────────────────────────── $ curl -N -X POST https://your-worker.workers.dev \ -H "Content-Type: application/json" \ -d '{"prompt":"Tell me about fintech","model":"@cf/meta/llama-3.1-8b-instruct"}'
How This Explorer Works
Browser → Cloudflare Worker → Workers AI
This page calls your Worker URL directly via fetch() with a POST body containing
{"prompt":"…","model":"…"}. Tokens stream back as plain text chunks through the browser's ReadableStream API.
CORS required: Your Worker must return
Access-Control-Allow-Origin: * (or your domain) in its response headers, otherwise browser requests will be blocked. See the worker.ts tab above.
Security
API keys never touch this page. All auth happens inside your Worker via Cloudflare secrets.
Performance
Cloudflare Workers run at the edge, giving sub-100ms cold starts globally across 300+ data centres.
Compatibility
Workers AI exposes an OpenAI-compatible API, so any OpenAI SDK client works with zero changes.
Pricing
Workers AI is billed per neuron. Llama 3.1 8B costs ~$0.011 / 1K input tokens on the Standard tier.