vtc

package
v0.8.0 Latest Latest
Warning

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

Go to latest
Published: Oct 12, 2026 License: Apache-2.0 Imports: 13 Imported by: 1 module, 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 vtc implements the Virtual Token Counter routing algorithms focused on fairness and utilization

Index

Constants

View Source
const (
	VTC_TOKEN_TRACKER_WINDOW_SIZE = "AIBRIX_ROUTER_VTC_TOKEN_TRACKER_WINDOW_SIZE"
	VTC_TOKEN_TRACKER_TIME_UNIT   = "AIBRIX_ROUTER_VTC_TOKEN_TRACKER_TIME_UNIT"
	VTC_TOKEN_TRACKER_MIN_TOKENS  = "AIBRIX_ROUTER_VTC_TOKEN_TRACKER_MIN_TOKENS"
	VTC_TOKEN_TRACKER_MAX_TOKENS  = "AIBRIX_ROUTER_VTC_TOKEN_TRACKER_MAX_TOKENS"
)
View Source
const (
	VTC_MAX_POD_LOAD        = "AIBRIX_ROUTER_VTC_BASIC_MAX_POD_LOAD"
	VTC_INPUT_TOKEN_WEIGHT  = "AIBRIX_ROUTER_VTC_BASIC_INPUT_TOKEN_WEIGHT"
	VTC_OUTPUT_TOKEN_WEIGHT = "AIBRIX_ROUTER_VTC_BASIC_OUTPUT_TOKEN_WEIGHT"
	VTC_FAIRNESS_WEIGHT     = "AIBRIX_ROUTER_VTC_BASIC_FAIRNESS_WEIGHT"
	VTC_UTILIZATION_WEIGHT  = "AIBRIX_ROUTER_VTC_BASIC_UTILIZATION_WEIGHT"
)
View Source
const RouterVTCBasic types.RoutingAlgorithm = "vtc-basic"

Variables

This section is empty.

Functions

func EnvOverrides added in v0.8.0

func EnvOverrides() types.VTCOverrides

EnvOverrides returns the VTC routing knob defaults the environment configures. The routing algorithm package folds them into the process default table at startup.

The token tracker's window, time unit, token floors and the two token weights are part of the table because a profile may override them: they are baked into a tracker instance rather than read per request, so a profile that sets any of them gets a tracker of its own (see TokenTrackerFor).

func EnvTokenTrackerKnobs added in v0.8.0

func EnvTokenTrackerKnobs() types.VTCTokenTrackerOverrides

EnvTokenTrackerKnobs returns the window, time unit and token floors the environment configures. The routing algorithm package folds them into the process default table at startup, and a request whose model config profile sets none of them shares the tracker built from them.

func NewVTCBasicRouter

func NewVTCBasicRouter() (types.Router, error)

func TimeUnitName added in v0.8.0

func TimeUnitName(name string) string

TimeUnitName canonicalizes a time unit name the environment or a model config profile configures, so that callers carry the name of a unit the tracker has instead of an arbitrary string. An unknown name falls back to minutes, which is what the environment loader has always done.

Types

type BasicVTCRouter

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

BasicVTCRouter implements the VTC routing algorithm

func NewBasicVTCRouter

func NewBasicVTCRouter(tokenTracker TokenTracker, tokenEstimator TokenEstimator, config *VTCConfig) (*BasicVTCRouter, error)

NewBasicVTCRouter creates a new BasicVTCRouter with the provided token tracker and estimator

func (*BasicVTCRouter) Polarity added in v0.7.0

func (r *BasicVTCRouter) Polarity() types.Polarity

Polarity returns whether higher or lower score is better.

func (*BasicVTCRouter) Route

func (r *BasicVTCRouter) Route(ctx *types.RoutingContext, readyPodList types.PodList) (string, error)

Route implements the VTC routing algorithm

func (*BasicVTCRouter) ScoreAll added in v0.7.0

func (r *BasicVTCRouter) ScoreAll(ctx *types.RoutingContext, readyPodList types.PodList) ([]float64, []bool, error)

ScoreAll computes the scores for all ready pods in a single batch operation.

func (*BasicVTCRouter) SubscribedMetrics

func (r *BasicVTCRouter) SubscribedMetrics() []string

type InMemorySlidingWindowTokenTracker

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

InMemorySlidingWindowTokenTracker tracks tokens per user in a fixed-size sliding window (in-memory, thread-safe).

func (*InMemorySlidingWindowTokenTracker) GetMaxTokenCount

func (t *InMemorySlidingWindowTokenTracker) GetMaxTokenCount(ctx context.Context) (float64, error)

Time: O(1) | Space: O(1)

func (*InMemorySlidingWindowTokenTracker) GetMinTokenCount

func (t *InMemorySlidingWindowTokenTracker) GetMinTokenCount(ctx context.Context) (float64, error)

Time: O(1) | Space: O(1)

func (*InMemorySlidingWindowTokenTracker) GetTokenCount

func (t *InMemorySlidingWindowTokenTracker) GetTokenCount(ctx context.Context, user string) (float64, error)

Time: Avg O(1) (amortized), Worst O(B_u) where B_u = buckets for user u | Space: O(1)

func (*InMemorySlidingWindowTokenTracker) UpdateTokenCount

func (t *InMemorySlidingWindowTokenTracker) UpdateTokenCount(ctx context.Context, user string, inputTokens, outputTokens float64) error

Time: Avg O(1) (amortized), Worst O(B_u) where B_u = buckets for user u | Space: O(1)

type SimpleTokenEstimator

type SimpleTokenEstimator struct {
	CharactersPerToken float64
	OutputRatio        float64
}

func (*SimpleTokenEstimator) EstimateInputTokens

func (e *SimpleTokenEstimator) EstimateInputTokens(message string) float64

func (*SimpleTokenEstimator) EstimateOutputTokens

func (e *SimpleTokenEstimator) EstimateOutputTokens(message string) float64

type TimeUnit

type TimeUnit int
const (
	Minutes TimeUnit = iota
	Seconds
	Milliseconds
)

type TokenEstimator

type TokenEstimator interface {
	EstimateInputTokens(message string) float64

	EstimateOutputTokens(message string) float64
}

TokenEstimator estimates token counts for messages

func NewSimpleTokenEstimator

func NewSimpleTokenEstimator() TokenEstimator

TODO: advanced token estimators

type TokenTracker

type TokenTracker interface {
	GetTokenCount(ctx context.Context, user string) (float64, error)

	UpdateTokenCount(ctx context.Context, user string, inputTokens, outputTokens float64) error

	GetMinTokenCount(ctx context.Context) (float64, error)

	GetMaxTokenCount(ctx context.Context) (float64, error)
}

TokenTracker tracks token usage per user

func NewInMemorySlidingWindowTokenTracker

func NewInMemorySlidingWindowTokenTracker(config *VTCConfig, opts ...TokenTrackerOption) TokenTracker

TODO: add redis token tracker so that state is shared across plugin instances NewInMemorySlidingWindowTokenTracker creates a new token tracker with configurable options

func NewScopedInMemorySlidingWindowTokenTracker added in v0.8.0

func NewScopedInMemorySlidingWindowTokenTracker(config *VTCConfig, knobs types.VTCTokenTrackerOverrides, opts ...TokenTrackerOption) TokenTracker

NewScopedInMemorySlidingWindowTokenTracker creates a tracker whose window, bucket size and token floors come from knobs instead of the process-wide environment variables. It backs the tracker a model config profile scopes to itself, so the profile's window, weights and floors are read from and written to that tracker alone.

type TokenTrackerOption

type TokenTrackerOption func(*InMemorySlidingWindowTokenTracker)

TokenTrackerOption is a function that configures a token tracker

func WithTimeUnit

func WithTimeUnit(unit TimeUnit) TokenTrackerOption

func WithWindowSize

func WithWindowSize(size int) TokenTrackerOption

type VTCConfig

type VTCConfig struct {
	Variant           types.RoutingAlgorithm
	InputTokenWeight  float64
	OutputTokenWeight float64
}

func DefaultVTCConfig

func DefaultVTCConfig() VTCConfig

Jump to

Keyboard shortcuts

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