Files
traefik-llmgateway/smart_router_calibrate.go
lukaszraczyloandGitHub 4c61d19a72 feat(router): smart routers and per-member rate limits (#9)
Add smart routers: virtual models that score each request and route it to admin-defined model tiers. A retryable failure moves the request to a higher tier. The classifier v2 extracts prose, finds the original ask in long tool loops, adds an uncertainty margin and reads tool-output failures. Sessions, outcome signals, bounded calibration, adaptive selection within a tier, gated saturation downgrade, a first-byte timeout and an opt-in timeout fallback are included. Every new feature is off by default.

Add per-member group limits, a tokensPerMinute window that counts uncached input and output, rate-limit headers in three dialects, soft pacing before the hard limit, and a limitExemptKinds list so embedding and reranking traffic never counts toward or hits a limit.

Fix yaegi named-result state that made smartContentSize grow between calls.
2026-11-25 00:40:30 +00:00

94 lines
2.8 KiB
Go

package traefikllmgateway
import "sync"
const (
// smartDefaultBytesPerToken is the uncalibrated estimate.
smartDefaultBytesPerToken = 4
// smartCalibMinSamples keeps a few odd requests from steering the estimate.
smartCalibMinSamples = 50
// smartCalibMinBytes skips small prompts, where fixed provider overhead dominates the token count.
smartCalibMinBytes = 256
// smartCalibMinRatio and smartCalibMaxRatio bound the learned bytes per token against bad usage data.
smartCalibMinRatio = 1.5
smartCalibMaxRatio = 6.0
// smartCalibAlpha weights the newest sample; 0.1 follows drift within tens of requests.
smartCalibAlpha = 0.1
// smartCalibMaxModels bounds the map; models come from config and discovery, so it is a safety cap.
smartCalibMaxModels = 1024
)
type smartCalibKey struct {
provider, model string
}
type smartCalibStat struct {
ratio float64
samples int
}
// smartTokenCalibrator learns bytes per prompt token for each upstream model
// from reported usage. It keeps two numbers per model and no request data.
type smartTokenCalibrator struct {
stats map[smartCalibKey]*smartCalibStat
mu sync.Mutex
}
func newSmartTokenCalibrator() *smartTokenCalibrator {
return &smartTokenCalibrator{stats: make(map[smartCalibKey]*smartCalibStat)}
}
// observe records one request's estimated prompt bytes against the upstream's reported prompt tokens.
func (c *smartTokenCalibrator) observe(provider, model string, promptBytes, promptTokens int64) {
if c == nil || promptTokens <= 0 || promptBytes < smartCalibMinBytes {
return
}
sample := float64(promptBytes) / float64(promptTokens)
key := smartCalibKey{provider: provider, model: model}
c.mu.Lock()
defer c.mu.Unlock()
st := c.stats[key]
if st == nil {
if len(c.stats) >= smartCalibMaxModels {
return
}
c.stats[key] = &smartCalibStat{ratio: sample, samples: 1}
return
}
st.ratio += smartCalibAlpha * (sample - st.ratio)
if st.samples < smartCalibMinSamples {
st.samples++
}
}
// promptTokens estimates the prompt tokens for promptBytes on a model: the
// clamped learned ratio once enough samples exist, else one token per four bytes.
func (c *smartTokenCalibrator) promptTokens(provider, model string, promptBytes int64) int64 {
if c != nil {
c.mu.Lock()
st := c.stats[smartCalibKey{provider: provider, model: model}]
ratio, ready := 0.0, false
if st != nil && st.samples >= smartCalibMinSamples {
ratio, ready = st.ratio, true
}
c.mu.Unlock()
if ready {
if ratio < smartCalibMinRatio {
ratio = smartCalibMinRatio
}
if ratio > smartCalibMaxRatio {
ratio = smartCalibMaxRatio
}
tokens := int64(float64(promptBytes)/ratio + 0.999999)
return tokens
}
}
return (promptBytes + smartDefaultBytesPerToken - 1) / smartDefaultBytesPerToken
}
func (c *smartTokenCalibrator) size() int {
c.mu.Lock()
defer c.mu.Unlock()
return len(c.stats)
}