AI Engineer Portal
Your personal operating system for career transition.
Exercise
Implement batch processing with rate limiting
Implement Batch Processing with Rate Limiting
Provider APIs enforce rate limits in two dimensions: requests per minute (RPM) and tokens per minute (TPM). Exceeding either causes 429 errors. A naive batch processor that fires all requests immediately will hit these limits before reaching even 10% of most corpora.
What to build
Implement a RateLimitedBatcher that processes items in batches while respecting both request and token rate limits:
__init__(rpm_limit: int, tpm_limit: int, batch_size: int)— stores limits.process(items: list[dict], process_fn: Callable[[list[dict]], list[dict]]) -> BatchStats— processes all items in batches. Each item has"tokens": int. Tracks: total requests made, total tokens sent, total elapsed time, and rate limit waits.- Before each batch, check if sending it would exceed either limit within the current 60-second window. If so, sleep until the window resets.
BatchStatsdataclass:total_items,total_batches,total_tokens,elapsed_s,rate_limit_waits,items_per_second.
The rate limiting logic: track how many requests and tokens were sent in the last 60 seconds. If the next batch would exceed either limit, compute how long to sleep until the window clears.
Why this matters
Rate limit handling is one of the most common sources of production failures in AI pipelines. Engineers often add naive time.sleep(60) calls that make pipelines 10x slower than necessary. A proper token-aware rate limiter sleeps only when needed and for only as long as required.
Constraints
- Use
time.time()for rate limit window tracking (notasyncio). - Do not sleep for more than 60 seconds at a time.
- Test with a mock
process_fnthat does not actually call any API. items_per_secondinBatchStatsshould be rounded to 2 decimal places.
Data Transformation / hard / Step 11 of 16
Transform and summarize data safely
Think like an evaluation or trace pipeline: group clearly, preserve the data story, and make edge cases visible instead of clever.
- - Handles missing or noisy records predictably
- - Produces summary output that is easy to inspect
- - Would be safe to rerun in a script workflow
- - Do grouped metrics stay readable?
- - Would malformed rows be debuggable?
- - Did I choose names that explain the transformation?
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.