Engineering · AI · Performance
Trimming Huge Pages Without Cutting Out the Answer
How a cut at N tokens became a scoring pass that finds the right content instead of the first content.
Here is the case, as a synthetic example. A research paper has a dense single-column layout, and its page 12 is a 15,000-token literature review covering dozens of studies. A student asks which studies support a link between sleep deprivation and cognitive decline. The system flags page 12 as highly relevant, gives it 8,000 tokens, and does what our code had always done with overflow: it keeps the first 8,000 tokens and discards the rest.
The answer is in tokens 10,000 to 12,000. We sent the model everything except that.
This is part 2 of our series on fitting large files into a question, between Compressing Files So File Chat Uses 92% Fewer Tokens and Auditing Cereby's File Compressor: Why Answers Came From the Preface. Part 1 cut token usage by 92% by choosing which pages to send: the three to five most relevant in full, the rest skipped. That works when content is spread across many pages. This post is about the page bigger than its share, and the query-aware compression we built for it: split the page into chunks, score them against the question, and send the best.
Nothing said anything was missing
A cut by position looks correct from the outside. The model receives a complete block of text, writes a fluent answer, and nothing in the reply signals that anything was missing; a student finds out only if they already know the answer. Early sections got in by default and later ones were dropped by default, whatever the question asked:
Select by relevance, keep the page's order
The new path splits the page into chunks, scores each against the question, and keeps the best. The key decision was to print the kept chunks in their original order, so the model reads coherent prose rather than a relevance-ranked scramble:
Chunks follow paragraph boundaries (200 to 500 tokens each) and fall back to groups of sentences on dense pages with no breaks. A chunk's score is its keyword overlap with the question (60%), topic matching by case-insensitive substring comparison (30%), and a small position bias toward the first and last chunks (10%). That 10% is not a crutch. It reflects how documents are organized, and it breaks ties when the keyword evidence is thin. Selection is a greedy pass: sort by score and add chunks until the budget runs out. We tested more complex optimizations, and the quality difference did not justify the overhead. The kept chunks are joined with gap markers:
[Content compressed: Showing 8/25 most relevant sections]
Introduction text...
[... 4 sections omitted ...]
Relevant content about sleep deprivation and cognitive decline...
[... 2 sections omitted ...]
More relevant findings...
[... 10 sections omitted ...]
The markers matter. Without them, the model tries to infer connections across gaps it cannot see, which is one of the cleaner paths to hallucination. And when no chunk contains the question's keywords, usually because the question is phrased very differently from the document, selection falls back to position-based sampling as a floor.
The methodology section, found
Our concrete test case was a research paper and a question about its methodology:
document a 25,000-token research paper
question about its methodology
old cut the first 8,000 tokens: introduction and background
→ the methodology section is never sent
query-aware chunks scored against the question
→ 7 chunks, 7,550 tokens, the methodology among them
→ answered correctly
Across the answers we measured, correct ones went from 52% to 94%, partial ones from 31% to 5%, and failures from 17% to 1%. User satisfaction went from 2.8 to 4.7 out of 5.
What this taught us
The failure that cost the most was the silent one. Answers that are wrong in obvious ways get reported. Answers that are fluent but incomplete, because the relevant section sat 2,000 tokens past the cut, do not, and that class of bug is caught only by measuring it on purpose.
Semantic boundaries matter more than tight token accounting. Cutting mid-paragraph to hit an exact budget costs more in coherence than it saves in tokens. The same held for selection: a greedy pass over scored chunks was good enough, and it was faster than a more involved optimization.