Skip to content

About

Cloudflare Worker that proxies LLM calls for Quilt with rate limiting and Workers AI fallback. Self-host your own in 15 min.

Resources

Code of conduct

Contributing

Security policy

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

3 Commits

Folders and files

Repository files navigation

quilt-llm-worker

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:

  1. Hides API keys server-side — the browser never sees them
  2. Rate-limits per IP — 5/min burst, 50/day per IP, 5000/day global (configurable)
  3. Falls back to Workers AI — when no premium provider is requested, uses the free @cf/meta/llama-3.1-8b-instruct model
  4. Returns a graceful 429 — when limits are hit, the response includes a link to self-host

Endpoints

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

Request format

{
  "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
}

Response format

{
  "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"
}

Rate-limit response (HTTP 429)

{
  "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
}

Setup (Cloudflare)

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 deploy

The 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');

Setup (local)

For development, run:

npm install
npx wrangler dev

This 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');

The four problems this solves

  1. API keys stay server-side. The browser never sees your z.ai / Kimi / DeepSeek tokens.
  2. Rate limits protect Casey's wallet. Per-IP and per-day limits prevent abuse.
  3. Workers AI is the cheap default. When prefer_cheap: true (the default), every call uses the free Llama 3.1 8B on Workers AI. Only premium: true triggers the paid providers.
  4. 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.

The watch extended

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.

License

MIT

About

Cloudflare Worker that proxies LLM calls for Quilt with rate limiting and Workers AI fallback. Self-host your own in 15 min.

Resources

Code of conduct

Contributing

Security policy

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages