Own Weights

LLM long document extraction: chunking and context

LLM long document extraction: why accuracy drops on longer files, how chunking compares with a long context window, and what our test found.

Updated

Part of: AI for customs and logistics

To extract data from a long document with an LLM, you either send the whole text in one call or split it into pieces and extract from each one. Both can work, but accuracy usually falls as the input grows, and the drop is not spread evenly across the page.

Why accuracy drops on longer documents

Models read long inputs, but they do not weigh every part equally. The lost in the middle effect is the name for a pattern seen in many models: facts near the start and end of a long input are picked up more reliably than facts in the middle. A liability cap buried in the middle of a long agreement is the kind of detail that can get missed.

Long documents also repeat themselves. Contracts reuse definitions, boilerplate and cross-references. More near-identical text gives the model more chances to pull the wrong clause.

Does a long context window replace chunking?

It removes the size limit. It does not remove the attention problem. A large context window means the text fits in one call, not that every fact in it gets the same attention.

For a few documents a month, or a one-off review, sending the whole file in one prompt is often the simplest option, and you should test it first. At high volume it gets harder. A single long prompt is costly to repeat, and you have only one answer to check for the whole file. Check your model provider’s current pricing page for long-prompt rates before you decide.

How to process long documents with an LLM

A workable pipeline has four steps.

  1. Convert the file to clean text. Scanned pages need OCR first. Keep the page number for every block of text, because a reviewer will need it.
  2. Split by structure, not by character count. Cut at section headings, article numbers or table edges so a clause is not cut in half. Where a clause does cross a boundary, overlap the two chunks slightly.
  3. Extract each chunk into a fixed schema. Ask for the exact quoted passage and its page number, not a paraphrase. An empty field is a valid answer.
  4. Merge and check. If two chunks answer the same field, keep the answer with the stronger quote. Send conflicts and empty required fields to a person.

Hypothetical example, with round numbers: a 100-page PDF of about 40,000 words is split at section headings into 20 chunks of about 2,000 words each. That is 20 calls. Each answer points to a page, so a reviewer can check any field against the source.

What a trained model changes on long documents

We tested this on contracts. The public exam used 510 commercial contracts from CUAD, a public dataset with answers marked by lawyers. Each model had to find 10 kinds of passage and quote each one exactly. We scored with 5-fold cross-validation, so every document was scored by a model that never saw it in training.

The score is the share of exactly-right quotes, where 1.0 is perfect. Our Accuracy model scored 0.717, our Standard (fast) model 0.650, Claude Opus 0.635 and Claude Haiku 0.584. Our lead over Opus was +0.082 and over Haiku +0.133, both larger than the margin of error. Our Accuracy model was ahead of both in all 5 groups of 102 documents.

Claude took the exam with one prompt, no examples and no task training. Our models were fine-tuned for this exam. This shows what training for one task does. It is not a legal product or legal advice, and it does not mean our model beats Claude at everything.

The exam does not test chunking or document length on its own, so read it as evidence about training for one task. On a narrow task with good data, a fine-tuned model can beat a frontier model. It does not do so every time, so run the same kind of test on your own documents. See the benchmark results for the full numbers.

Cost on the same exam: our Accuracy tier was $4.50 per 1,000 documents, our Standard tier $3.54, Claude Haiku $4.95 and Claude Opus $154.49 at list price. The Standard tier handled 2.7x more documents an hour than Accuracy when running 32 requests at a time. One request at a time, Accuracy wrote about 90 output tokens per second and Standard about 174.

When a frontier model is the better choice

Frontier models are often the right first step. If you have few documents, the task changes every month, or you have no labeled examples yet, a well-written prompt on a frontier API is faster to start. Training needs a test set built from your own files, and that takes time. Build that test first, using the guide on building a test set from your own data.

Self-hosting also adds work. You run the model under your own controls, so you also own the serving setup and the review process. If your answers depend on a large and changing library of documents, retrieval may fit better than training. The fine-tuning vs RAG guide covers that choice.

If you want to see where your current long-document calls stand, run the free AI cost audit on your code. It maps each AI call and what it costs. Or talk to us about one of your document tasks, and we will look at it together. Start at the Own Weights home page for an overview of how we work.

Questions

How to process long documents with an LLM

Split the file at its own section breaks and extract from each piece with the same output schema. Store the page number and the quoted passage for every answer, so a reviewer can check it. Merge the pieces, then send conflicting or missing required fields to a person.

Does a long context window replace chunking?

A long context window removes the size limit, but it does not guarantee that every fact gets the same attention. One large call is also harder to audit than many small ones. Test both approaches on your own documents before you pick one.

What is the lost in the middle problem?

It is a pattern seen in many models, where information near the start and end of a long input is used more reliably than information in the middle. It is worth testing for on your own files rather than assuming it is absent.

How accurate are LLMs on long documents?

There is no single figure, because the answer depends on the model, the task and how the text is split. Our public contract test scored a model trained for one task at 0.717, against 0.635 for Claude Opus, but that test does not isolate document length.

How to extract data from a 100 page PDF with AI

Convert the PDF to text first, running OCR on scanned pages. Chunk the text by section, extract each chunk into a fixed schema with quotes and page numbers, then check a sample of results against the original pages by hand.