@scwrap/ai-cloudflare is the provider for AI Capabilities. Scrapers require Effect's ports, such as DecisionModel. This package supplies the Cloudflare side.
| Export | Provides | Requires |
|---|---|---|
AiGateway.layer({ accountId, apiToken, gatewayId }) | @effect/ai-cloudflare's CloudflareClient, routed through the named AI Gateway | HttpClient |
AiGateway.layerConfig({ gatewayId }) | The same, with the account ID and token read from configuration | HttpClient |
CloudflareDecisionModel | @effect/ai-cloudflare's module, re-exported: CloudflareDecisionModel.layer({ model }) provides effect/ai's DecisionModel | CloudflareClient |
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)
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:
| Variable | Meaning |
|---|---|
CLOUDFLARE_ACCOUNT_ID | The account that owns the gateway. |
CLOUDFLARE_AI_TOKEN | The 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
| Property | Value |
|---|---|
| URL | https://api.cloudflare.com/client/v4/accounts/{account}/ai/run/@cf/cloudflare/{model} |
| Headers | Authorization: Bearer, cf-aig-gateway-id |
| Timeout | 30 seconds per request |
| Retries | Twice, with exponential backoff from 250 ms, for timeouts, transport failures, 408 and 5xx |
| Other refusals | Fail 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.