Don't Waste LLM Credits: Async Scheduling Within Quota Limits
LLM APIs enforce RPM, TPM, QPS, and expiration windows, so batch jobs need to use the remaining quota intelligently instead of being fired all at once. This article explains how to separate offline tasks from real-time traffic, add priority, idempotency, and persistence, choose a queue by workload, and implement a Python async scheduler with retry, Retry-After, exponential backoff, jitter, deduplication, and incremental result writes.
Know the two constraints: quota and expiry
LLM API providers commonly limit calls along three dimensions:
- RPM: requests per minute, limiting the number of API requests.
- TPM: tokens per minute, limiting the total input and output token volume.
- QPS: queries per second, limiting instantaneous traffic pressure.
If any of these limits is exceeded, the server returns HTTP 429. Some providers also include a Retry-After header in the response, telling the client how many seconds to wait before trying again.
The second constraint is the validity period of credits or quota. Many platforms issue credits in cycles, and unused credits expire at the end of the cycle. The goal of scheduling is therefore to spread offline tasks evenly within the quota before the deadline, while leaving a buffer before expiration. Sending all requests in a burst will trigger rate limiting and queueing, and it will not solve the problem of idle quota.
Separate real-time tasks from offline tasks
Not every LLM call needs an immediate response. Batch code review for historical repositories, documentation summarization, log classification, test case generation, and similar workloads can often tolerate delays of minutes or even hours. The first step is to remove these tasks from the online request path and place them in a separate offline batch module.
Before an offline task enters the queue, add three attributes.
- Priority: Online business calls should occupy quota first. Offline tasks should use the remaining quota.
- Idempotency: Each task has a unique ID, so repeated execution does not produce duplicate results.
- Result persistence: Successful results are written to a database or file. Failed tasks go to a dead-letter queue and are retried by a scheduled job or manually.
Choosing a message queue
Asynchronous processing usually relies on a message queue. The common options have different boundaries.
| Component | Suitable for | Notes |
|---|---|---|
| RabbitMQ | Medium workload, flexible routing, priority queues, dead-letter queues | Throughput is lower than Kafka; memory usage needs attention when messages accumulate |
| Kafka | Large workload, durable message storage, replay, multiple consumer groups | Higher operational cost; best for teams that already run a data pipeline |
| Redis queue | Small to medium workload, Redis already in use, quick implementation | Reliability depends on persistence settings; Redis Streams is a better choice for critical tasks |
If you only have a few dozen offline tasks, a database task table plus periodic polling is enough. Choose based on task volume, existing technology stack, and acceptable operational cost. There is no need to introduce a heavy component just to make the architecture look complete.
Code practice: batch scheduling with backoff and rate limiting
The offline batch processor is an async worker with the following behavior:
- Configurable concurrency limit.
- Retry on HTTP 429 and 5xx errors.
- Fail immediately on 400, 401, and other non-retryable client errors.
- Prefer the
Retry-Afterheader when available. - Otherwise use exponential backoff with random jitter.
- Skip tasks that are already completed by task ID.
- Write each result to a file as it is produced.
The example runs on Python 3.10+ and requires httpx. Install it with:
pip install httpx
The API endpoint and key are read from environment variables instead of being hardcoded, so the worker can be deployed without changing the source:
export LLM_API_URL=...export LLM_API_KEY=...
With these two constraints understood—quota limits and expiry—offline batch tasks can be scheduled safely and efficiently without wasting unused LLM capacity.
