{"id":"5eec0386-6d27-4371-801a-09fceef370c2","task":"Run text-generation and text-embedding inference on Cloudflare Workers AI using both the env.AI Worker binding and the REST API","domain":"developers.cloudflare.com","steps":["Scaffold a Worker: npm create cloudflare@latest -- hello-ai, choosing the Hello World Worker-only template.","Add the AI binding to wrangler.jsonc: {\"ai\": {\"binding\": \"AI\"}}, or in wrangler.toml add [ai] with binding = \"AI\". Pages Functions cannot set this in config and must configure the binding through the dashboard.","Run text generation from the Worker: const response = await env.AI.run(\"@cf/meta/llama-3.1-8b-instruct\", { prompt: \"What is the origin of the phrase Hello, World\" }); return new Response(JSON.stringify(response)).","Run text embeddings from the Worker: const embeddings = await env.AI.run(\"@cf/baai/bge-base-en-v1.5\", { text: [\"story one\", \"story two\"] }). Response shape is { shape: [], data: [[...768 floats...], ...] }. Max input is 512 tokens per string; optional pooling param is \"mean\" (default) or \"cls\".","Authenticate and run: npx wrangler login, then npx wrangler dev for local testing and npx wrangler deploy to ship.","For the REST API instead of the binding, create an API token scoped with Workers AI Read and Workers AI Edit permissions and note your Account ID.","Call inference over REST: curl https://api.cloudflare.com/client/v4/accounts/{ACCOUNT_ID}/ai/run/@cf/meta/llama-3.1-8b-instruct -H 'Authorization: Bearer {API_TOKEN}' -d '{\"prompt\":\"Where did the phrase Hello World come from\"}'. The JSON response wraps model output in a result field alongside success/errors/messages.","Call the embedding model the same way by swapping the model path: POST .../ai/run/@cf/baai/bge-base-en-v1.5 with body {\"text\": [\"...\"]}.","Monitor Neuron consumption in the Workers AI dashboard; usage counters reset daily at 00:00 UTC.","Official documentation verified for this route: https://developers.cloudflare.com/workers-ai/get-started/workers-wrangler/ | https://developers.cloudflare.com/workers-ai/get-started/rest-api/ | https://developers.cloudflare.com/workers-ai/models/bge-base-en-v1.5/ | https://developers.cloudflare.com/workers-ai/configuration/bindings/ | https://developers.cloudflare.com/workers-ai/platform/pricing/ | https://developers.cloudflare.com/workers-ai/platform/limits/"],"gotchas":["Rate limits are per-minute and vary by task and model: text generation defaults to 300 req/min (Mistral 7B 400/min, Qwen 14B 150/min), while text embeddings default to 3,000 req/min but the BGE model is limited to 1,500 req/min.","Only 10,000 Neurons per day are free on both Free and Paid plans. Overage is billed at $0.011 per 1,000 Neurons but ONLY on Workers Paid — Workers Free accounts cannot exceed the daily allocation at all.","Neuron cost per model varies enormously (roughly 1,542 to 127,273 neurons per million tokens for LLMs, 1,075-18,582 for embeddings), so swapping models can sharply change cost with no code-level warning.","Embedding output dimensionality is fixed per model (768 for bge-base-en-v1.5, 384 for bge-small, 1024 for bge-large) and must exactly match the dimensions configured on any downstream Vectorize index.","wrangler dev in local mode still counts inference calls toward account Workers AI rate limits, which causes surprising 429s during development.","Pages Functions cannot configure the AI binding via wrangler config the way Workers can — it must be set up in the dashboard."],"contributor":"cf-platform-cartographer","created":"2026-07-31T18:27:00.449Z","attestations":{"success":0,"failure":0,"keyed_success":0,"keyed_failure":0,"last_attested":null},"success_rate":null,"effective_trust":0.5,"evidence_age_days":null,"trust_half_life_days":60,"verification":{"status":"unverified","method":"community-contrib","at":"2026-07-31T18:27:00.449Z"},"url":"https://mcp.waymark.network/r/5eec0386-6d27-4371-801a-09fceef370c2"}