throttle

package
v0.14.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 22, 2026 License: MIT Imports: 14 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package throttle enforces per-model RPM via GCRA, daily token budgets and monetary spend limits in Redis, shared across replicas. A broken Redis degrades to "no limits", never "no service", unless spend fails closed.

Index

Constants

View Source
const (

	// DefaultOutputReserve is the output tokens reserved when a call sets no
	// ceiling of its own.
	DefaultOutputReserve = 4096
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Redis

type Redis struct {
	// contains filtered or unexported fields
}

func New

func New(client *redis.Client, keyPrefix string) *Redis

func (*Redis) AllowRPM

func (t *Redis) AllowRPM(ctx context.Context, keyName, userID string, m *proxydb.Model) bool

AllowRPM evaluates this attempt against the key's and user's rate limits using GCRA atomically.

func (*Redis) ChargeDailyBudget

func (t *Redis) ChargeDailyBudget(ctx context.Context, m *proxydb.Model, keyName, userID, teamID string, usage llm.TokensUsage)

ChargeDailyBudget books actual batch usage against the daily token budgets and, priced at the batch rate, against the spend limits.

func (*Redis) CheckDailyBudget

func (t *Redis) CheckDailyBudget(ctx context.Context, m *proxydb.Model, keyName, userID, teamID string) string

CheckDailyBudget reports a daily token budget or spend limit for this model that is already at or beyond its cap, or "" if the call may proceed.

func (*Redis) Reserve

func (t *Redis) Reserve(ctx context.Context, m *proxydb.Model, keyName, userID, teamID string, estimate, maxOutput int64) (*router.Reservation, string)

Reserve books the estimate against every applicable daily budget, then the priced worst case against every spend limit. Supports budget_grace_margin: allows the call if the token budget was not already exhausted before.

func (*Redis) SetDefaultOutputReserve

func (t *Redis) SetDefaultOutputReserve(tokens int64)

SetDefaultOutputReserve sets the output tokens priced into a spend reservation when the call has no output ceiling.

func (*Redis) SetGraceMargin

func (t *Redis) SetGraceMargin(margin float64)

func (*Redis) SetSpendFailClosed

func (t *Redis) SetSpendFailClosed(closed bool)

SetSpendFailClosed refuses calls under a spend limit while Redis is unreachable instead of letting them through unmetered.

func (*Redis) SetSpendLimitResolver

func (t *Redis) SetSpendLimitResolver(resolver SpendLimitResolver)

func (*Redis) SetUserRateLimitResolver

func (t *Redis) SetUserRateLimitResolver(resolver UserRateLimitResolver)

func (*Redis) Settle

func (t *Redis) Settle(res *router.Reservation, usage llm.TokensUsage)

Settle adjusts every reserved budget from the estimate to the vendor's actual token count, and every spend counter from the reserved worst case to the priced actuals. Fire-and-forget: settling must never block a response.

func (*Redis) SpendSoFar added in v0.14.0

func (t *Redis) SpendSoFar(ctx context.Context, subject, subjectID, period string) (int64, time.Time, error)

SpendSoFar reads a subject's spend counter for the current window, the one Reserve checks; nano-USD, 0 when nothing has been spent yet.

type SpendLimitResolver

type SpendLimitResolver func(subjectType, subjectID, period string) int64

SpendLimitResolver returns the cap in nano-USD for a subject and period, 0 for none.

type UserRateLimitResolver

type UserRateLimitResolver func(userID, modelID string) *proxydb.UserRateLimit

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL