Token counting, model quotas, and sliding window rate limiter
Find a file
Claude 138ec82897
fix(ratelimit): AX compliance pass 1 — naming, comments, test coverage
Rename short parameter names to predictable full names per AX principle 1:
cfg → configuration, dbPath → databasePath, db → database (local var).

Add usage-example comments to newSQLiteStore and createSchema per AX principle 2.

Add mandatory missing test categories:
- ratelimit_test.go: TestRatelimit_InvalidInputs_Bad (was missing _Bad)
- error_test.go: TestError_EdgeCases_Ugly (was missing _Ugly)
- iter_test.go: TestIter_CountTokensErrors_Bad (was missing _Bad)

All tests pass: go test ./... -short -count=1

Co-Authored-By: Virgil <virgil@lethean.io>
2026-03-31 08:32:48 +01:00
.core chore: add .core/ build and release configs 2026-03-06 18:52:37 +00:00
.forgejo/workflows ci: add Forgejo Actions test and security scan workflows 2026-02-23 03:28:08 +00:00
.github/workflows ci: add Core ecosystem CI workflow with CodeRabbit auto-fix 2026-03-17 14:05:50 +00:00
docs feat(ratelimit): add agent decision guidance 2026-03-30 08:16:44 +00:00
specs feat(ratelimit): add agent decision guidance 2026-03-30 08:16:44 +00:00
.editorconfig chore: add Go repo norms (badges, contributing, lint, taskfile, editorconfig) 2026-02-23 06:45:45 +00:00
.gitignore chore: add .core/ and .idea/ to .gitignore 2026-03-15 10:17:50 +00:00
.golangci.yml chore: add Go repo norms (badges, contributing, lint, taskfile, editorconfig) 2026-02-23 06:45:45 +00:00
CLAUDE.md fix(ratelimit): align module metadata and repo guidance 2026-03-27 04:23:34 +00:00
CONTRIBUTING.md fix(ratelimit): align module metadata and repo guidance 2026-03-27 04:23:34 +00:00
error_test.go fix(ratelimit): AX compliance pass 1 — naming, comments, test coverage 2026-03-31 08:32:48 +01:00
go.mod fix(ratelimit): align module metadata and repo guidance 2026-03-27 04:23:34 +00:00
go.sum refactor(ratelimit): upgrade to core v0.8.0-alpha.1 2026-03-26 15:41:11 +00:00
iter_test.go fix(ratelimit): AX compliance pass 1 — naming, comments, test coverage 2026-03-31 08:32:48 +01:00
ratelimit.go fix(ratelimit): AX compliance pass 1 — naming, comments, test coverage 2026-03-31 08:32:48 +01:00
ratelimit_test.go fix(ratelimit): AX compliance pass 1 — naming, comments, test coverage 2026-03-31 08:32:48 +01:00
README.md feat(ratelimit): add agent decision guidance 2026-03-30 08:16:44 +00:00
sqlite.go fix(ratelimit): AX compliance pass 1 — naming, comments, test coverage 2026-03-31 08:32:48 +01:00
sqlite_test.go fix(ratelimit): align module metadata and repo guidance 2026-03-27 04:23:34 +00:00

Go Reference Licence: EUPL-1.2 Go Version

go-ratelimit

Provider-agnostic sliding window rate limiter for LLM API calls. Enforces requests per minute (RPM), tokens per minute (TPM), and requests per day (RPD) quotas per model using an in-memory sliding window. Ships with default quota profiles for Gemini, OpenAI, Anthropic, and a local inference provider. State persists across process restarts via YAML (single-process) or SQLite (multi-process, WAL mode). Includes a Gemini-specific token counting helper and a YAML-to-SQLite migration path.

Module: dappco.re/go/core/go-ratelimit Licence: EUPL-1.2 Language: Go 1.26

Quick Start

import "dappco.re/go/core/go-ratelimit"

// YAML backend (default, single-process)
rl, err := ratelimit.New()
if err != nil {
    log.Fatal(err)
}

// SQLite backend (multi-process)
rl, err = ratelimit.NewWithSQLite("/tmp/ratelimits.db")
if err != nil {
    log.Fatal(err)
}
defer rl.Close()

if rl.CanSend("gemini-2.0-flash", 1500) {
    rl.RecordUsage("gemini-2.0-flash", 1000, 500)
}

if err := rl.Persist(); err != nil {
    log.Fatal(err)
}

For agent workflows, Decide returns a structured verdict with retry guidance:

decision := rl.Decide("gemini-2.0-flash", 1500)
if !decision.Allowed {
    log.Printf("throttled (%s); retry after %s", decision.Code, decision.RetryAfter)
}

Documentation

  • Architecture — sliding window algorithm, provider quotas, YAML and SQLite backends
  • Development Guide — prerequisites, test patterns, coding standards
  • Project History — completed phases with commit hashes, known limitations

Build & Test

go build ./...
go test ./...
go test -race ./...
go vet ./...
go test -cover ./...
go mod tidy

Licence

European Union Public Licence 1.2.