Documentation
¶
Overview ¶
Package middleware provides reusable model.Client middlewares such as adaptive rate limiting.
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ErrRequestTooLarge = errors.New("model middleware: request exceeds rate limiter capacity")
ErrRequestTooLarge is returned when a single request's token cost exceeds the limiter's maximum tokens-per-minute capacity. Such a request can never be admitted no matter how long it waits, so the limiter fails fast instead of blocking. Callers should raise the limiter's max TPM or reduce the request size.
Functions ¶
This section is empty.
Types ¶
type AdaptiveRateLimiter ¶
type AdaptiveRateLimiter struct {
// contains filtered or unexported fields
}
AdaptiveRateLimiter applies an AIMD-style adaptive token bucket on top of a model.Client. NewAdaptiveRateLimiter charges estimated input tokens. NewOutputReservationAdaptiveRateLimiter charges exact input tokens plus the requested maximum output. Both adjust effective tokens-per-minute capacity from provider rate-limit signals.
The limiter is process-local and designed to sit at the provider client boundary. Callers construct a single instance per process and wrap the underlying model.Client with Middleware before passing it to planners or runtimes.
func NewAdaptiveRateLimiter ¶
func NewAdaptiveRateLimiter(ctx context.Context, m *rmap.Map, key string, initialTPM, maxTPM float64) *AdaptiveRateLimiter
NewAdaptiveRateLimiter constructs an AdaptiveRateLimiter with a tokens-per-minute budget. When m and key are set, it coordinates capacity across processes using a Pulse replicated map; otherwise it operates as a process-local limiter.
func NewOutputReservationAdaptiveRateLimiter ¶ added in v2.1.0
func NewOutputReservationAdaptiveRateLimiter( ctx context.Context, m *rmap.Map, key string, initialTPM, maxTPM float64, ) *AdaptiveRateLimiter
NewOutputReservationAdaptiveRateLimiter constructs an AdaptiveRateLimiter that charges exact input tokens plus each request's positive MaxTokens value. Its versioned cluster key keeps combined-cost coordination separate from input-only limiters during rolling upgrades.
func (*AdaptiveRateLimiter) Middleware ¶
func (l *AdaptiveRateLimiter) Middleware() func(model.Client) model.Client
Middleware returns a model.Client middleware that enforces the request-cost mode selected by the constructor for both Complete and Stream calls.