Files
traefik-llmgateway/smart_router_size.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

77 lines
1.6 KiB
Go

package traefikllmgateway
const (
// smartSizeMaxDepth and smartSizeMaxNodes bound the walk so a hostile
// payload cannot make the size estimate expensive.
smartSizeMaxDepth = 12
smartSizeMaxNodes = 4096
// smartScalarSize approximates a serialized number, bool or null.
smartScalarSize = 6
)
// smartValueSize estimates the JSON-serialized byte length of a decoded
// value without allocating. Past the bounds it returns a lower bound.
func smartValueSize(v any) int {
budget := smartSizeMaxNodes
return smartValueSizeWalk(v, 0, &budget)
}
func smartValueSizeWalk(v any, depth int, budget *int) int {
switch x := v.(type) {
case string:
return len(x) + 2
case map[string]any:
n := 2
if depth >= smartSizeMaxDepth {
return n
}
for k, val := range x {
if *budget <= 0 {
break
}
*budget--
n += len(k) + 4 + smartValueSizeWalk(val, depth+1, budget)
}
return n
case []any:
n := 2
if depth >= smartSizeMaxDepth {
return n
}
for i := 0; i < len(x); i++ {
if *budget <= 0 {
break
}
*budget--
n += 1 + smartValueSizeWalk(x[i], depth+1, budget)
}
return n
}
return smartScalarSize
}
// smartToolCallsSize sums the argument payloads of OpenAI tool_calls.
func smartToolCallsSize(calls any) int {
list, ok := calls.([]any)
if !ok {
return 0
}
n := 0
for i := 0; i < len(list); i++ {
call, isMap := list[i].(map[string]any)
if !isMap {
continue
}
fn, hasFn := call["function"].(map[string]any)
if !hasFn {
continue
}
if args, isStr := fn["arguments"].(string); isStr {
n += len(args)
} else {
n += smartValueSize(fn["arguments"])
}
}
return n
}