← All ShelfBot Guides
Edge Architecture Updated 2026-12-01 · 6 min read

Self-Hosted AI Chatbot on Cloudflare Workers Free: BYOK, 4.7KB Widget & Hard Rate Caps (2026 Architecture)

Most hosted chatbot platforms charge a monthly seat fee to forward your visitors' messages to a model API you could call yourself. For most support, sales-qualification, and FAQ bots, the only real infrastructure you need is a tiny edge function, an API key you own, and a set of guardrails that never change behavior under pressure. This guide covers the architecture ShelfBot ships and the reasoning behind each layer.

1. Edge Workers Free + BYOK: Remove the Monthly Lock-In

The Cloudflare Workers Free plan allows 100,000 requests per day. A chatbot turn is one request, so that ceiling covers the traffic of most small SaaS products, stores, and agency client sites with plenty of margin.

With Bring Your Own Key (BYOK), your model API key is stored as an encrypted Worker secret. The browser never sees it. The Worker receives the visitor's message, applies guardrails, calls the model provider, and returns the grounded reply. Model usage is billed straight to your account at provider rates, with no markup on top.

  • No per-seat or per-conversation fee. Your costs are model tokens plus $0 hosting at free-tier volume.
  • No vendor migration risk. The code sits in your repository and deploys with wrangler deploy.
  • CPU budget fits. Workers Free limits CPU time to roughly 10ms per request, and time spent waiting on an upstream fetch does not count against it.

2. A 4.7KB Async Widget With Zero Third-Party Cookies

The embed is one script tag loaded with async, so it never blocks rendering or hurts Largest Contentful Paint. Lightweight at 4.7KB, it renders a launcher button and clean chat panel without bulky framework bundles.

<script async src="https://bot.example.com/widget.js" data-bot="support"></script>

The widget sets no cookies and loads no third-party analytics or tracking pixels, adding zero third-party cookie consent bloat.

3. Deterministic Input/Output Guardrails

Asking the model to behave is not a security control. Every message passes through deterministic code before and after the model call, returning the exact same verdict for the same input every time.

Input pipeline

  • Unicode normalization. Apply NFKC, strip zero-width/format characters, and fold Cyrillic homoglyphs so attackers cannot hide blocked phrases or numbers.
  • Sensitive data blocking. Payment card numbers (regex plus Luhn check), US SSNs, and IBANs are rejected before they can reach the model API.
  • Prompt-injection refusals. Known override patterns receive an immediate fixed refusal. The model is never called for these, so zero tokens are spent.

Output pipeline

Model output is re-scanned for leaked API keys, prohibited advice claims, and echoed PII, and rendered with safe DOM text escaping so untrusted markup can never execute.

Zero-storage logs

The Worker stores no visitor message bodies. Operational counters track aggregate volume only — with no transcript store, there is nothing to breach and nothing to retain.

4. IP + Global Flood Caps: 429 Before the Bill Explodes

A public chatbot endpoint is a public path into your API budget. ShelfBot stacks two hard limits:

  • Per-IP cap: limits daily requests per hashed client IP.
  • Global flood cap: a hard daily ceiling on total model calls across all visitors. Once triggered, requests return 429 Too Many Requests and the model is never called.
if (await overIpLimit(ipHash) || await overGlobalLimit()) {
  return new Response('Rate limited', { status: 429 });
}

Combined with input length caps and a strict max_tokens ceiling, your worst-case daily spend is mathematically bounded in advance.

5. How ShelfBot Packages It: $19 One-Time ZIP

Each ShelfBot kit is a $19 one-time download containing Worker source, the 4.7KB widget, deterministic guardrails, a grounded industry FAQ knowledge file, and a 10-minute deploy guide. Deploy it to your own Cloudflare account and you own it outright.

Want to inspect the open baseline first? Browse our free MIT-licensed starter project on GitHub: shelfbot-starter on GitHub.

Frequently Asked Questions

Is the Cloudflare Workers Free plan enough for a production chatbot?

For most small and mid-size sites, yes. The free plan allows 100,000 requests per day, and waiting on an upstream model API fetch does not count toward per-request CPU time. High-traffic sites can move to Workers Paid without changing code.

Where is my API key stored, and can visitors see it?

Your model API key is stored as an encrypted Cloudflare Worker secret and used only on the server side. The browser widget never receives the key, so it cannot be extracted from page source or network traffic.

How do the rate caps keep my API costs predictable?

Every request is checked against a per-IP limit and a global limit before the model is called. When either limit trips, the Worker returns HTTP 429 and spends zero tokens. Combined with input length caps and max_tokens, worst-case daily spend is fixed ahead of time.

Deploy a Pre-Hardened ShelfBot Kit in 10 Minutes

Every $19 kit ships with grounded FAQ JSON, deterministic security guardrails, EU AI Act Art. 50 disclosure badge, 4.7KB async widget, and zero monthly credit fees.

Try Live Sandboxes Now → Buy Any Bot Kit — $19 Compare Plans