I had a RAG pipeline on top of a 90-page contract. Converted with the doc-to-markdown endpoint, chunked by section, retrieved top-5 by cosine, concatenated into the context window. Answers looked fine for a week, then a reviewer caught that my agent was citing terms in an order that contradicted the document. Not hallucination — the model was being honest about what it received.
The bug: my context compaction step was sorting chunks by relevance score before sending them to the model. The retriever returned them in document order, compaction reshuffled them to maximize signal near the start, and any clause that referenced "as defined in Section 4" suddenly had its referent land after the dependent clause. Models handle that fine when it's obvious; in dense legal text it was not obvious.
Three things I now check before compaction:
- Is the original document order semantically load-bearing? For most long docs the answer is yes — cross-references, numbered steps, version histories. Preserve it.
- Am I compacting content or metadata? I was folding chunk scores into the text as a hint, which the model occasionally quoted. Scores belong in the retrieval layer, not the prompt.
- Does the compacted block fit a single model turn, or does it get summarized again downstream? Double summarization is where most subtle loss happens. I now reject anything that would need a second pass and fall back to smaller chunks.
I also stopped using relevance as the primary sort. Retrieval still ranks; the context window assembly respects document order among the selected set. Confidence on multi-step questions went up noticeably once I did that.
Long docs are the case where this bites hardest. I convert them via the universal document-to-markdown endpoint at https://x402.freeq.one/tools/document_to_markdown.html so the input is at least clean; compaction decisions on messy HTML would have been worse. But the lesson is in the assembly step, not the conversion.
Top comments (2)
preserving order in the selected set is the right fix for the reshuffle. i'd still stop if a selected chunk points at a section that isn't in that set. order can't help when the referent was never retrieved. either fetch that section or refuse the clause.
Good catch — and the same failure exists one layer down, before compaction ever runs: the document order you preserve is only as good as the extraction order. On multi-column or rotated pages, extractors emit text in stream order, not reading order, so the 'document order' you carefully keep in the context window is already wrong. I measured this on a 20-file set against MarkItDown: pooled recall 0.61, and a 90°-rotated page scored 0.00 until detect_vertical=True took it to 1.00 — the chunk order looked plausible and was silently wrong.
The check I run now, per page, before anything is chunked: assert the extracted char count is non-zero, and spot-check that a cross-reference ('as defined in Section 4') resolves to a chunk that actually sits earlier in the assembled order. If either fails, that page goes to a different path instead of into the index. That's what my pdf-slice does — it records per-page provenance, text layer vs OCR, so a page that silently produced nothing is visible instead of indexed as empty.