Skip to content

Workers AI

@scwrap/ai-cloudflare connects AI Capabilities to Cloudflare Workers AI through AI Gateway: Clef as a DecisionModel.

@scwrap/ai-cloudflare is the provider for AI Capabilities. Scrapers require Effect's ports, such as DecisionModel. This package supplies the Cloudflare side.

ExportProvidesRequires
AiGateway.layer({ accountId, apiToken, gatewayId })@effect/ai-cloudflare's CloudflareClient, routed through the named AI GatewayHttpClient
AiGateway.layerConfig({ gatewayId })The same, with the account ID and token read from configurationHttpClient
CloudflareDecisionModel@effect/ai-cloudflare's module, re-exported: CloudflareDecisionModel.layer({ model }) provides effect/ai's DecisionModelCloudflareClient

CloudflareDecisionModel is re-exported so a Release can use a Workers AI decision model while importing only effect and the Scwrap SDKs, which is all a one-file Scraper and an instance build accept.

AiGateway ​

Every request goes through a Cloudflare AI Gateway, which gives you logging, caching and rate limits in one place.

Example (Clef as a DecisionModel)

ts
import { AiGateway, CloudflareDecisionModel } from "@scwrap/ai-cloudflare";
import { Config, Layer } from "effect";

const Clef = CloudflareDecisionModel.layer({ model: "clef" }).pipe(
  Layer.provide(
    AiGateway.layerConfig({
      gatewayId: Config.NonEmptyString("SCWRAP_CAPTCHA_GATEWAY_ID"),
    })
  )
);

AiGateway.layerConfig reads:

VariableMeaning
CLOUDFLARE_ACCOUNT_IDThe account that owns the gateway.
CLOUDFLARE_AI_TOKENThe API token, redacted. Read only outside an instance.

In a Worker release on an instance, AiGateway.layerConfig sends every request through the instance's front desk instead and reads no token. The front desk needs the Scraper's ai:workers-ai grant (Grants), accepts only URLs under the instance account's Workers AI API (https://api.cloudflare.com/client/v4/accounts/{account}/ai/), and puts the instance's CLOUDFLARE_AI_TOKEN from RELEASE_ENV in the Authorization header. The Release itself still reads CLOUDFLARE_ACCOUNT_ID and its gateway through Config, so both go in its Scraper's entry of SCRAPER_SETTINGS. A refusal fails the request as an AiError.

Requests ​

PropertyValue
URLhttps://api.cloudflare.com/client/v4/accounts/{account}/ai/run/@cf/cloudflare/{model}
HeadersAuthorization: Bearer, cf-aig-gateway-id
Timeout30 seconds per request
RetriesTwice, with exponential backoff from 250 ms, for timeouts, transport failures, 408 and 5xx
Other refusalsFail at once as AiError

When the gateway returns a log ID, the current span gets cloudflare.aig_log_id.

Token ​

Create an account-scoped API token with Account → Workers AI → Read and Account → AI Gateway → Run. It is a runtime secret. Never use a management token for it.

Clef for captchas ​

Clef is a Workers AI decision model. TextCaptchaSolver.fromDecisionModel turns any DecisionModel into a captcha solver, so Clef needs no code of its own here.

Measured on one site's 49 server-verified four-letter holdout captchas, upscaled 4× with upscaleCaptcha: Clef read 43, or 87.8%. A trained template engine read at least 95% of the same set, so combine the two with firstConfident and ask Clef only when the templates fall below their confidence threshold.

Source ​