RateLimit enforces sliding-window quotas per key, such as a user ID, API key, IP address, or tenant. It supports weighted request costs, non-throwing checks with check, guard wrappers with guard, and manual resets.
Use RateLimit to protect login routes, public APIs, LLM endpoints, and outbound webhooks from bursts and abuse.
Declaring a RateLimit
Plain TypeScript SDK (@chimp-labs/keel/sdk)
import { k } from "@chimp-labs/keel/sdk"
export const LoginLimiter = k.rateLimit("auth.login", {
limit: 5,
window: "1 minute",
})
export const ApiTokenLimiter = k.rateLimit("api.tokens", {
limit: 1000,
window: "1 hour",
})
Configuration options
| Option |
Type |
Required |
Description |
limit |
number |
Yes |
Maximum total cost units permitted within each window. |
window |
Duration.Input |
Yes |
Sliding window duration, for example "1 minute" or "1 hour". |
Effect v4 (@chimp-labs/keel/rate-limit)
import { RateLimit } from "@chimp-labs/keel/rate-limit"
export class LoginLimit extends RateLimit<"login", `ip:${string}`>("login", {
limit: 5,
window: "1 minute",
}) {}
Attaching to a Service
export const AuthService = k.service("auth", {
resources: [SessionsKv, LoginLimiter],
operations: { /* ... */ },
})
Operations reference
Guarding operations and checking quotas
// Guard an operation — throws RateLimitExceededError (HTTP 429) if over quota
const session = await keel.loginLimiter.guard(
`ip:${clientIp}`,
async () => authenticateUser(email, password),
{ cost: 1 }
)
// Guard without a callback (just consume quota or throw 429)
await keel.apiLimiter.guard(`tenant:${tenantId}`, undefined, { cost: 10 })
// Non-throwing inspection (returns RateLimitDecision)
const decision = await keel.apiLimiter.check(`tenant:${tenantId}`, 1)
// { allowed: boolean, remaining: number, limit: number, resetAt: Date, retryAfterMs: number }
// Clear quota state for a key (e.g. after successful MFA or admin reset)
await keel.loginLimiter.reset(`ip:${clientIp}`)
Complete SDK and Effect API mapping
SDK method (keel.<limiter>.*) |
Effect function (@chimp-labs/keel/rate-limit) |
Description |
check(key, cost?) |
checkRateLimit(limiter, key, cost?) |
Consume cost if allowed and return RateLimitDecision. |
guard(key, fn?, { cost? }) |
guardRateLimit(limiter, key, effect, cost?) |
Run fn if allowed, or fail with RateLimitExceededError. |
reset(key) |
resetRateLimit(limiter, key) |
Reset accumulated quota for key. |
Errors and failure modes
| Error tag |
Category |
HTTP status |
Retryable |
When it occurs |
RateLimitExceededError |
rate_limited |
429 |
Yes |
Request exceeds limit within window (includes retryAfterMs). |
RateLimiterError |
internal |
500 |
Yes |
Backend rate-limiter storage fails. |
Backends and infrastructure
- Memory tracks exact sliding-window buckets using Effect’s
Clock, which makes tests deterministic with TestClock.
- On PostgreSQL 17 (
KeelPostgresLive or backend: "postgres"), SQLite, and SurrealDB 4, createKeel uses RateLimiterMemory by default. For shared multi-replica quota tracking, provide a custom RateLimiter layer, for example one backed by PostgreSQL AtomicKV incr in keel_kv or Redis.
Testing in memory
await using keel = await createKeel({
backend: "memory",
resources: { loginLimiter: LoginLimiter },
})
for (let i = 0; i < 5; i++) {
await keel.loginLimiter.guard("ip:1.2.3.4")
}
const blocked = await tryKeel(() => keel.loginLimiter.guard("ip:1.2.3.4"))
// blocked.ok === false, blocked.error.tag === "RateLimitExceededError", blocked.error.httpStatus === 429