# Rate Limiter State Calculator AI Agent Connect

> Deterministic engine for evaluating Token Bucket, Leaky Bucket, and sliding window rate limiting states.

## Overview
- **Category:** infrastructure
- **Price:** Free
- **Endpoint:** https://edge.vinkius.com/vk_preview_L55y3CBkTSDBxwgWFAb6J0mIyY3O7oITmFmzJReE/ai-agent-connect
- **Tags:** rate-limiting, token-bucket, leaky-bucket, api-control, deterministic

## Description

This MCP server provides a deterministic engine for evaluating the current state of various rate-limiting algorithms. It allows AI agents to decide if API calls should be permitted, queued, or dropped based on mathematical models. Use `calculate_token_bucket_state` to check token availability and burst headroom, `calculate_leaky_bucket_state` to evaluate queue depth and overflow, and `calculate_distributed_and_window_metrics` to manage global limits and 60-second sliding window compliance.

## Tools

### calculate_distributed_and_window_metrics
Provides high-level oversight of globalThis limits and temporal request density

### calculate_leaky_bucket_state
Evaluates the current congestion and overflow status of a Leaky Bucket

### calculate_token_bucket_state
Determines if a specific request can be fulfilled based on the Token Bucket algorithm and calculates the wait time if denied

## Prompt Examples

**Prompt:** 
```
Check if I have enough tokens for a request that costs 5 tokens, given a rate limit of 2 per second, a burst capacity of 10, 8 current tokens, and the last refill was 1 second ago.
```

**Response:** 
```
Yes, you have enough tokens. After the refill, you have 10 tokens available, which covers the cost of 5.
```

**Prompt:** 
```
What is the current queue depth if the leaky bucket has a capacity of 10, a drain rate of 1 per second, and 5 requests just arrived while the queue was already at 7?
```

**Response:** 
```
The current queue depth is 10, and 2 requests were dropped due to overflow.
```

**Prompt:** 
```
Calculate the per-agent share for a global limit of 100 requests per second shared by 5 agents.
```

**Response:** 
```
Each agent is allocated a share of 20 requests per second.
```

## Frequently Asked Questions

**How does the Token Bucket model work?**
The `calculate_token_bucket_state` tool calculates available tokens by adding the refill rate multiplied by elapsed time to the current token count, capped by the burst capacity.

**Can I check for queue overflow?**
Yes, use `calculate_leaky_bucket_state` to determine the current queue depth and the number of requests that resulted in an overflow.

**How are global limits shared among multiple agents?**
The `calculate_distributed_and_window_metrics` tool calculates the per-agent share by dividing the total limit by the number of active agents.
