# Class AdapterRateLimiter

Token bucket rate limiter for controlling request rates.

The token bucket algorithm works as follows:

1. A bucket holds tokens up to a maximum capacity
2. Tokens are added at a fixed rate (refillRate per second)
3. Each request consumes one or more tokens
4. If insufficient tokens, the request is rejected or waits

#### Example

    const limiter = new RateLimiter({
      capacity: 100,    // Max 100 tokens
      refillRate: 10,   // 10 tokens per second
    });

    if (limiter.tryAcquire()) {
      // Proceed with operation
    } else {
      // Rate limited
    }

    // Or wait for tokens
    await limiter.waitForTokens();

- Defined in packages/nexus-agents/src/adapters/rate-limiter.ts:72

##### Index

### Constructors

constructor

### Methods

tryAcquire
acquire
waitForTokens
getRemainingTokens
getAvailableTokens
reset
getCapacity
getRefillRate
getTimeUntilAvailable

## Constructors

### constructor

- new AdapterRateLimiter(config: AdapterRateLimiterConfig): AdapterRateLimiter

  Creates a new RateLimiter instance.

  #### Parameters

  - config: AdapterRateLimiterConfig

    Configuration options

  #### Returns AdapterRateLimiter

  #### Throws

  If configuration is invalid

  - Defined in packages/nexus-agents/src/adapters/rate-limiter.ts:85

## Methods

### tryAcquire

- tryAcquire(tokens?: number): boolean

  Attempts to acquire the specified number of tokens.

  #### Parameters

  - tokens: number = 1

    Number of tokens to acquire (default: 1)

  #### Returns boolean

  true if tokens were acquired, false if rate limited

  #### Example

      if (limiter.tryAcquire(5)) {
        // Acquired 5 tokens
      }

  - Defined in packages/nexus-agents/src/adapters/rate-limiter.ts:145

### acquire

- acquire(tokens?: number): Result<void, RateLimitExceeded>

  Attempts to acquire tokens and returns a Result with detailed information.

  #### Parameters

  - tokens: number = 1

    Number of tokens to acquire (default: 1)

  #### Returns Result<void, RateLimitExceeded>

  Result containing void on success, or RateLimitExceeded on failure

  #### Example

      const result = limiter.acquire(5);
      if (!result.ok) {
        console.log(`Retry after ${result.error.retryAfterMs}ms`);
      }

  - Defined in packages/nexus-agents/src/adapters/rate-limiter.ts:177

### waitForTokens

- waitForTokens(tokens?: number): Promise<void>

  Waits until the specified number of tokens are available, then acquires them.

  #### Parameters

  - tokens: number = 1

    Number of tokens to acquire (default: 1)

  #### Returns Promise<void>

  Promise that resolves when tokens are acquired

  #### Throws

  If tokens exceed capacity (would wait forever)

  #### Example

      await limiter.waitForTokens(10);
      // 10 tokens acquired

  - Defined in packages/nexus-agents/src/adapters/rate-limiter.ts:221

### getRemainingTokens

- getRemainingTokens(): number

  Returns the current number of available tokens. Performs a refill before returning the count.

  #### Returns number

  Number of available tokens (may be fractional)

  - Defined in packages/nexus-agents/src/adapters/rate-limiter.ts:250

### getAvailableTokens

- getAvailableTokens(): number

  Returns the number of whole tokens available.

  #### Returns number

  Integer number of available tokens

  - Defined in packages/nexus-agents/src/adapters/rate-limiter.ts:260

### reset

- reset(): void

  Resets the rate limiter to its initial state. The bucket is refilled to capacity.

  #### Returns void

  - Defined in packages/nexus-agents/src/adapters/rate-limiter.ts:268

### getCapacity

- getCapacity(): number

  Returns the bucket's maximum capacity.

  #### Returns number

  - Defined in packages/nexus-agents/src/adapters/rate-limiter.ts:276

### getRefillRate

- getRefillRate(): number

  Returns the refill rate in tokens per second.

  #### Returns number

  - Defined in packages/nexus-agents/src/adapters/rate-limiter.ts:283

### getTimeUntilAvailable

- getTimeUntilAvailable(tokens?: number): number

  Calculates the time in milliseconds until the specified tokens are available.

  #### Parameters

  - tokens: number = 1

    Number of tokens needed (default: 1)

  #### Returns number

  Time in milliseconds until tokens are available, 0 if already available

  - Defined in packages/nexus-agents/src/adapters/rate-limiter.ts:293