A Cloudflare Worker that proxies LLM calls for the Quilt ecosystem — with rate limiting, Workers AI fallback, and a graceful "Casey's free limit is used up" path to self-hosting.
Browser → Worker (rate-limited, cheap default) → z.ai / Kimi / DeepSeek
(or Workers AI)
This Worker exists because superinstance.dev is a public site and was burning Casey's API keys with no rate limit. The Worker solves four problems at once:
- Hides API keys server-side — the browser never sees them
- Rate-limits per IP — 5/min burst, 50/day per IP, 5000/day global (configurable)
- Falls back to Workers AI — when no premium provider is requested, uses the free
@cf/meta/llama-3.1-8b-instructmodel - Returns a graceful 429 — when limits are hit, the response includes a link to self-host
| Path | Method | Purpose |
|---|---|---|
/ or /health |
GET | Health check |
/status |
GET | Rate-limit status for the calling IP |
/v1/chat/completions |
POST | OpenAI-compatible chat completions (proxied to z.ai/Kimi/DeepSeek or Workers AI) |
/chat |
POST | Alias for /v1/chat/completions |
{
"messages": [
{"role": "system", "content": "You are..."},
{"role": "user", "content": "Hello"}
],
"provider": "workers-ai", // workers-ai | zai | kimi | deepseek
"model": "@cf/meta/llama-3.1-8b-instruct", // optional
"max_tokens": 2000,
"temperature": 0.7,
"prefer_cheap": true, // default true: downgrades to Workers AI unless premium=true
"premium": false // set true to allow non-Workers-AI providers
}{
"content": "Hello! How can I help?",
"model": "@cf/meta/llama-3.1-8b-instruct",
"provider": "workers-ai",
"usage": {
"daily": 3,
"global": 142,
"burst": 1,
"dailyLimit": 50,
"globalLimit": 5000,
"burstLimit": 5
},
"version": "1.0.0"
}{
"error": "rate_limit",
"reason": "per_ip_daily",
"message": "Casey's free limit is used up for your IP (50/day). Run your own — see https://superinstance.dev/self-host.html",
"selfHostUrl": "https://superinstance.dev/self-host.html",
"usedToday": 50,
"limit": 50
}git clone https://github.com/SuperInstance/quilt-llm-worker
cd quilt-llm-worker
npm install
npx wrangler login
npx wrangler kv:namespace create "QUILT_LIMITS"
# paste the id into wrangler.toml
# Optional — set premium provider keys
npx wrangler secret put ZAI_TOKEN
npx wrangler secret put KIMI_TOKEN
npx wrangler secret put DEEPSEEK_TOKEN
# Deploy
npx wrangler deployThe deploy output will give you a https://quilt-llm-worker.YOUR_SUBDOMAIN.workers.dev URL. Set this in the Quilt pages:
localStorage.setItem('quilt_worker_url', 'https://quilt-llm-worker.YOUR_SUBDOMAIN.workers.dev');For development, run:
npm install
npx wrangler devThis starts the Worker locally at http://localhost:8787. The Quilt pages will pick it up if you set:
localStorage.setItem('quilt_worker_url', 'http://localhost:8787');- API keys stay server-side. The browser never sees your z.ai / Kimi / DeepSeek tokens.
- Rate limits protect Casey's wallet. Per-IP and per-day limits prevent abuse.
- Workers AI is the cheap default. When
prefer_cheap: true(the default), every call uses the free Llama 3.1 8B on Workers AI. Onlypremium: truetriggers the paid providers. - Graceful degradation. When limits are hit, the response is a 429 with a JSON body pointing to the self-host guide. The Quilt pages display a clean "Casey's free limit is used up — run your own" message.
This Worker is a cell in the Quilt sheet. The LLM provider is a cell. The rate-limit counter is a cell. The "graceful 429" is a cell. When the Worker is at its daily cap, the sheet reads "self-host now" instead of "503 service unavailable." The user is the watch; the watch sees when the budget is hit; the watch says "this is yours now."
See essay 45 for the broader context, and self-host.html for the user-facing guide.
MIT