# Run an LLM Over 50,000 Documents With Limits and Retries

> How to send 50,000 documents through an LLM without hitting limits: batch jobs, a concurrency cap, Retry-After and saved results, explained simply for your AI interview.

**In short:** Use a batch job when nobody waits for the answers: Anthropic charges half price for batches, and the provider runs the queue. If you must call the model live, cap the requests in flight, wait the time a 429 asks for, and save each result as it arrives.

AI and LLM interview question Q473, Coding and practical. Intermediate, common (our estimate). From Venkat R. Murty's free Interview Kit: https://designervenkat.online/interview/ai/run-an-llm-over-50000-documents

## Say it in 30 seconds

I would send 50,000 documents as a batch job if nobody waits for the answers, because Anthropic charges half price for batches and the provider runs the queue. If the answers are needed live, I cap the requests in flight, wait the time a 429 response asks for, and back off when there is no wait time. I save every answer with its document ID as it arrives, so a crash does not lose the run.

## Say it in 2 minutes

Every document is one request, so the first question is whether anyone waits for each answer. If no one does, I use a batch job. The provider runs the queue, Anthropic's batch price is half the standard rate, and most batches finish within an hour. Results can come back in any order, so I match each one by its document ID.

If the answers are needed live, the code has to do the work. I keep a fixed number of requests in flight, so I stay under the limit and never send a burst. I also watch the per-minute token limits, because long documents can hit those first. When a request gets a 429 error, I read the Retry-After header and wait that long. If there is no header, I double the wait each try, with a random part, up to a set number of tries.

I save each answer the moment it arrives. A restart then skips the documents already done. A spend-cap 429 is different: it has no wait time and keeps failing until access resumes, usually at the start of next month, so I stop the run and raise an alert. I also test with a small batch of fifty documents first, to check the output shape and the cost. So a crash costs me only the requests in flight, never the whole run.

## What is a bulk LLM run, in plain words?

You have 50,000 documents, and you want a model to read each one and return something: a summary, a tag, a yes or no. Doing them one by one could take days, so you send many at once. How many requests run at the same moment is called concurrency.

Anthropic limits how much you can send. Go over the limit and the reply is HTTP 429, which means too many requests. A job that waits the time the provider asks for, then tries again, gets through the queue. A job that keeps hammering the service until the run falls over does not.

Think of a claims clerk with a backlog of 50,000 insurance files. She keeps only a few open on her desk, and when the records office says to come back in two minutes, she waits instead of knocking every second. A bank that wants 50,000 call summaries tagged by complaint reason is in the same spot, and that is the job this answer is about.

- **Concurrency:** How many requests are in flight at the same moment.
- **Rate limit:** The most requests or tokens a provider lets you send over a set period.
- **429:** The HTTP reply that means too many requests. Slow down.
- **Retry-After:** A header on the reply that says how many seconds to wait before trying again.
- **Batch job:** You send one list of requests in a single call, and the provider works through them later.

## Should you use a batch job or live calls?

First ask whether anyone waits for each answer. If no one does, a batch job is cheaper and needs less code. If someone does, you run live calls and own every cap and every retry yourself.

- **Batch job: Nobody waits.** Send up to 100,000 requests, or 256 MB, as one job. The provider runs the queue, and most jobs finish within an hour.
- **Live calls: Answers as they come.** Your code sends one request per document under a cap, then handles every 429 and every failed save itself.
- **Watch out: Everything at once.** Short bursts of requests can exceed the limit and trigger rate limit errors, and one crash can lose the whole run.

*Diagram, Pick the path for a 50,000 document run: Two ways to send the same 50,000 requests. Live calls give answers as they come, but you own the caps and retries. A batch job costs half the standard price on Anthropic, and the provider runs the queue, so it fits any run where nobody waits. Both end in a results file matched by document ID.*
1. Live: your code sends one request per document, within a cap.
2. Batch: the same requests go in one list for the provider to run.
3. Each live answer is saved with its document ID as it arrives.
4. A batch is ready only when every request has finished. Match each result by its ID, since order varies.

> "A Message Batch is limited to either 100,000 Message requests or 256 MB in size, whichever is reached first." (Anthropic, Message Batches API: limits)
>
> "Batch results can be returned in any order, and might not match the ordering of requests when the batch was created." (Anthropic, Message Batches API: order)

The batch route saves money. Anthropic's batch docs say all usage is charged at half the standard API price, as checked on 2026-11-27. Say each request is about 4 KB (a made-up size). Then 50,000 requests make one batch near 200 MB. That fits the 256 MB limit, so one batch does the job. Bigger documents mean several batches.

A batch has a catch. Results can come back out of order, and Anthropic's batch expires if it does not finish within 24 hours. Its results stay downloadable for 29 days. So set a custom_id on every request and match each result to it. Keep your own list of what you sent.

## How do you cap concurrency and handle 429s?

Live calls need three habits. Cap the requests in flight, obey the wait the provider sends back, and save each answer the moment it arrives.

*Diagram, The live loop: cap, check, wait, save: At most four requests run at once. A good answer is saved with its document ID. A 429 waits the time the provider asks for, then the same document goes round again, and a restart skips what is already saved.*
1. The next document leaves the queue when a slot is free.
2. The model answers, and the reply is fine.
3. The provider says 429: over its limit.
4. Wait the Retry-After seconds, then send the same document again.
5. The answer is saved with its document ID.

1. **Start below the limit:** Begin with a few requests in flight, watch how often 429s appear, then raise the cap one step at a time. Anthropic's docs ask for this on bulk jobs: start slowly and spread the requests out.
2. **Read Retry-After first:** If the 429 carries a Retry-After header, wait that many seconds. Anthropic's SDK does this for you. If you call the API directly, your code has to follow the header.
3. **Back off when there is no header:** Double the wait after each failed try, up to a cap. Add a random part to each wait, so thousands of workers do not all retry in the same second. The Anthropic SDK's own backoff runs from 0.5 to 8 seconds.
4. **Stop after a set number of tries:** Park a document that fails five times in a failed list, for a person or a later run. Do not loop forever, and do not retry a spend-cap 429, because it keeps failing until access resumes.
5. **Save as you go:** Write each answer with its document ID the moment it arrives. A crash at document 31,000 then costs only the requests in flight, because a restart skips the IDs already saved.

Anthropic's limits are per minute: requests (RPM), input tokens (ITPM) and output tokens (OTPM), for each model class. With made-up timings, four calls in flight at 2 seconds each is about 120 requests a minute, so long documents can hit the token limit first. Watch both.

> "Start scheduled or bulk jobs at a low rate and spread requests evenly, rather than sending everything at once." (Anthropic, rate limits: bulk jobs)
>
> "A tier spend-cap 429 has no retry-after header and keeps failing until access resumes" (Anthropic, errors: spend cap)

The core fits in plain Python. The provider here is made up: every fourth document hits its limit on the first try and asks for a 2 second wait.

```python
from concurrent.futures import ThreadPoolExecutor

MAX_IN_FLIGHT = 4  # never more than 4 requests at once

def fake_llm(doc_id, attempt):
    # Made-up provider: every 4th document hits its limit on the first try.
    if doc_id % 4 == 0 and attempt == 0:
        return 429, 2  # status, and Retry-After in seconds
    return 200, None

def run_one(doc_id):
    waited = 0
    for attempt in range(5):
        status, retry_after = fake_llm(doc_id, attempt)
        if status == 200:
            return f"doc {doc_id}: ok, retries {attempt}, waited {waited} s"
        # Obey Retry-After when the provider sends it; otherwise double the wait.
        wait = retry_after or 2 ** attempt
        waited += wait  # real code would sleep here
    return f"doc {doc_id}: gave up"

docs = range(1, 13)
with ThreadPoolExecutor(max_workers=MAX_IN_FLIGHT) as pool:
    for line in pool.map(run_one, docs):
        print(line)
```

Output:

```text
doc 1: ok, retries 0, waited 0 s
doc 2: ok, retries 0, waited 0 s
doc 3: ok, retries 0, waited 0 s
doc 4: ok, retries 1, waited 2 s
doc 5: ok, retries 0, waited 0 s
doc 6: ok, retries 0, waited 0 s
doc 7: ok, retries 0, waited 0 s
doc 8: ok, retries 1, waited 2 s
doc 9: ok, retries 0, waited 0 s
doc 10: ok, retries 0, waited 0 s
doc 11: ok, retries 0, waited 0 s
doc 12: ok, retries 1, waited 2 s
```

Four requests run at most at once, because the pool has four workers. Each 429 adds its Retry-After wait to the total. A real run would sleep through that wait instead of counting it.

> **Tip:** Worked example, with made-up numbers. Each call takes 2 seconds and you keep 4 in flight. The 50,000 documents need about 7 hours of calls (50,000 × 2 s ÷ 4). If 5,000 first tries get a 429 with a 2 second wait, that adds about 40 minutes more (5,000 × 2 s ÷ 4). The cap sets the clock, so a bigger cap is the first lever, as far as the 429 rate allows.

### How would you run this overnight with no one watching?

Treat the run as a job that can stop at any moment. Four guards make it safe to leave alone.

- **A checkpoint:** Each answer is saved with its document ID as it arrives, so a restart picks up where the run stopped.
- **A failed list:** Documents that fail the set number of tries go to their own file. A person or a later run deals with them.
- **A spend guard:** When the monthly cap is hit, the run stops and sends an alert. Retries cannot fix it. Usage pauses until 00:00 UTC on the first day of the next month, unless you ask for a higher limit sooner.
- **A small test first:** Run fifty documents before the full 50,000. Check the output shape, the failure count and the cost.

## What does the interviewer want to hear?

**A strong answer covers**

- [ ] Batch first when nobody waits: Anthropic's batch price is half, and the provider runs the queue.
- [ ] A cap on requests in flight, plus a watch on the per-minute token limits.
- [ ] Retry-After read first, then backoff with a random part and a retry limit.
- [ ] Each answer saved by document ID as it arrives, so a crash restarts cleanly.
- [ ] A stop rule for the monthly spend cap, which retries cannot fix.

**Common traps**

- Trap: "Use 200 threads and retry until it works." Better: That gets 429s and a stalled run. Cap the requests in flight, and stop after a set number of tries.
- Trap: "Sleep one second on every 429." Better: Read Retry-After first, and back off so that workers do not all retry together.
- Trap: "Keep the results in memory and write them at the end." Better: One crash loses the whole run. Save each answer as it lands.

## Can you spot the problem?

**Spot the problem:** Which plan is safe to run over 50,000 documents?

A. Fire all 50,000 at once with no limit, and let the 429s sort themselves out.
B. Retry each 429 right away, with no wait and no limit on tries.
C. Run a pool of 4 workers. On a 429, wait the Retry-After seconds, save each answer, and stop after 5 tries.
D. Run 200 workers, and keep all the results in memory until the run ends.

**Answer:** C. Only the third caps the requests in flight, waits the time the provider asks for, saves as it goes and gives up after a set number of tries. The others retry at once, pile on more requests, or lose everything in a crash.

**Quick check:** A bank wants 50,000 call summaries tagged by the next morning, and no one waits on any single answer. Which path fits?

A. Live calls with 200 workers
B. A batch job, with the results read the next morning
C. A person copies each call into a chat window
D. Live calls at four per second, with no saves

**Answer:** B. Nobody waits on one answer, so a batch job fits. Anthropic charges half the standard price, and the provider runs the queue. Most batches finish within an hour, but a batch that runs past 24 hours expires, so check its status. The other options add load, are slow by hand, or lose work in a crash.

## Can you answer these from memory?

**Flash cards**

- **Batch or live: which first?** Batch when nobody waits: Anthropic's batch price is half, up to 100,000 requests.
- **What does a 429 mean?** Too many requests. Wait the Retry-After seconds, then try again.
- **Why cap the requests in flight?** So you stay under the limit and avoid a storm of 429s. Watch the per-minute token limits too.
- **What if there is no Retry-After?** Back off: double the wait each try, add a random part, and stop after a set number of tries.
- **Why save each answer as it lands?** A crash then costs only the work in flight, not the whole run.
- **Does retrying fix a spend-cap 429?** No. It has no Retry-After and keeps failing until access resumes, usually next month. Stop and alert.

## Likely follow-up questions

- Q330 How do you handle rate limits and 429 errors? https://designervenkat.online/interview/ai/how-do-you-handle-rate-limits-and-429-errors
- Q331 When do you use batch APIs? https://designervenkat.online/interview/ai/when-do-you-use-batch-apis
- Q337 Timeouts, retries and backoff? https://designervenkat.online/interview/ai/timeouts-retries-and-backoff
- Q471 Implement a rate limiter (per-user and global). https://designervenkat.online/interview/ai/implement-a-rate-limiter
- Q356 Design rate limiting. https://designervenkat.online/interview/ai/design-rate-limiting
- Q182 How do long-running agents use checkpoints? https://designervenkat.online/interview/ai/how-do-long-running-agents-use-checkpoints
