Abstract
Retrieval-Augmented Generation (RAG) systems often face challenges when processing long, noisy documents, resulting in high API costs and significant latency. This paper introduces LongRefiner, a plug-and-play framework designed to enhance long-context RAG by parsing documents into hierarchical XML trees and adaptively filtering content based on query requirements. By shifting structural processing offline, LongRefiner maintains high-quality question-answering performance while achieving a 10x reduction in token usage and a 4x reduction in latency compared to processing full documents.
1. Introduction
Retrieval-Augmented Generation (RAG) has become a standard approach for grounding Large Language Models (LLMs) in external knowledge. However, real-world retrieval often yields lengthy documents that present two primary challenges. First, the low signal-to-noise ratio makes it difficult for LLMs to identify relevant information—the "needle in a haystack" problem. Second, the computational overhead of processing tens of thousands of tokens per query leads to prohibitive costs and latency bottlenecks in production environments.
Existing solutions, such as fixed-length chunking or perplexity-based compression, often degrade document coherence or produce unreadable output that harms the LLM's reasoning capabilities. LongRefiner addresses these issues by preserving document structure through a hierarchical approach that balances local relevance with global context.
2. Methodology
LongRefiner operates through a three-stage pipeline designed for efficiency and accuracy. The system leverages a single foundation model (Qwen2.5-3B-Instruct) fine-tuned with multi-task LoRA to handle different aspects of the refinement process.
2.1 Dual-Level Query Analysis
Not all queries require the same depth of context. A factual question might require specific local facts, while a summary question necessitates a global understanding of the document. LongRefiner employs a lightweight LLM module to analyze the user's query and score it on a continuous scale between "Local" and "Global." This score dynamically weights the importance of specific paragraphs versus the document's overall outline during the selection phase.
2.2 Hierarchical Document Structuring
To maintain logical flow, documents are converted into XML-formatted trees (e.g., <section>, <subsection>, <br>). To ensure computational feasibility during indexing, the system utilizes a "structural tag generation" approach. Instead of full reconstruction, the model generates tags and only the boundary tokens of paragraphs, using a <skip> tag for the interior content. This captures the document's skeleton while reducing the token footprint by approximately 90% during the offline indexing phase.
2.3 Adaptive Document Refinement
During online inference, the system calculates relevance scores for document nodes. Local scores are derived from embedding-based comparisons between the query and leaf nodes (paragraphs). Global scores are calculated by comparing the query against the document's structural outline (titles and abstracts). These scores are dynamically weighted based on the query analysis, allowing the system to select the most pertinent nodes within a predefined token budget and reconstruct a coherent, condensed document for the generator LLM.
3. Experimental Evaluation
The framework was evaluated across seven open-domain QA datasets to test its versatility:
- Single-hop QA: NQ, TriviaQA, and PopQA (used for out-of-domain testing).
- Multi-hop QA: HotpotQA and 2WikiMultiHopQA.
- Long-form QA: ASQA and ELI5. Wikipedia 2018 served as the retrieval corpus for all experiments.
4. Results and Discussion
LongRefiner demonstrates significant improvements over both vanilla RAG and existing compression methods across all tested benchmarks.
As shown in the experimental results, LongRefiner consistently outperforms "Full Content" baselines in accuracy while using 10x fewer tokens. It also surpasses specialized compression methods like LongLLMLingua by over 9% in accuracy metrics. Notably, the online overhead is minimal (approx. 10.8ms), as the most intensive structural processing is performed offline during the indexing phase.
Key insights include the importance of document hierarchy for LLM reasoning. By including parent section titles when a child paragraph is selected, the model provides essential context that flat chunking methods lose. Furthermore, the effectiveness of the <skip> tag as an efficiency mechanism allows for high-fidelity structural extraction without the cost of full document generation.
5. Conclusion
LongRefiner provides a robust solution for long-context RAG by integrating structural awareness with adaptive filtering. By treating documents as hierarchical trees rather than arbitrary text chunks, the system preserves logical relationships and improves the signal-to-noise ratio for downstream generators. Future work will focus on extending the framework to handle multimodal document elements, such as tables and images, and exploring domain-specific adaptations for specialized fields like law and finance.
Summary updated