esc
  • WorkflowsPages
  • Case studiesPages
  • Machine Learning FoundationsLearn
  • AI EngineeringLearn
  • AI Guardrails & Red TeamingLearn
  • UI/UXLearn
  • Design SystemsLearn
  • Production System DesignLearn
  • AI papersPages
  • Interview KitPages
  • Book a callContact
Letโ€™s talk

Iโ€™ll build the agent for it.

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.

13 min readIntermediateCommon ยท estimateFacts checked 27 November 2026

Say it out loud

Read it once, then say it without looking. Start with the short one; use the long one when they ask you to go deeper.

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.

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.

Key words

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.

Pick the path for a 50,000 document run

50,000 documents

one request per document

Does anyone wait?

Live calls

you write the cap and retries

Batch job

Anthropic: half price, most under 1 hr

Results file

match each result by its ID

  1. 1Live: your code sends one request per document, within a cap.
  2. 2Batch: the same requests go in one list for the provider to run.
  3. 3Each live answer is saved with its document ID as it arrives.
  4. 4A batch is ready only when every request has finished. Match each result by its ID, since order varies.
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.
  • โ€œ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.

The live loop: cap, check, wait, save

Next document

from the queue, by document ID

LLM call

at most 4 in flight

What came back?

200: answer

good, so save it

429: too many

wait Retry-After, then retry

Results file

a restart skips these IDs

  1. 1The next document leaves the queue when a slot is free.
  2. 2The model answers, and the reply is fine.
  3. 3The provider says 429: over its limit.
  4. 4Wait the Retry-After seconds, then send the same document again.
  5. 5The answer is saved with its document ID.
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. 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. 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. 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. 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. 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

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."

    BetterThat 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."

    BetterRead 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."

    BetterOne 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?

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?

Can you answer these from memory?

Flash cards ยท tap a card to flip it

1 / 6

What will they ask next?

  1. Q330How do you handle rate limits and 429 errors?12 min
  2. Q331When do you use batch APIs?9 min
  3. Q337Timeouts, retries and backoff?14 min
  4. Q471Implement a rate limiter (per-user and global).12 min
  5. Q356Design rate limiting.12 min
  6. Q182How do long-running agents use checkpoints?12 min

Last updated on 27 November 2026

Building a bulk job over thousands of documents?

I design and build AI products and agents, including the system behind them. Book a short call and tell me how many documents you have and how fast you need them.