Token counting, model quotas, and sliding window rate limiter
Find a file
Snider d74811f2d0
Some checks failed
Security Scan / security (push) Successful in 7s
Test / test (push) Failing after 40s
feat: modernise to Go 1.26 — slices.DeleteFunc, iterators, range
- Use slices.DeleteFunc in prune() for cleaner time-window filtering
- Add Models() iter.Seq[string] and Iter() iter.Seq2[string, ModelStats]
- Use range over int in benchmarks and tests
- Update docs example to modern range syntax

Co-Authored-By: Gemini <noreply@google.com>
Co-Authored-By: Virgil <virgil@lethean.io>
2026-02-23 05:14:19 +00:00
.forgejo/workflows ci: add Forgejo Actions test and security scan workflows 2026-02-23 03:28:08 +00:00
docs feat: modernise to Go 1.26 — slices.DeleteFunc, iterators, range 2026-02-23 05:14:19 +00:00
CLAUDE.md docs: graduate TODO/FINDINGS into production documentation 2026-02-20 15:01:55 +00:00
go.mod chore: sync workspace dependency versions 2026-02-22 21:41:59 +00:00
go.sum chore: sync workspace dependency versions 2026-02-22 21:41:59 +00:00
ratelimit.go feat: modernise to Go 1.26 — slices.DeleteFunc, iterators, range 2026-02-23 05:14:19 +00:00
ratelimit_test.go feat: modernise to Go 1.26 — slices.DeleteFunc, iterators, range 2026-02-23 05:14:19 +00:00
README.md docs: add README with quick start and docs links 2026-02-20 15:11:19 +00:00
sqlite.go feat(persist): Phase 2 — SQLite backend with WAL mode 2026-02-20 07:50:48 +00:00
sqlite_test.go feat: modernise to Go 1.26 — slices.DeleteFunc, iterators, range 2026-02-23 05:14:19 +00:00

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: forge.lthn.ai/core/go-ratelimit Licence: EUPL-1.2 Language: Go 1.25

Quick Start

import "forge.lthn.ai/core/go-ratelimit"

// YAML backend (default, single-process)
rl, err := ratelimit.New()

// SQLite backend (multi-process)
rl, err := ratelimit.NewWithSQLite("~/.core/ratelimits.db")
defer rl.Close()

ok, reason := rl.CanSend("gemini-2.0-flash", 1500)
if ok {
    rl.RecordUsage("gemini-2.0-flash", 1500)
}

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 test ./...
go test -race ./...
go vet ./...
go build ./...

Licence

European Union Public Licence 1.2 — see LICENCE for details.