| 1 | # @effect/ai-cloudflare |
| 2 | |
| 3 | Cloudflare Workers AI provider for Effect's `DecisionModel`. Supports text and JSON classification, ordered ratings, and probabilities with Clef and Clef-flash over REST. |
| 4 | |
| 5 | ## Installation |
| 6 | |
| 7 | ```sh |
| 8 | npm install effect @effect/ai-cloudflare |
| 9 | ``` |
| 10 | |
| 11 | ## Usage |
| 12 | |
| 13 | Set `CLOUDFLARE_ACCOUNT_ID` and `CLOUDFLARE_API_TOKEN`, then provide a Cloudflare decision model: |
| 14 | |
| 15 | ```ts |
| 16 | import { CloudflareClient, CloudflareDecisionModel } from "@effect/ai-cloudflare" |
| 17 | import { Effect, Layer, Schema } from "effect" |
| 18 | import { Decision, DecisionModel } from "effect/ai" |
| 19 | import { FetchHttpClient } from "effect/http" |
| 20 | |
| 21 | const Triage = Decision.make({ |
| 22 | input: Schema.String, |
| 23 | decisions: { |
| 24 | urgent: Decision.probability({ |
| 25 | instructions: "Does this request need immediate attention?" |
| 26 | }) |
| 27 | } |
| 28 | }) |
| 29 | |
| 30 | const Clef = CloudflareDecisionModel.layer({ model: "clef-flash" }).pipe( |
| 31 | Layer.provide(CloudflareClient.layerConfig()), |
| 32 | Layer.provide(FetchHttpClient.layer) |
| 33 | ) |
| 34 | |
| 35 | const program = DecisionModel.decide(Triage, { |
| 36 | input: "Checkout has been failing for every customer for an hour." |
| 37 | }).pipe(Effect.provide(Clef)) |
| 38 | ``` |
| 39 | |
| 40 | Use `"clef"` for the larger model. `CloudflareDecisionModel.model` provides a model descriptor with Cloudflare provider metadata. `CloudflareClient.layer` accepts an explicit account ID and redacted API token; `apiUrl` and `transformClient` customize HTTP access. |
| 41 | |
| 42 | The client unwraps Cloudflare's REST envelope and maps failures to `AiError`. It does not retry automatically. Rate-limit errors include `Retry-After` when provided. The decision model validates answers and rescales small probability drift from four-decimal rounding. |
| 43 | |
| 44 | Cloudflare limits each request to 64 questions, classification to 2–255 choices, and ratings to 2–10 levels. See the [Clef API documentation](https://developers.cloudflare.com/workers-ai/models/clef/) for request limits. |