Skip to main content
HasteKit supports managing multiple API keys for a single provider. This is useful for:
  • Load Balancing: Distribute requests across multiple API keys to avoid rate limits.
  • Redundancy: Ensure high availability by having fallback keys.
  • Cost Management: Allocate traffic based on usage quotas or costs associated with different keys.

Weighted Random Selection

When multiple API keys with weights are configured for a provider, HasteKit uses weighted random selection to choose which key to use for each request. Each API key must be assigned a Weight. The probability of a key being selected is proportional to its weight relative to the sum of all weights for that provider.

How it Works

  1. HasteKit retrieves all the configured API keys for the requested provider.
  2. It extracts the Weight from each APIKeyConfig.
  3. If multiple keys exist, it performs a weighted random selection.
  4. The request is then executed using the selected API key.
If all keys have the same weight (e.g., all set to 1 or not specified), the selection becomes uniformly random.

Configuration Example

When using the HasteKit SDK in direct mode, you can configure multiple keys by passing a []hastekit.ProviderConfig to hastekit.NewLLMClient, assigning each APIKeyConfig a Weight.