Million-Token Context APIs: Principles and Implementation Key Points

    APIs supporting million-token contexts allow developers to submit entire code repositories, long contracts, or multi-chapter documents in a single request. However, alongside these enhanced capabilities, three key questions must be addressed: how models process such extensive inputs, how costs are calculated, and how to ensure stability in production. This article explores these aspects by covering technical challenges, architectural concepts, inference efficiency metrics, and post-training strategies.

    APIs that open up million-token contexts allow developers to submit entire code repositories, long contracts, or multi-chapter documents at once. While the capabilities have improved, three questions need to be clarified first: how the model processes such long inputs, how the cost is calculated, and how to ensure stability after going live. This article expands on these three questions.

    1. Why Long Context is Challenging: Attention Computation and VRAM Usage

    The computation of standard attention grows quadratically with the sequence length; doubling the sequence length approximately quadruples the attention computation. During inference, the model must also save the Key and Value for each token, known as the KV cache. The VRAM usage of the KV cache can be estimated using the following formula:

    VRAM ≈ 2 × Number of Layers × Number of KV Heads × Head Dimension × Sequence Length × Bytes per Element × Concurrent Requests

    Using a sample configuration for calculation: 80 layers, 8 KV heads, head dimension of 128, FP16 precision (2 bytes per element), sequence length of 1 million, and a single request. The result is approximately 3.3 × 10^11 bytes, or about 330 GB. This indicates that in million-token scenarios, VRAM often becomes a bottleneck before computational capacity does. Refer to the official documentation for actual model configurations; the numbers above are only for estimating the magnitude.

    To reduce computation, a common approach is hybrid attention: some layers use local or sparse attention, while others retain global attention. This reduces most of the computation, but the ability to capture global information depends on the specific design. Before going live, you should test the information recall capability on your own tasks rather than relying solely on theoretical complexity.

    2. Understanding Architectural Terminology

    Technical articles often feature the following terms; understanding their meanings helps in judging whether promotional claims are reliable.

    • Hyper-Connections: An expansion of residual connections that provides more paths for information flow between network layers.
    • Manifold Constraints: Mathematical constraints applied to the mixing matrix in hyper-connections to make training more stable. Refer to the original paper for the exact form.
    • Sparse Mixture of Experts (MoE): The model has a large total parameter count, but only activates a subset of experts per inference. Therefore, inference compute depends primarily on the activated parameter count, not the total parameter count.
    • Muon Optimizer: An optimizer that orthogonalizes momentum before updating parameters. Public implementations are available, but its benefits depend on the training configuration and should not be directly applied to other training tasks.

    These terms describe research directions or specific designs. If an article only lists terms without providing controlled experiments, its conclusions should be treated as unverified hypotheses.

    3. Inference Efficiency: Measuring Under Consistent Conditions

    When evaluating long-context inference efficiency, consider the following metrics:

    • Time to First Token (TTFT): The time from submitting a request to receiving the first output token. Long inputs have the greatest impact on this metric.
    • Time per Output Token (TPOT) or Output Speed: The number of output tokens per second.
    • Throughput: The number of completed requests or tokens per unit of time.
    • Unit Cost: The cost per million input/output tokens, or the GPU hours consumed per million tokens.

    When comparing two models or versions, you must specify the model version, input length distribution, output length, batch size, GPU model, and inference framework. Claims of “doubled speed” or “significantly reduced cost” without these conditions cannot be reproduced and cannot be used for decision-making. Your own tests should use real business request samples rather than just short prompts.

    4. Post-Training: Expert Fusion and Capability Retention

    The capabilities of large models in coding, math, and agent tasks are often developed by training multiple domain-specific expert models and then fusing them into a unified model. Fusion typically uses distillation methods, allowing the unified model to learn the output distributions of the experts on the same batch of inputs.

    The main risk during the fusion process is catastrophic forgetting, where the model becomes stronger in new domains but loses performance in original capabilities. A preventive measure is to establish a regression test suite covering representative tasks for each domain, which should be run after every training or version update.

    Generative Reward Models (GRM) prompt the model to generate evaluation reasoning before giving a score. Interleaved thinking allows the model to reason step-by-step before answering. The effectiveness of both must be validated on specific benchmarks.

    5. Choosing Between RAG and Long Context

    Long context cannot completely replace Retrieval-Augmented Generation (RAG). The trade-offs between the two can be referenced in the table below:

    | Dimension