router-for-me/CLIProxyAPI · error

xai-api-key[%d].weight: %w

Error message

xai-api-key[%d].weight: %w

What it means

Returned while validating the weight field of an xai-api-key entry in config.yaml. The loop over cfg.XAIKey calls ValidateCredentialWeight on each entry's Weight; on failure the error identifies the entry index and wraps the underlying credentialweight.Normalize error (weight out of the allowed 1..1000000 range).

Source

Thrown at internal/config/weight.go:141

	}
	for index := range cfg.ClaudeKey {
		if errValidate := ValidateCredentialWeight(cfg.ClaudeKey[index].Weight); errValidate != nil {
			return fmt.Errorf("claude-api-key[%d].weight: %w", index, errValidate)
		}
	}
	for index := range cfg.VertexCompatAPIKey {
		if errValidate := ValidateCredentialWeight(cfg.VertexCompatAPIKey[index].Weight); errValidate != nil {
			return fmt.Errorf("vertex-api-key[%d].weight: %w", index, errValidate)
		}
	}
	for index := range cfg.CodexKey {
		if errValidate := ValidateCredentialWeight(cfg.CodexKey[index].Weight); errValidate != nil {
			return fmt.Errorf("codex-api-key[%d].weight: %w", index, errValidate)
		}
	}
	for index := range cfg.XAIKey {
		if errValidate := ValidateCredentialWeight(cfg.XAIKey[index].Weight); errValidate != nil {
			return fmt.Errorf("xai-api-key[%d].weight: %w", index, errValidate)
		}
	}
	for providerIndex := range cfg.OpenAICompatibility {
		for keyIndex := range cfg.OpenAICompatibility[providerIndex].APIKeyEntries {
			weight := cfg.OpenAICompatibility[providerIndex].APIKeyEntries[keyIndex].Weight
			if errValidate := ValidateCredentialWeight(weight); errValidate != nil {
				return fmt.Errorf("openai-compatibility[%d].api-key-entries[%d].weight: %w", providerIndex, keyIndex, errValidate)
			}
		}
	}
	return nil
}

View on GitHub (pinned to 78f0c4079e)

Solutions

  1. Set xai-api-key[i].weight to a value in 1..1000000 (use 0 or negative only if you intend to exclude the credential)
  2. If you intended relative proportions, use small integers like 1, 3, 10
  3. Validate the whole file with the server's config check / dry-run before reloading

Example fix

# before
xai-api-key:
  - api-key: xai-...
    weight: 99999999

# after
xai-api-key:
  - api-key: xai-...
    weight: 10
Defensive patterns

Strategy: validation

Validate before calling

func checkXAIWeights(keys []XAIKeyConfig) error {
    for i, k := range keys {
        if k.Weight > 1_000_000 {
            return fmt.Errorf("xai-api-key[%d].weight %d exceeds 1000000", i, k.Weight)
        }
    }
    return nil
}

Prevention

When it happens

Trigger: A config.yaml (or hot-reloaded config) where xai-api-key[i].weight exceeds 1000000 or otherwise fails Normalize; validation runs before the server accepts the config, so startup or reload aborts.

Common situations: Migrating from a percentage-based weight scheme, adding many zeros by typo, or hand-editing the YAML after copying a template entry with a large weight.

Related errors


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