- requests from one address
- 68
- refused by the first limiter
- 0
- the limit
- 10 / min
- first request the fix refuses
- 11th
What we built
On October 20 we moved Ask Infinary, the chat on this site, to Claude Haiku 5.5. The same change added abuse limits, because a public chat box that calls a paid model is an obvious target. The main one was a per-address limit of 10 requests a minute, using Cloudflare’s (Cloudflare's built-in Workers API for rate limits. Each machine checks a locally cached counter, synced with the location in the background, so it's approximate by design.).
The binding is a few lines of config and one call, and we read it as “10 a minute per Cloudflare location”:
return !(await limiter.limit({ key: `chat:${ip}` })).success;It passed its tests, it returned a 429 on the 11th request when we ran the built Worker locally under (Cloudflare's open-source Workers runtime. Wrangler runs it locally for development, so local tests use the same engine as production.), and it shipped that evening.
What broke
The probes sent empty message lists, which the limiter counts before the chat refuses them, so they cost no model calls.
Show data
| went through | refused with 429 | |
|---|---|---|
| 14 quick | 14 | 0 |
| 30 in 4 s | 30 | 0 |
| 24 over 39 s | 24 | 0 |
| 15, 2.5 s apart | 10 | 5 |
The first three probes ran against the binding; the last one ran against the fix, described below. We ruled out the boring causes first:
- The binding was there. The live Worker version listed it.
- The code called it correctly. The compiled bundle awaited
limit()and readsuccess, and no error was logged. - The same bundle worked locally. Under
wrangler devit refused the 11th request, every time.
Why
One client’s requests are spread across many machines in a location, so no single machine’s view of the count reached 10 before the minute was up. Locally there is only one machine, which is why every local test passed.
We didn’t instrument production to watch the split happen. So that part is the documented behaviour plus what we measured, not something we observed directly.
What we changed
We replaced the binding with a (A Cloudflare Workers feature: a single, named instance with its own storage. Every request for the same name reaches the same object, wherever it comes from.), one per client. Every request from an address goes to the object named for it, whichever Worker (One running copy of a Worker inside the Workers runtime. A busy site runs many at once, on many machines, and they share no memory.), machine or location handles the request, so the count is exact: a per-client limit with an exact window.
return !(await limiter.limit({ key: `chat:${ip}` })).success;return await limiters.get(limiters.idFromName(chatRateLimitKey(ip))).isLimited();The window is a sliding one. A request at time is allowed when fewer than 10 counted requests fall in the minute before it:
where is the stored list of requests the object has let through. A refused request is never added to , so hammering the endpoint doesn’t push the reset further away. The object itself is short:
async isLimited(now = Date.now()): Promise<boolean> { const cutoff = now - CHAT_RATE_WINDOW_MS; const hits = ((await this.ctx.storage.get<number[]>(HITS_KEY)) ?? []).filter((t) => t > cutoff); if (hits.length >= CHAT_RATE_LIMIT) return true; hits.push(now); await this.ctx.storage.put(HITS_KEY, hits); await this.ctx.storage.setAlarm(now + CHAT_RATE_WINDOW_MS); return false;}The tests cover a 12-request burst from one address: 10 requests through, then two 429s, all counted in one object, while a second address is unaffected. Locally, the count also survived the object dropping out of memory: after 12 seconds idle, the next request was still refused.
The evening, in order
- The chat moves to Claude Haiku 5.5, with the rate-limiting binding. The site deploys within a minute.
- The check after the deploy: 68 requests from one address, no 429.
- The Durable Object fix goes up for review.
- Review finds why the limiter failed on a Windows machine: the path limit (below).
- The fix merges and the site deploys.
- The live check: 15 requests, 2.5 seconds apart. Ten go through; the 11th gets a 429.
One more surprise, on Windows
[chat] Rate limiter failed: internal error; reference = …| Runtime | Path to the object’s SQLite file | Result |
|---|---|---|
| Windows | 215 characters | 10 requests through, then 429 |
| Windows | 250 characters | 10 requests through, then 429 |
| Windows | 266 characters | every call internal error |
| Linux | 329 characters | 10 requests through, then 429 |
Production storage isn’t a Windows filesystem, so this is local only. The fix is a short --persist-to, such as C:/wst, and a note next to the binding in wrangler.jsonc.