Skip to content

Function: roundRobinKeys()

@providerprotocol/ai


@providerprotocol/ai / @providerprotocol/ai / roundRobinKeys

roundRobinKeys(keys): KeyStrategy

Defined in: src/http/keys.ts:38

Creates a key strategy that distributes API requests across multiple keys using round-robin selection.

Each call to getKey() returns the next key in sequence, cycling back to the first key after reaching the end. This provides even distribution of requests across all available keys, which is useful for:

  • Spreading rate limits across multiple API keys
  • Load balancing between different accounts
  • Maximizing throughput when multiple keys are available

string[]

Array of API keys to rotate through

KeyStrategy

A KeyStrategy that cycles through the provided keys

When the keys array is empty

const keys = roundRobinKeys([
'sk-key-1',
'sk-key-2',
'sk-key-3'
]);
keys.getKey(); // Returns 'sk-key-1'
keys.getKey(); // Returns 'sk-key-2'
keys.getKey(); // Returns 'sk-key-3'
keys.getKey(); // Returns 'sk-key-1' (cycles back)