Files
causius/lib/limits.ts
CatoandClaude 5710d86b3e chore(deps): bip-kit, limitkit, ai-forms and threadkit from the @bitbaum scope (#143)
Owner, 2026-11-06: the fleet's packages belong under the @bitbaum npm
organization (fleet docs/package-scope-migration-plan.md). Same code and
versions; dependency keys and import specifiers moved with
scripts/packages/rename-package.mjs from fleet.


Claude-Session: https://claude.ai/code/session_019MDtmvpWNTLxXNfwUHS7MD

Co-authored-by: Claude <noreply@anthropic.com>
2026-11-10 11:17:49 +01:00

42 lines
2.2 KiB
TypeScript

import { clientIp, slidingWindow, toHeaders } from "@bitbaum/limitkit";
/**
* Rate limits, as limitkit decisions. In-memory and per process, which is the
* right size for blunting scripted abuse of a free tier on one box and the
* wrong size for billing — nothing here is billing.
*
* The free analysis is the scarce thing: it draws on a daily budget shared by
* every visitor, so one caller may not drain it. A reader on their own key
* spends their own budget and gets a much wider window.
*/
export const limits = {
analyzeFree: slidingWindow({ limit: 8, windowMs: 60 * 60_000 }),
analyzeOwn: slidingWindow({ limit: 60, windowMs: 60 * 60_000 }),
// A question without a matter (/api/ask, the fleet's bot contract): cheaper
// than a letter, dearer than a probe, on the same shared daily budget.
askFree: slidingWindow({ limit: 30, windowMs: 60 * 60_000 }),
askOwn: slidingWindow({ limit: 200, windowMs: 60 * 60_000 }),
modelCheck: slidingWindow({ limit: 30, windowMs: 10 * 60_000 }),
// Keyed by actor, not IP: a signed-in person's budget follows them.
turnFree: slidingWindow({ limit: 30, windowMs: 60 * 60_000 }),
turnOwn: slidingWindow({ limit: 200, windowMs: 60 * 60_000 }),
matterCreate: slidingWindow({ limit: 20, windowMs: 60 * 60_000 }),
upload: slidingWindow({ limit: 60, windowMs: 60 * 60_000 }),
// A spoken take or a photographed page each cost a model call on the shared free tier.
transcribe: slidingWindow({ limit: 40, windowMs: 10 * 60_000 }),
pages: slidingWindow({ limit: 20, windowMs: 60 * 60_000 }),
// Profiles, credentials and requests to helpers: people writing, not a model.
helperWrite: slidingWindow({ limit: 60, windowMs: 60 * 60_000 }),
engage: slidingWindow({ limit: 20, windowMs: 60 * 60_000 }),
// Anyone may check an attestation; a script may not hammer it.
verify: slidingWindow({ limit: 60, windowMs: 10 * 60_000 }),
};
export function callerKey(request: Request, scope: string): string {
return `${scope}:${clientIp(request.headers)}`;
}
export function tooMany(result: Parameters<typeof toHeaders>[0]): Response {
return Response.json({ error: "rate_limited" }, { status: 429, headers: toHeaders(result) });
}