方寸 Portal

AI Engineer Portal

Your personal operating system for career transition.

Private mode

Exercise

Build a rate limiter for AI API endpoints

Build a Rate Limiter for AI API Endpoints

LLM API calls are expensive and slow. Without rate limiting at your application layer, a single misbehaving client can exhaust your provider token budget or trigger provider-side rate limits that degrade service for everyone.

You need two rate limiting strategies working together:

  • Per-user RPM (requests per minute): limit how many requests a single user can make per minute.
  • Global TPM (tokens per minute): limit the total tokens consumed across all users per minute to stay within your provider quota.

What to build

Implement a TwoLayerRateLimiter that:

  1. check_user(user_id: str) -> RateLimitResult — checks if the user is within their per-user RPM limit.
  2. check_global(estimated_tokens: int) -> RateLimitResult — checks if the global TPM budget has headroom.
  3. record_usage(user_id: str, tokens_used: int) -> None — records actual usage after a successful call.
  4. Uses a sliding window algorithm (1-minute window) for both limits.
  5. RateLimitResult has: allowed: bool, reason: str, retry_after_seconds: float.

Constraints

  • Standard library only (use time.monotonic()).
  • Per-user default limit: 10 RPM.
  • Global default: 100,000 TPM.
  • Sliding window: deque of timestamps for RPM, deque of (timestamp, tokens) for TPM.

Api Async / medium / Step 11 of 23

Practice stage

Async and provider control

Hint

Make waiting behavior explicit. Timeouts, retries, and concurrency limits matter more than squeezing everything into one helper.

Success criteria
  • - Uses async boundaries coherently
  • - Makes timeout and retry decisions legible
  • - Would be maintainable under provider instability
Review checklist
  • - Is timeout behavior explicit?
  • - Is retryable failure separate from terminal failure?
  • - Would logs reveal what actually timed out?

Practice

Generate a variation

Generate a new exercise variation to deepen understanding or practice a related concept.

Attempt history

Recent submissions

Before you submit, decide what a strong answer should make obvious to the reviewer.

No attempts yet.