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
- type Redis
- func (t *Redis) AdmitKey(ctx context.Context, keyName string, rpm int32, tpm, estimate int64) (settle func(actual int64), kind string, retry time.Duration)
- func (t *Redis) AdmitTool(ctx context.Context, keyName, server, tool string, rpm int32) (bool, time.Duration)
- func (t *Redis) AllowRPM(ctx context.Context, keyName, userID string, m *proxydb.Model) bool
- func (t *Redis) ChargeDailyBudget(ctx context.Context, m *proxydb.Model, keyName, userID, teamID string, ...)
- func (t *Redis) CheckDailyBudget(ctx context.Context, m *proxydb.Model, keyName, userID, teamID string) string
- func (t *Redis) Reserve(ctx context.Context, m *proxydb.Model, keyName, userID, teamID string, ...) (*router.Reservation, string)
- func (t *Redis) SetDefaultOutputReserve(tokens int64)
- func (t *Redis) SetGraceMargin(margin float64)
- func (t *Redis) SetSpendFailClosed(closed bool)
- func (t *Redis) SetSpendLimitResolver(resolver SpendLimitResolver)
- func (t *Redis) SetUserRateLimitResolver(resolver UserRateLimitResolver)
- func (t *Redis) Settle(res *router.Reservation, usage llm.TokensUsage)
- func (t *Redis) SpendSoFar(ctx context.Context, subject, subjectID, period string) (int64, time.Time, error)
- type SpendLimitResolver
- type UserRateLimitResolver
Constants ¶
const ( KindKeyRPM = "key_rpm" KindKeyTPM = "key_tpm" )
Refusal kinds of a key's own rates.
const ( // DefaultOutputReserve is the output tokens reserved when a call sets no // ceiling of its own. DefaultOutputReserve = 4096 )
const KindToolRPM = "mcp_tool_rpm"
KindToolRPM refuses a call above an MCP server's tool_rpm.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Redis ¶
type Redis struct {
// contains filtered or unexported fields
}
func (*Redis) AdmitKey ¶ added in v1.0.0
func (t *Redis) AdmitKey(ctx context.Context, keyName string, rpm int32, tpm, estimate int64) (settle func(actual int64), kind string, retry time.Duration)
AdmitKey takes a request and books estimate tokens on the key's rates; settle(actual) rebooks the real tokens. A refusal names its kind and wait (0: never fits). Fails open, like every other limit.
func (*Redis) AdmitTool ¶ added in v1.4.0
func (t *Redis) AdmitTool(ctx context.Context, keyName, server, tool string, rpm int32) (bool, time.Duration)
AdmitTool books one call of a key to an MCP tool under rpm calls a minute, or says how long until one fits. Fails open, like AdmitKey.
func (*Redis) AllowRPM ¶
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 ¶
SetDefaultOutputReserve sets the output tokens priced into a spend reservation when the call has no output ceiling.
func (*Redis) SetGraceMargin ¶
func (*Redis) SetSpendFailClosed ¶
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.
type SpendLimitResolver ¶
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