2026-03-27 04:23:34 +00:00
<!-- SPDX - License - Identifier: EUPL - 1.2 -->
[](https://pkg.go.dev/dappco.re/go/core/go-ratelimit)

2026-02-23 06:45:45 +00:00
[](go.mod)
2026-02-20 15:11: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.
2026-03-27 04:23:34 +00:00
**Module**: `dappco.re/go/core/go-ratelimit`
2026-02-20 15:11:19 +00:00
**Licence**: EUPL-1.2
2026-03-27 04:23:34 +00:00
**Language**: Go 1.26
2026-02-20 15:11:19 +00:00
## Quick Start
```go
2026-03-27 04:23:34 +00:00
import "dappco.re/go/core/go-ratelimit"
2026-02-20 15:11:19 +00:00
// YAML backend (default, single-process)
rl, err := ratelimit.New()
2026-03-27 04:23:34 +00:00
if err != nil {
log.Fatal(err)
}
2026-02-20 15:11:19 +00:00
// SQLite backend (multi-process)
2026-03-27 04:23:34 +00:00
rl, err = ratelimit.NewWithSQLite("/tmp/ratelimits.db")
if err != nil {
log.Fatal(err)
}
2026-02-20 15:11:19 +00:00
defer rl.Close()
2026-03-27 04:23:34 +00:00
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)
2026-02-20 15:11:19 +00:00
}
```
2026-03-30 08:16:44 +00:00
For agent workflows, `Decide` returns a structured verdict with retry guidance:
```go
decision := rl.Decide("gemini-2.0-flash", 1500)
if !decision.Allowed {
log.Printf("throttled (%s); retry after %s", decision.Code, decision.RetryAfter)
}
```
2026-02-20 15:11:19 +00:00
## Documentation
- [Architecture ](docs/architecture.md ) — sliding window algorithm, provider quotas, YAML and SQLite backends
- [Development Guide ](docs/development.md ) — prerequisites, test patterns, coding standards
- [Project History ](docs/history.md ) — completed phases with commit hashes, known limitations
## Build & Test
```bash
2026-03-27 04:23:34 +00:00
go build ./...
2026-02-20 15:11:19 +00:00
go test ./...
go test -race ./...
go vet ./...
2026-03-27 04:23:34 +00:00
go test -cover ./...
go mod tidy
2026-02-20 15:11:19 +00:00
```
## Licence
2026-03-27 04:23:34 +00:00
European Union Public Licence 1.2.