Files
traefik-llmgateway/smart_router_fallback.go
T
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

64 lines
2.3 KiB
Go

package traefikllmgateway
import (
"fmt"
"net/http"
"time"
)
// validateSmartTimeoutFallback rejects timeoutFallback without a deadline to miss or a tier to fall to.
func validateSmartTimeoutFallback(router string, cfg *SmartRouterConfig, tiers int, firstByte time.Duration) error {
if !cfg.TimeoutFallback {
return nil
}
if firstByte <= 0 {
return fmt.Errorf("llmgateway: smartRouters: router %q: timeoutFallback needs firstByteTimeout", router)
}
if tiers < 2 {
return fmt.Errorf("llmgateway: smartRouters: router %q: timeoutFallback needs at least two tiers", router)
}
return nil
}
// routerFallbackRank returns the highest tier below firstRank that has a usable candidate, or -1.
func routerFallbackRank(firstRank int, usable func(int) bool) int {
for r := firstRank - 1; r >= 0; r-- {
if usable(r) {
return r
}
}
return -1
}
// routerFallbackCount sums the finished requests of one router that took the extra attempt.
func routerFallbackCount(router string, outcomes map[routerOutcomeKey]int64) int64 {
var n int64
for k, v := range outcomes {
if k.router == router && k.reason == routerReasonFallback {
n += v
}
}
return n
}
// addTimeoutFallback appends the one extra attempt when every candidate is spent, one of them missed the
// first-byte deadline, the last failure still allows a retry, and the client is still waiting. It reports
// whether an attempt was added; the caller then continues its loop. The extra attempt may exceed maxAttempts
// by one, goes below any floor or session pin, and never repeats.
func (g *Gateway) addTimeoutFallback(plan *routerPlan, r *http.Request, last int, endpoint string, scopes []limitScope, callErr error) bool {
if plan.pickFallback == nil || plan.fallbackAt > 0 || !plan.slowSeen || r.Context().Err() != nil || !routerEscalatable(callErr, true) {
return false
}
entry, ok := plan.pickFallback()
if !ok {
return false
}
from := plan.entries[0].tier
plan.entries = append(plan.entries, entry)
plan.fallbackFrom = from
plan.fallbackAt = len(plan.entries) - 1
g.recordRouterEventKind(plan, plan.fallbackAt, endpoint, scopes, eventKindRouterTimeoutFallback, &g.routerFallbackGate,
fmt.Sprintf("router %q: every attempt missed the first-byte deadline; falling back from tier %q to tier %q", plan.router, from, entry.tier))
return true
}