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 Requestsand 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.
- Qualify inbound demo requests with the B2B SaaS Demo Qualifier Chatbot.
- Deflect return and refund tickets with the Shopify Returns & Refund Chatbot.
- Compare all single kits, 5-site agency packs, and bundles on the pricing page.
Want to inspect the open baseline first? Browse our free MIT-licensed starter project on GitHub: shelfbot-starter on GitHub.