AI
Source:
src/Cloudflare/Workers/AI.ts
The native Cloudflare Workers AI binding — run inference on Workers AI
models directly from a Worker, with no AI Gateway (or any other cloud
resource) required. This is the plain { type: "ai" } Worker binding: the
runtime value is the same env.AI handle you would declare in
wrangler.json.
AI is a single value that is at once the Binding.Service tag, the
callable that produces an AIBinding, and the type. Declare it on a
Worker’s env (it flows through InferEnv → the runtime Ai handle) or
yield* it inside an Effect-native Worker to attach the binding and obtain
the AIClient.
Use Cloudflare.AI.Gateway + QueryGateway instead when you want requests
routed through an AI Gateway (caching, rate limiting, logs); use AI when
you just want to call Workers AI models.
Effect-style Worker (recommended)
Section titled “Effect-style Worker (recommended)”Cloudflare.Worker("AiWorker", { main: import.meta.url }, Effect.gen(function* () { const ai = yield* Cloudflare.Workers.AI(); return { fetch: Effect.gen(function* () { const result = yield* ai.run("@cf/meta/llama-3.3-70b-instruct-fp8-fast", { prompt: "What is the origin of the phrase Hello, World?", }).pipe(Effect.orDie); return yield* HttpServerResponse.json(result); }), }; }).pipe(Effect.provide(Cloudflare.Workers.AIBinding)),);Effect AI LanguageModel
Section titled “Effect AI LanguageModel”model(options) produces a Layer<LanguageModel, never, RuntimeContext>
that translates LanguageModel.generateText / streamText calls
(including tool calls) into ai.run(...) against the bound Workers AI
model — the same adapter AI Gateway’s QueryGateway uses, minus the
gateway routing.
const ai = yield* Cloudflare.Workers.AI();
const languageModel = ai.model({ model: "@cf/meta/llama-3.3-70b-instruct-fp8-fast", parameters: { temperature: 0.7, maxTokens: 1024 },});
const response = yield* LanguageModel.generateText({ prompt }).pipe( Effect.provide(languageModel),);Binding to an Async Worker
Section titled “Binding to an Async Worker”export const Worker = Cloudflare.Worker("Worker", { main: "./src/worker.ts", env: { AI: Cloudflare.Workers.AI() },});
export type WorkerEnv = Cloudflare.InferEnv<typeof Worker>;// { AI: Ai }