890. Schedule requests under RPM and TPM limits
Why it matters Every agent and batch pipeline that calls a hosted model hits rate limits. Planning around them is how jobs finish on time without 429 errors.
An LLM API allows at most rpm requests and tpm tokens per minute. You have a queue of requests with known token counts and send them in order.
Write schedule_minutes(token_counts, tpm, rpm). Minutes are numbered 0, 1, 2, .... Process the queue in order and assign each request to a minute:
- A request with more tokens than tpm can never be sent: give it -1 and skip it (it does not use any capacity).
- Otherwise put it in the current minute if both the request count and the token total of that minute stay within the limits. If not, move to the next minute (which starts empty) and put it there.
- Requests are never sent before an earlier request, so the minute only moves forward.
Inputs: token_counts is a list of ints >= 0 (can be empty). tpm and rpm are ints >= 1.
Return a list of ints, one per request.
def schedule_minutes(token_counts, tpm, rpm)Example 1
Input: schedule_minutes([400, 300, 500, 100], 1000, 10)
Output: [0, 0, 1, 1]
Example 2
Input: token_counts = [10, 10, 10, 10, 10], tpm = 1000, rpm = 2
Output: [0, 0, 1, 1, 2]
Example 3
Input: token_counts = [2000, 100, 900], tpm = 1000, rpm = 5
Output: [-1, 0, 0]
Constraints
- Python 3 standard library. Code runs in your browser.
- Your code must pass all 5 tests to be accepted.
Hint 1
Track the current minute, tokens used and requests used.
Hint 2
Check both limits before adding a request.
Hint 3
Handle t > tpm before anything else.
Common mistakes
- Letting a later small request jump into an earlier minute.
- Counting a skipped request against the limits.
- Using < instead of <= for the token limit.
Understand the idea
Two budgets reset each minute: the number of requests and the number of tokens. A greedy in-order packer fills the current minute until either budget would overflow, then starts the next one. A request larger than the token budget can never fit and must be split or rejected, so it gets -1. In the example, 400 + 300 fit in minute 0, 500 would make 1200, so it starts minute 1 and 100 joins it.
Shapes and data flow
List in, list out.