Enforce Requests and Tokens Per Minute for Shared LLM Quota

Last updated: November 29, 2026

Quick Overview

Implement a rate limiter that admits an LLM request only if both the requests-per-minute and tokens-per-minute limits over a sliding 60 second window would still hold, and that can report how long a caller must wait before a given request would be admitted.

Ode
Coding & Algorithms
Software Engineer
Ode
November 29, 2026
Software Engineer
Onsite
Coding & Algorithms
Medium

0

0

0 solved


Implement a rate limiter that admits an LLM request only if both the requests-per-minute and tokens-per-minute limits over a sliding 60 second window would still hold, and that can report how long a caller must wait before a given request would be admitted.

Ode teams run batch jobs (backfilling extractions over years of client documents) against provider quotas shared with live traffic. This question checks sliding-window bookkeeping with two constraints at once and an exact wait-time computation rather than sleep-and-poll.

What the Interviewer Expects
  • Use a deque of (timestamp, tokens) and a running token sum
  • Evict expired events before every decision
  • Admit only when both limits hold after adding the request
  • Compute wait time by walking events oldest first until both constraints are satisfied
  • Reject a single request larger than the token limit instead of waiting forever
Key Topics to Cover
sliding window
rate limiting
deque
LLM quotas
concurrency
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
  • How would you share this limit across 20 worker processes?
  • The provider counts output tokens too, which you only know afterwards. How do you reconcile?
  • How would you give live user traffic priority over the backfill?
  • How do you react to a 429 that arrives even though your limiter said yes?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice DSA Problems
Sample Answer
Requirements

try_acquire(now, tokens) returns True and records the request if, within (now - 60, now], the request count stays at or below rpm and the token total at or below tpm. wait_time(now, tokens) ...

High-Level Design

A sliding log is exact and the volume is small (thousands of events a minute at most), so it beats a token bucket for clarity here. Keep events in arrival order; eviction pops from the left. For the w...


Submit Your Answer
Markdown supported

Related Questions