Docs menu

Guides

Adaptive rate limiting

A static rate limit is a guess made once, at config time, about traffic that changes every day. AdaptiveLimiter wraps the configured limiter and auto-tunes the effective policy from the same success/failure signal the circuit breaker already observes — no ML, no external model, just the same AIMD shape TCP congestion control has used for decades: additive growth on healthy traffic, multiplicative cut when errors rise.

Turn it on

Go
rg := rateguard.New(rateguard.Config{
    Preset:            "llm-heavy",
    AdaptiveRateLimit:  true,
    Adaptive: rateguard.AdaptiveOptions{
        TargetErrorRate: 0.05, // default
        MinFactor:       0.25, // floor: 25% of configured policy
        MaxFactor:       2.0,  // ceiling: 200% of configured policy
    },
})

The policy stays the anchor

Adaptation scales your configured preset within [MinFactor, MaxFactor] — it never replaces it. Turn it off and the effective limit snaps back to exactly what you configured. Peek scales identically to Allow, so agent pre-flight answers stay honest while the limit is being adjusted.

How the controller decides

Every request outcome (success = HTTP status < 500) updates an exponential moving average of the error rate. Once per AdjustInterval (default 1s), the controller compares that EMA against TargetErrorRate:

ConditionAction
Error EMA ≥ 80% of targetCut the effective limit multiplicatively (factor × DecreaseFactor, default 0.5) — before the error rate actually breaches the target, so the limiter sheds load before the circuit breaker would have to trip.
Error EMA below the triggerGrow the effective limit additively (factor + IncreaseStep, default 0.05 per interval).

This predictive 80% trigger is deliberate: reacting only once the breaker's own threshold is breached means the breaker and the limiter fight the same fire at the same time. Cutting earlier gives the limiter first crack at recovery.

All options

FieldDefaultMeaning
MinFactor0.25Floor — the effective limit never drops below 25% of the configured policy.
MaxFactor2.0Ceiling — the effective limit never exceeds 200% of the configured policy.
TargetErrorRate0.05The error rate the controller steers under.
IncreaseStep0.05Additive factor gain per healthy interval.
DecreaseFactor0.5Multiplicative cut applied on breach.
AdjustInterval1sMinimum time between controller decisions — rate-limits the rate limiter's own adjustments.
EMAAlpha0.2Weight given to each new outcome sample in the error-rate moving average.