Long-Context LLM Inputs: Cost, Compute, and Engineering Constraints

    This article explains why an LLM may seem to process only the first part of a long document. It covers context window limits, token-based billing, prefill compute, KV cache memory, and practical strategies such as chunked summarization, retrieval, and budget control.

    1. Why the Model Seems to Read Only the First Few Pages

    When working with long documents, developers often run into a common problem. They send a very long text to a model in one request, and the system appears to process only the beginning, or it returns a message saying the input is too long. Some people attribute this to insufficient compute, concluding that the model can only read the first few pages or tokens. That explanation is incomplete. The behavior usually results from several constraints working together: the context window, server-side protection policies, token costs, prefill computation, memory, and throughput scheduling.

    First, every model has a context window limit. The context window is the maximum number of tokens a model can handle in a single request, counting both input and output. When the input exceeds this limit, the system must truncate it, reject the request, or require the user to split the content into segments. Second, even when the window is large enough, the service may enforce a maximum input length to prevent very long requests from overloading the queue or causing unpredictable costs. Third, in services that bill by token, sending hundreds of thousands of characters in one request with no cache hits significantly increases input cost. Fourth, long-sequence prefill uses substantial compute and memory bandwidth, which reduces concurrent throughput. In this sense, “the model reads only the first N pages” is often an engineering policy rather than a direct result of hardware capacity.

    2. Why Long Context Is Expensive: Token Billing and Prefill Cost

    Under token-based billing, input cost can be approximated as:

    Input cost ≈ input tokens × input price per token

    Total cost ≈ input tokens × input price per token + output tokens × output price per token

    If you send hundreds of thousands of characters in one request, a rough conversion for mixed Chinese and English text yields hundreds of thousands of tokens. Even when the unit price is low, the cost of a single call grows quickly. If there are no cache hits, every input token must be billed and processed again. This is why very long inputs with no cache hits can create significant cost pressure.

    Billing is not the only cost. The prefill stage also requires computation. Standard self-attention has a complexity of approximately O(n²), where n is the sequence length. Matrix multiplications also depend on the number of layers and the hidden dimension. As the sequence gets longer, prefill takes more time and places more pressure on memory access. To improve throughput, serving systems typically use batching and scheduling. However, very long requests occupy time slices for longer periods, which reduces overall concurrency efficiency. Long context is therefore not a free magnifying glass. It is a capability that requires payment in compute, memory, and scheduling capacity.

    The KV cache is another key constraint. During autoregressive generation, the model must store the key and value states of previous tokens. The approximate size of the KV cache is:

    2 × number of layers × number of heads × head dimension × sequence length × bytes per value

    Doubling the sequence length roughly doubles the KV cache size. When many concurrent requests are added, memory capacity and bandwidth quickly become bottlenecks. Even if a model advertises a large context window, a real service will often set more conservative input policies based on memory, latency, and cost.

    3. Technical Sources of Compute Constraints

    “Insufficient compute” can be broken down into several technical sources. The first is memory capacity and bandwidth. Activations, the KV cache, and intermediate tensors for long sequences consume GPU memory, and insufficient bandwidth slows down attention computation. The second is attention complexity. Standard attention grows with the square of the sequence length, so long documents substantially increase prefill time. The third is scheduling fragmentation. Long requests hold time slices for extended periods, which affects response speed for shorter requests and overall throughput. The fourth is document parsing and transfer. PDFs, tables, and images must first be converted into text. Parsing quality and token inflation both increase the input size. The fifth is cost control. Service providers must balance SLAs, quotas, and billing, so they may impose upper limits on the input length of a single request.

    4. Engineering Practice: Don’t Fill the Context Window Blindly

    For long documents, a safer strategy is to process content in layers, retrieve only what is needed, and control the token budget. Several approaches can help:

    • Chunked summarization: Split the document into overlapping windows, summarize each chunk, and then summarize the summaries.
    • Retrieval-augmented generation (RAG): Use vector search to find relevant passages first, then send only those passages to the model.
    • Structured extraction: Extract the table of contents, headings, tables, and key fields first to reduce irrelevant tokens.
    • Cache reuse: Use caching for stable system prompts, knowledge base fragments, and repeated documents to reduce repeated prefill cost.
    • Sliding windows and incremental summaries: Process long documents over multiple rounds and keep a rolling summary instead of the full original text.

    The following illustrative pseudocode shows the chunking and summarization flow:

    1. def chunk_text(text, size, overlap):
    2. chunks = []
    3. start = 0
    4. while start < len(text):
    5. end = min(start + size, len(text))
    6. chunks.append(text[start:end])
    7. start = end - overlap
    8. if start < 0:
    9. start = 0
    10. if end == len(text):
    11. break
    12. return chunks
    13. def summarize_long_doc(text, token_budget, summarize_fn):
    14. chunks = chunk_text(text, size=4000, overlap=200)
    15. partial = []
    16. used = 0
    17. for c in chunks:
    18. s = summarize_fn(c)
    19. partial.append(s)
    20. used += estimate_tokens(s)
    21. if used >= token_budget:
    22. break
    23. return summarize_fn("\n".join(partial))

    Budget control should also be built in. Set limits for maximum input tokens, maximum output tokens, and maximum retry count for each request. For long documents, prefer offline batch processing or asynchronous jobs. For real-time question answering, prefer retrieval. For multi-turn conversations, keep a rolling summary. On Baidu AI Cloud, you can store source documents in object storage, write call-chain logs to a log service, and monitor token consumption, P95 latency, and error rates through monitoring and alerts. This brings cost and performance into an observable system.

    5. Choosing an Approach

    For a short document with a clear question, you can send the full text directly. For a long document with a focused question, retrieve the relevant passages first. When you need a global understanding of the document, use chunked summarization followed by a final summary. When there is a lot of repeated context, enable cache reuse. For offline workloads, use batch processing and asynchronous queues. For online services, set input limits and fallback strategies.

    The core principle is that the context window defines the upper limit of capability, not the default usage. Token budget, latency targets, and answer quality must be optimized together. Treating “the model can only read the first few pages” as a signal of an engineering constraint helps you design long-document workflows that are more stable and more cost-effective.