router-for-me/CLIProxyAPI · error

weight must not exceed

Error message

weight must not exceed %d

What it means

Core range check in credentialweight.Normalize: an explicit positive weight above the package Max (1,000,000) is rejected. The cap bounds scheduler arithmetic while still allowing practical proportional routing. This is the error wrapped by the config-level per-key weight errors.

Solutions

  1. Clamp or rescale weights to fit in 1..1000000 before passing them in
  2. If weights come from user input or an external feed, normalize with a proportional rescale: w_i = max(1, round(w_i * Max / maxW))
  3. For excluded credentials, pass 0 or a negative value instead of a huge number as a sentinel

Example fix

// before
w, err := credentialweight.Normalize(5_000_000) // error

// after
w, err := credentialweight.Normalize(min(requested, credentialweight.Max))
Defensive patterns

Strategy: validation

Validate before calling

const maxW = int64(1_000_000)

func safeWeight(w int64) int64 {
    if w <= 0 {
        return 0 // excluded from routing
    }
    if w > maxW {
        return maxW
    }
    return w
}

Prevention

When it happens

Trigger: Calling credentialweight.Normalize (directly or via ValidateCredentialWeight / ParseString / ParseValue) with an int64 greater than 1000000. Values <= 0 do not trigger it — they normalize to 0 (excluded from routing).

Common situations: Importing weights from an external system with a different scale, computing weights programmatically (e.g. multiplying ratios by 10^7 for precision), or unit tests using huge sentinel values.

Related errors


AI-assisted analysis of router-for-me/CLIProxyAPI@78f0c4079e (2026-08-15). Data as JSON: /api/errors/10447f818b55e951. Report an issue: GitHub.

Appendix: source

Thrown at internal/credentialweight/weight.go:26

	"strconv"
	"strings"
)

const (
	// Default is used when a credential does not define a weight.
	Default int64 = 1
	// Max bounds scheduler arithmetic while allowing practical proportional routing.
	Max int64 = 1_000_000
)

// Normalize validates and normalizes an explicit weight. Non-positive values are
// valid and normalize to zero, which excludes the credential from weighted routing.
func Normalize(weight int64) (int64, error) {
	if weight <= 0 {
		return 0, nil
	}
	if weight > Max {
		return 0, fmt.Errorf("weight must not exceed %d", Max)
	}
	return weight, nil
}

// ParseString parses a scheduler attribute. An empty value uses the default weight.
func ParseString(raw string) (int64, error) {
	raw = strings.TrimSpace(raw)
	if raw == "" {
		return Default, nil
	}
	weight, errParse := strconv.ParseInt(raw, 10, 64)
	if errParse != nil {
		return 0, fmt.Errorf("weight must be an integer: %w", errParse)
	}
	return Normalize(weight)
}

// ParseValue parses a JSON-compatible auth-file metadata value.

View on GitHub (pinned to 78f0c4079e)