Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Comprehensive Cloudflare platform skill covering Workers, D1, R2, KV, AI, Durable Objects, and security.
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
references/workers-ai/configuration.md
1# Workers AI Configuration23## wrangler.jsonc45```jsonc6{7"name": "my-ai-worker",8"main": "src/index.ts",9"compatibility_date": "2024-01-01",10"ai": {11"binding": "AI"12}13}14```1516## TypeScript1718```bash19npm install --save-dev @cloudflare/workers-types20```2122```typescript23interface Env {24AI: Ai;25}2627export default {28async fetch(request: Request, env: Env) {29const response = await env.AI.run('@cf/meta/llama-3.1-8b-instruct', {30messages: [{ role: 'user', content: 'Hello' }]31});32return Response.json(response);33}34};35```3637## Local Development3839```bash40wrangler dev --remote # Required for AI - no local inference41```4243## REST API4445```typescript46const response = await fetch(47`https://api.cloudflare.com/client/v4/accounts/${ACCOUNT_ID}/ai/run/@cf/meta/llama-3.1-8b-instruct`,48{49method: 'POST',50headers: { 'Authorization': `Bearer ${API_TOKEN}` },51body: JSON.stringify({ messages: [{ role: 'user', content: 'Hello' }] })52}53);54```5556Create API token at: dash.cloudflare.com/profile/api-tokens (Workers AI - Read permission)5758## SDK Compatibility5960**OpenAI SDK:**61```typescript62import OpenAI from 'openai';63const client = new OpenAI({64apiKey: env.CLOUDFLARE_API_TOKEN,65baseURL: `https://api.cloudflare.com/client/v4/accounts/${env.ACCOUNT_ID}/ai/v1`66});67```6869## Multi-Model Setup7071```typescript72const MODELS = {73chat: '@cf/meta/llama-3.1-8b-instruct',74embed: '@cf/baai/bge-base-en-v1.5',75image: '@cf/stabilityai/stable-diffusion-xl-base-1.0'76};77```7879## RAG Setup (with Vectorize)8081```jsonc82{83"ai": { "binding": "AI" },84"vectorize": {85"bindings": [{ "binding": "VECTORIZE", "index_name": "embeddings-index" }]86}87}88```8990## Troubleshooting9192| Error | Fix |93|-------|-----|94| `env.AI is undefined` | Check `ai` binding in wrangler.jsonc |95| Local AI doesn't work | Use `wrangler dev --remote` |96| Type 'Ai' not found | Install `@cloudflare/workers-types` |97| @cloudflare/ai package error | Don't install - use native binding |98