Neural Mastery
← Practice
Token Bucket Rate Limiter Simulatormedium

Token Bucket Rate Limiter Simulator

medium Python for AI 15–20 min

NumPy allowed · plain Python earns 10 bonus points

Simulate the industry-standard Token Bucket traffic-shaping algorithm for API throttling and LLM rate limiting.

Related Skills

Task

Implement simulate_token_bucket(requests, rate, capacity): the bucket starts at full capacity. At each arrival timestamp t, compute replenished tokens (t - last_time) * rate (capped at capacity). If tokens >= 1.0, consume 1.0 and record True, otherwise reject and record False.

Function Signature

simulate_token_bucket(requests: list[float], rate: float, capacity: float) -> list[bool]

Examples

Example 1: Burst Then Throttled
Input: requests = [0, 0.2, 0.4, 0.6, 1] rate = 1 capacity = 2
Output: [True, True, False, False, False]
Example 2: Evenly Spaced Requests
Input: requests = [0, 1, 2] rate = 1 capacity = 1
Output: [True, True, True]
Example 3: Large Capacity High Burst
Input: requests = [0, 0.05, 0.1, 0.15] rate = 1 capacity = 5
Output: [True, True, True, True]

Constraints

  • Libraries are allowed; a plain-Python solution earns 10 bonus points.
  • Timestamps in requests are in non-decreasing order.
  • Tokens are capped at capacity and replenished continuously.
Python3Saved ✓
def simulate_token_bucket(requests: list[float], rate: float, capacity: float) -> list[bool]:
"""Simulate a token bucket rate limiter over timestamps requests (sorted floats).
Bucket starts full at capacity tokens and refills at rate tokens/sec.
Each allowed request consumes 1.0 token. Return list of bools for each request."""
# Your implementation here
pass

Case 1: Burst Then Throttled
Input: requests = [0, 0.2, 0.4, 0.6, 1] rate = 1 capacity = 2
Expected: [True, True, False, False, False]
Case 2: Evenly Spaced Requests
Input: requests = [0, 1, 2] rate = 1 capacity = 1
Expected: [True, True, True]
Case 3: Large Capacity High Burst
Input: requests = [0, 0.05, 0.1, 0.15] rate = 1 capacity = 5
Expected: [True, True, True, True]