Back to problems

Design Token Limiter

Algorithm · xAI · Medium

Design a per‑user token rate limiter for an AI API provider. Each user receives a custom policy that defines the bucket’s maximum token count, how many tokens are added on each refill, and the time interval in seconds between refills. The system must enforce distinct limits for every user and perform refills correctly even when requests do not arrive on a regular schedule. Implement the TokenLimiter class: TokenLimiter(List > policies) Initializes the limiter with a list of…

Checking your access…