Why LLM Long Context Is Limited: Cost, Compute, and Memory Constraints

    This article explains why large language models struggle with long documents. The real bottleneck is not the context window size alone, but the nonlinear cost of full attention, memory and bandwidth limits that reduce concurrency, and caching behavior that changes the cost structure. It also outlines practical engineering approaches such as chunking with overlap, retrieval-augmented generation, layered summarization, cache reuse, structured extraction, and observability with budget controls, plus guidance on choosing billing and architecture patterns.

    1. The Real Issue: It Is Not That the Model Cannot Read, It Is That It Cannot Afford to Compute

    After uploading a document hundreds of pages long, many users notice that the model responds to only a small portion at the beginning. They often conclude that limited compute is why the model can only read the first twenty pages or ten thousand tokens. This description is intuitive, but it mixes two different things: the capacity limit of the context window and the cost limit of performing full attention across that window. The first determines how much can be packed in; the second determines whether packing it in is worthwhile. What truly makes long-text processing expensive is the latter.

    From an engineering perspective, the cost of a single request can be roughly broken into three parts: compute and GPU memory usage on the input side, token-by-token generation on the output side, and caching mechanisms introduced to reduce repeated computation. Long-document scenarios amplify the first part to the point where it dominates, which is why the pain becomes a common experience.

    2. Four Real Sources of Constraint

    2.1 The Context Window Is a Hard Boundary

    The context window is the maximum total number of tokens a model can perceive in a single inference call, and input and output share this allowance. There is no fixed conversion between tokens and “pages”; Chinese, English, code, tables, and parsed PDF layouts all significantly change token density. Therefore, “twenty pages” or “ten thousand tokens” is only an empirical value for a certain type of document under specific settings, not a universal rule. A more reliable engineering practice is to run tokenization statistics first and then decide on a splitting strategy.

    2.2 Attention Complexity Grows Nonlinearly

    Standard self-attention requires every token to interact with all other tokens, so the computation grows quadratically with sequence length. Increase the sequence tenfold and the computation grows roughly a hundredfold. This explains why stuffing an entire document in is not cost-effective in terms of both cost and latency, and it is the fundamental reason why sparse attention, block attention, and linear attention approaches exist.

    2.3 Memory and Bandwidth Determine Concurrency

    During inference, GPU memory must hold weights, activations, and the key-value cache at the same time. The longer the sequence, the larger the key-value cache, and the fewer concurrent requests a single card can support. Once throughput drops, the service provider can only protect stability through rate limiting, truncation, or tiered pricing. On the user side, this feels like being “limited to the first section.”

    2.4 Caching Changes the Cost Structure

    Mechanisms such as prefix caching and context reuse make repeated prefixes nearly free, while loading six hundred thousand characters at once with no cache at all is the worst case: full computation, full billing, and no reuse. This is exactly the pitfall that cost-sensitive users are most likely to fall into.

    Illustrative pseudocode helps clarify the difference:

    1. # Counter-example: full input with no cache
    2. prompt = load_full_document() # extremely long prefix
    3. resp = model.generate(prompt) # full attention, highest cost
    4. # Practice: retrieval + chunking + summarization
    5. chunks = split(load_full_document(), size=800, overlap=80)
    6. topk = retrieve(query, chunks, k=6)
    7. ctx = summarize(chunks) + concat(topk)
    8. resp = model.generate(ctx) # short context, controllable cost

    3. Practical Engineering Approaches

    1. Chunking with overlap: Split along semantic boundaries (headings, paragraphs, code blocks) and keep moderate overlap to avoid breaking information across chunks.
    2. Retrieval-augmented generation: Use vector or keyword retrieval to locate relevant fragments first, send only the highly relevant fragments to the model, and turn “read the whole text” into “read the key pages.”
    3. Layered summarization: Generate a short summary for each chunk first, then summarize the set of summaries again to form a reusable document-level view.
    4. Cache reuse: Place stable system prompts, specification notes, and long-document summaries at the prefix position to amortize repeated costs using prefix caching.
    5. Structured extraction first: Extract tables, clauses, and field-type information into structured data with rules or a lightweight model first, then hand it to the large model for reasoning.
    6. Observability and budget control: Record input and output tokens, hit rate, and latency for each request, and set budget thresholds and fallback strategies.

    4. Selection and Architecture Recommendations

    If the business is cost-sensitive with stable call volume, it is worth evaluating plan-based quotas on the platform side; if volume fluctuates and elasticity is required, pay-as-you-go billing is more suitable. Either way, the architecture should introduce object storage to hold original documents, a message queue for asynchronous parsing, and a container platform to host splitting and retrieval services, with logging services and monitoring alerts to track token consumption and failure rates. Baidu AI Cloud offers a relatively complete combination of these general capabilities, making it easier to build the long-document pipeline into a standard component that is operable and scalable.

    5. Conclusion

    “Can only read the first twenty pages” is not a defect in model capability, but an inevitable result of complexity, memory, and cost acting together. Only by turning the long-text problem into a combination of retrieval, chunking, summarization, and caching can you achieve stable, predictable, and economical results with limited compute.