Blog21 min read
Summarize any webpage with Workers AI
Deep dive into edge URL summarization on Cloudflare - Workers AI models, fetch + extract, AI Gateway, prompts, errors, cost control, and the AI Website Summary experiment.
Support teams, research agents, and product features often need the gist of a URL without stuffing full HTML into a context window. A Cloudflare Worker can fetch the page at the edge, extract readable text, and call Workers AI so retrieval and inference stay on Cloudflare’s network - no separate GPU box required.
This post covers pipeline design, product choices (AI Gateway, embeddings, RAG), prompt and extraction quality, and the AI Website Summary experiment as a pasteable reference.
Cloudflare products in this pattern
| Product | Role | Docs |
|---|---|---|
| Workers | Orchestrate fetch → extract → infer | Edge HTTP |
| Workers AI | Text summarization models via env.AI | Models + binding |
| AI Gateway (optional) | Caching, analytics, provider routing | Gateway |
| HTMLRewriter | Cheap HTML extraction when static | Streaming parser |
| Browser Rendering | JS-heavy pages before summarize | Headless Chrome |
| Vectorize | Next step: RAG over many docs | Vector DB |
| AI Search | Managed RAG alternative | Managed retrieval |
The minimal pipeline
A reliable summarizer is four steps:
- Validate the URL (
http/httpsonly) - same discipline as other catalog APIs - Fetch HTML with timeout and size cap (Fetch API)
- Extract main text (strip nav, scripts, chrome)
- Infer with a Workers AI text model and return JSON
Client → Worker → fetch(url) → extract text → env.AI.run(model, …) → JSONKeep the Worker under ~60 seconds end-to-end. Cap extracted characters so prompt size stays predictable and costs do not spike on pathological pages.
Why Workers AI fits this job
From the Workers AI docs:
- Colocated with fetch - HTML retrieval and inference share the edge
- Binding-based -
env.AIavoids shipping long-lived provider keys for core Cloudflare models - Composable - same binding powers tags, translation, embeddings, speech, and image models elsewhere in the catalog
When you outgrow “one model, one route,” look at:
- Cloud AI Proxy - generic model access patterns
- AI Gateway Dashboard - cache/latency metadata through AI Gateway
- Workers AI model catalog - pick models by task and size
Designing the response contract
Return structured JSON humans and agents can both consume:
{
"url": "https://example.com/article",
"title": "Example article",
"summary": "…",
"model": "@cf/…"
}On failure, prefer stable codes aligned with the monorepo convention:
| Code | When |
|---|---|
INVALID_URL | Missing or non-http(s) URL |
FETCH_ERROR | Upstream fetch failed / timed out |
EXTRACT_ERROR | No usable text extracted |
AI_ERROR | Model binding / inference failed |
Validate in the route, call lib helpers, return jsonSuccess / jsonError. Do not leak stack traces to clients.
Extraction quality matters more than model choice
Garbage HTML in → vague summary out. Practical tips:
- Prefer main-content heuristics over dumping
bodytext - Drop
script,style,nav, and footer blocks early (HTMLRewriter is ideal for streaming transforms) - Normalize whitespace; keep headings as soft section cues
- If the page is JS-only, plain
fetchmisses the article - use Browser Rendering via Browser Markdown Scrape, then summarize the markdown
For static marketing pages, Metadata Extractor may already give title + description without an LLM. Use Workers AI when you need a true synopsis, not just og:description.
Prompt sketch
Keep prompts explicit and bounded:
Summarize the following webpage text in 5-8 sentences.
Focus on facts; ignore navigation chrome.
If the text is empty or unrelated to an article, say so.
TEXT:
…Pin the model id in constants so rollouts are deliberate (Workers AI models).
AI Gateway: when to put it in front
AI Gateway adds:
- Request analytics and error rates
- Caching for identical prompts (great for repeated URLs)
- Rate limiting and provider fallbacks in larger architectures
For a single Workers AI binding on one experiment, Gateway is optional. For a product that fans out across models or teams, put Gateway early. The AI Gateway Dashboard experiment shows metadata you can surface in ops UIs.
From summary to a product surface
Once summarization works, nearby experiments extend the same product family:
| Goal | Experiment | Cloudflare product |
|---|---|---|
| Topic tags for a URL | AI Website Tag Generator | Workers AI |
| Q&A over your corpus | RAG Mini Search | Workers AI + Vectorize |
| Managed RAG | AI Search Demo | AI Search |
| Generic model access | Cloud AI Proxy | Workers AI |
| Gateway observability | AI Gateway Dashboard | AI Gateway |
A common product path: summarize → tag → embed → search. Each step stays a focused Worker you can deploy independently - matching the catalog’s single-responsibility rule.
Cost, latency, and safety
- Cap input characters and max tokens
- Cache summaries keyed by URL + content hash (Cache API or KV)
- Do not send secrets or private HTML to a model without a data policy
- Rate-limit public endpoints; summarizers get abused as free proxies
- Prefer allowlisted hosts for internal “summarize our docs” tools
Try the reference
- Skim Workers AI get started
- Open AI Website Summary
- Deploy with the button or
wrangler deployfromapps/experiments/ai-website-summary - Call the documented endpoint with a public article URL
- Inspect JSON summary and error paths
- Add AI Gateway, auth, or caching before production traffic
You should have a working edge summarizer in minutes, then harden it for your traffic profile instead of starting from a blank AI template.
Production checklist
- Pin a model id from the Workers AI model catalog.
- Cap extracted characters and max tokens.
- Cache by URL + content hash (Cache API or KV).
- Put AI Gateway in front when you need analytics or cross-team controls.
- Escalate JS-heavy pages through Browser Rendering before summarize.
- Rate-limit and authenticate public summarizers.
- Log model, token estimates, and extract length - not raw private HTML.
Prompt and evaluation loop
Ship with a golden set of 10–20 URLs (news, docs, SPA, empty page, paywall). For each, assert:
- Summary is non-empty when extract succeeded
- Refusal / clear message when extract failed
- Latency under your SLO
- No leakage of nav chrome phrases (“Accept cookies”, “Sign in”)
Re-run the set when changing models or extractors.
FAQ
Which model should I pick?
Start with a small instruct model for cost, then upgrade only if quality fails your golden set. See Workers AI models.
Is Workers AI enough for RAG?
Summarizing one URL is not RAG. For multi-doc Q&A use Vectorize via RAG Mini Search or AI Search.
Related experiments
These deployable references implement the patterns in this article. Open the docs for API details, then Deploy or clone the source.
- AI Website Summary - Summarize any webpage with Workers AI on Cloudflare's edge - fetch URL, extract content, return a concise AI summary
- AI Website Tag Generator - Generate SEO topic tags and keywords for any website using Workers AI - useful for content classification and metadata
- RAG Mini Search - Mini RAG: Vectorize retrieval + Workers AI grounded answers over experiment docs - retrieval-augmented generation reference
- Cloud AI Proxy - Call any Workers AI model with a prompt from one public Worker endpoint - simple edge proxy for Workers AI inference
- AI Gateway Dashboard - Route Workers AI through Cloudflare AI Gateway and surface cache hits, latency, and gateway metadata for observability demos
- Browser Markdown Scrape - Convert any URL to Markdown or scrape CSS with Browser Rendering quickAction - JS-rendered pages to clean text for LLMs