The Longformer architecture replaces the standard O(n²) self-attention of the original Transformer with a composite sparse attention pattern. It uses a sliding window of fixed size (typically 512 tokens) for local context, combined with pre-specified global attention positions—such as [CLS] tokens—that attend to the entire sequence. This hybrid design scales linearly with sequence length, enabling direct processing of full legal briefs, multi-page contracts, and consolidated case law without truncation or chunking.
Glossary
Longformer

What is Longformer?
Longformer is a Transformer model that employs a sparse attention mechanism combining local sliding window and global task-specific patterns to process sequences up to 4,096 tokens with linear memory scaling, eliminating the quadratic bottleneck of standard self-attention for long-document legal NLP tasks.
For multi-document legal reasoning, Longformer's global attention mechanism is critical. By designating query tokens or section headers as global, the model builds task-specific representations that bridge distant sections of a single document or link related passages across a corpus. This capability supports cross-document alignment and factual consistency verification in legal summarization pipelines, where a generated statement must be grounded against evidence scattered across hundreds of pages of source material.
Key Features of Longformer
The Longformer introduces a sparse attention mechanism that scales linearly with sequence length, enabling Transformer models to process entire legal documents without truncation.
Sliding Window Attention
A fixed-size window of w tokens surrounding each position attends locally, capturing immediate context. This reduces complexity from O(n²) to O(n × w).
- Default window size is typically 512 tokens
- Each token attends to w/2 tokens on each side
- Stacks multiple layers to build a large receptive field
- Critical for processing dense legal paragraphs where local context defines meaning
Dilated Sliding Windows
Increases the receptive field without increasing computation by inserting gaps between attended tokens. The dilation factor grows exponentially across layers.
- Lower layers use small dilation for fine-grained syntax
- Upper layers use large dilation for document-level semantics
- Enables a theoretical receptive field spanning thousands of tokens
- Essential for tracking obligations defined across distant sections of a contract
Global Attention Tokens
Designates specific tokens—such as [CLS] or task-specific markers—to attend to every token in the sequence. These act as information hubs for sequence-level tasks.
- Used for classification tokens and question tokens in QA
- Global tokens receive attention from all positions
- Symmetric: global tokens also attend to all positions
- Enables effective multi-document fusion by using global tokens to aggregate cross-document evidence
Linear Memory Scaling
Full self-attention requires memory proportional to n², making it infeasible for long legal documents. Longformer's memory footprint grows linearly with sequence length.
- Processes sequences up to 4,096 tokens in base configuration
- Extended variants handle 16,384+ tokens
- Enables end-to-end processing of full contracts without chunking
- Preserves cross-reference integrity that chunking strategies destroy
Task-Specific Global Patterns
The global attention pattern is configurable per task. For legal text summarization, global tokens can be placed on section headers and defined terms to anchor the summary.
- Classification: global attention on [CLS]
- QA: global attention on question tokens
- Summarization: global attention on key structural markers
- Allows the model to learn which positions require document-wide context
Pre-Training Objective Compatibility
Longformer uses the same masked language modeling (MLM) objective as BERT and RoBERTa, enabling initialization from existing pre-trained checkpoints.
- Initialized from RoBERTa weights for general language understanding
- Further pre-trained on legal corpora for domain adaptation
- Compatible with standard fine-tuning pipelines
- Supports both extractive and abstractive summarization heads
Longformer vs. Other Long-Context Transformers
A technical comparison of the Longformer's sliding window and global attention mechanism against other prominent sparse and efficient Transformer architectures designed for long-document processing.
| Feature | Longformer | BigBird | Reformer |
|---|---|---|---|
Attention Mechanism | Sliding Window + Dilated Sliding Window + Global | Random + Window + Global | Locality-Sensitive Hashing (LSH) |
Complexity Class | O(n) | O(n) | O(n log n) |
Max Sequence Length (Reported) | 4,096 tokens | 4,096 tokens | 64,000 tokens |
Global Attention on Special Tokens | |||
Autoregressive Decoding Support | |||
Pre-trained Checkpoints Available | |||
Dilated Sliding Window | |||
Compatible with Standard Transformer Layers |
Frequently Asked Questions
Clarifying the mechanics, applications, and advantages of the Longformer model for processing extended legal documents.
Longformer is a modified Transformer model designed to process long documents efficiently by using a sparse attention mechanism that scales linearly with sequence length. Unlike standard Transformers, which use full self-attention with a quadratic O(n²) memory cost, Longformer introduces an attention pattern combining a sliding window of local context with global attention on pre-selected tokens. The sliding window allows each token to attend to a fixed number of neighbors on either side, capturing local context. Simultaneously, task-specific tokens—like the [CLS] token for classification or question tokens for QA—are designated as global, attending to all tokens and being attended to by all tokens. This hybrid approach reduces the computational complexity to O(n), enabling the processing of documents up to 4,096 tokens in the base model, with variants scaling to even longer sequences, making it ideal for multi-page legal contracts and lengthy judicial opinions.
Enabling Efficiency, Speed & Accuracy
Intelligent Analysis, Decision & Execution
We build AI systems for teams that need search across company data, workflow automation across tools, or AI features inside products and internal software.
Talk to Us
Search across company data
Give teams answers from docs, tickets, runbooks, and product data with sources and permissions.
Useful when people spend too long searching or get different answers from different systems.

Automate internal workflows
Use AI to route work, draft outputs, trigger actions, and keep approvals and logs in place.
Useful when repetitive work moves across multiple tools and teams.

Add AI to products and internal tools
Build assistants, guided actions, or decision support into the software your team or customers already use.
Useful when AI needs to be part of the product, not a separate tool.
Related Terms
Mastering Longformer requires understanding its surrounding architecture. These concepts define how sparse attention integrates into the broader legal NLP pipeline.
Sparse Attention
The core algorithmic innovation powering Longformer. Unlike standard self-attention, which scales quadratically (O(n²)), sparse attention restricts each token to a fixed-size set of other tokens. Longformer combines a sliding window for local context with global attention on pre-selected tokens (like [CLS]) to maintain long-range connectivity while achieving linear O(n) complexity.
BigBird
A contemporary sparse-attention Transformer also designed for sequences up to 4096 tokens. BigBird uses a combination of random, window, and global attention patterns. It is mathematically proven to be a universal approximator and is often benchmarked against Longformer for long-document tasks like legal summarization.
Hierarchical Summarization
A complementary strategy for documents exceeding even Longformer's 4096-token window. The process involves:
- Chunking the document into overlapping segments.
- Summarizing each chunk independently with Longformer.
- Re-summarizing the collection of chunk summaries to produce a final, coherent output. This recursive approach is essential for full-length contracts or multi-hundred-page rulings.
Coreference Resolution
A critical pre-processing step for Longformer in legal contexts. This NLP task identifies all mentions that refer to the same entity (e.g., 'Acme Corp.', 'the Company', 'it'). Resolving these links before summarization ensures that Longformer's global attention correctly associates facts and obligations with the right party, preventing entity confusion in the final summary.
Source Attribution
The technique of explicitly linking each factual statement in a generated summary back to its precise location in the source document. When using Longformer for multi-document fusion, source attribution is vital for legal citation integrity. It allows a reviewing attorney to instantly verify a claim against the original contract clause or case paragraph.
Factual Consistency
The degree to which a Longformer-generated summary accurately reflects the source text without contradiction. This is measured using Natural Language Inference (NLI) models that check if the summary is entailed by the source. In legal AI, factual consistency is non-negotiable; a hallucinated obligation or misstated precedent is a critical failure.

About the author
Prasad Kumkar
CEO & MD, Inference Systems
Prasad Kumkar is the CEO & MD of Inference Systems and writes about AI systems architecture, LLM infrastructure, model serving, evaluation, and production deployment. Over 5+ years, he has worked across computer vision models, L5 autonomous vehicle systems, and LLM research, with a focus on taking complex AI ideas into real-world engineering systems.
His work and writing cover AI systems, large language models, AI agents, multimodal systems, autonomous systems, inference optimization, RAG, evaluation, and production AI engineering.
Partnered with leading AI, data, and software stack.
How We Work
Custom AI workflows for your Business
One-fit-all AI don't work for modern businesses. At Inferensys, we aim to understand your business & custom requirements; which we use to define most efficient agentic workflows, the data, and the tools for your business.
01
Review the use case
We understand the task, the users, and where AI can actually help.
Read more02
Pick the right approach
We define what needs search, automation, or product integration.
Read more03
Build the first useful version
We implement the part that proves the value first.
Read more04
Improve from there
We add the checks and visibility needed to keep it useful.
Read moreThe first call is a practical review of your use case and the right next step.
Talk to Us