Context Window Mechanics
As of 2026-11-14
What is Context Window Mechanics?
Doubling context length quadruples attention compute (O(n²)), and the KV cache grows linearly with it: Llama 3.3 70B at 131,072 tokens needs ~42.9 GB of KV cache per request on top of ~140 GB of BF16 weights — about 183 GB, so three H100 80 GB GPUs, not two.
What it is
The context window is the maximum number of tokens an LLM can take into account in one request — the prompt plus the output it generates. It governs what the model can "see" when it generates each token and decides whether a workload fits in a single call or needs a chunking strategy such as RAG or sliding-window overlap.
The problem it addresses is that useful enterprise documents — ABAP class files, procurement contracts, annual reports — routinely exceed a few thousand tokens, so naive truncation loses critical context and naive concatenation exceeds the model's capacity. For SAP analytics consultants this is concrete: a Joule-adjacent agent reasoning over a complete plant-maintenance history (hundreds of pages of service orders) or a full set of contract amendments can strain even a 128K-token model.
Why it matters
- FlashAttention-2 keeps attention memory linear in sequence length (exact attention computed in SRAM tiles); the compute stays O(n²), so long prompts remain expensive.
- 'Lost in the middle': accuracy is highest at the edges of the context and drops for information placed in the middle (GPT-3.5-Turbo's multi-document QA by more than 20%, Liu et al. 2023) — long windows don't guarantee uniform attention, a real risk for document-heavy Joule use cases.
- GPU sizing is weights plus KV cache plus headroom: Llama 3.3 70B at 131,072 tokens needs ~140 GB + 42.9 GB ≈ 183 GB, i.e. three H100 80 GB GPUs for a single full-length request.
Key points
- KV cache memory formula: 2 × n_layers × n_kv_heads × d_head × seq_len × batch × dtype_bytes — every factor is a variable an architect must size for the target model and sequence length.
- Llama 3.3 70B at 131,072 tokens, batch 1, BF16: 42.9 GB (40.0 GiB) of KV cache; with ~140 GB of weights that is ~183 GB, so at least three H100 80 GB GPUs before activations and overhead.
- The KV cache scales linearly with batch: four concurrent full-length requests on the same model need ~171.8 GB of cache alone.
- 'Lost in the middle' (Liu et al. 2023): accuracy follows a U-shape, highest when the relevant passage is at the start or end of the context — place the most critical facts at the beginning or end of the prompt.
- FlashAttention-2 makes attention memory linear instead of quadratic by tiling exact attention in SRAM; attention compute stays quadratic.
- YaRN extends a trained RoPE model beyond its training length with 10× fewer tokens and 2.5× fewer steps than earlier methods (Llama 2 7B/13B extended to 64K and 128K) — measure quality at your target length rather than assuming it.
- RoPE θ is necessary, not sufficient: Llama 3 used θ = 500,000 yet pre-trained at 8K and reached 128K only through a six-stage long-context phase on ~800B tokens — check how long-context training was done, then test at your own lengths.
- Hybrid RAG + long context is a common pattern: retrieval cuts the candidate set to a fixed budget, the long-context model reasons over the reduced set; in SAP AI Core the retrieval step is the orchestration service's document-grounding module.
- Everything in the request counts against the window — system prompt, tool definitions, history, documents and the generated output — so measure the non-user share with a token counter instead of budgeting a fixed percentage.
- Cost scales with tokens: at the same per-token price a 131,072-token prompt costs 16× an 8,192-token prompt, and some providers charge more above a length threshold — route short queries to short prompts.
Terms used on this page
- Context window
- Maximum number of tokens a model takes into account in one request, prompt and output included; determines the information horizon for generation and the choice between a single call and chunking.
- KV cache
- Stored keys and values of all prior positions, so that each new token attends to them instead of recomputing them; grows linearly with sequence length and batch and is the dominant variable memory cost in long-context inference.
- FlashAttention-2
- IO-aware exact attention algorithm (Dao 2023) that tiles computation in SRAM, making attention memory linear rather than quadratic in sequence length; about 2× faster than FlashAttention.
- Lost in the middle
- Empirical finding (Liu et al. 2023) that LLM accuracy follows a U-shape: highest when the relevant information sits at the beginning or end of a long context, lowest in the middle.
- YaRN
- Yet another RoPE extensioN (Peng et al. 2023): compute-efficient method to extend a trained RoPE model beyond its training length with limited fine-tuning; quality at the target length must be measured, not assumed.
- RoPE θ_base
- Base frequency of Rotary Position Embedding, setting how fast Q/K vectors rotate with position; Llama 3 and Llama 3.3 use 500,000, but long context also requires long-context training.
- Semantic chunking
- Chunking strategy that splits documents at natural semantic boundaries (paragraphs, sections, headings) rather than fixed token counts — preserves coherence at the cost of variable chunk sizes.
- Hierarchical chunking
- Chunking strategy that stores document summaries at a top level and detailed passages at a child level; retrieval first finds the relevant summary, then fetches the associated detail.
Sources
- Liu et al. 2023 — Lost in the Middle: How LLMs Use Long Contexts
- Peng et al. 2023 — YaRN: Efficient Context Window Extension of Large Language Models
- Dao 2023 — FlashAttention-2: Faster Attention with Better Parallelism and Work Partitioning
- Llama Team, Meta — The Llama 3 Herd of Models (RoPE θ = 500,000; 8K pre-training; six-stage extension to 128K)
- Llama 3.3 70B Instruct — config.json (80 layers, 8 KV heads, head_dim 128, 131,072 positions, BF16; Hugging Face mirror)
- NVIDIA — H100 Tensor Core GPU (H100 SXM: 80 GB, 3.35 TB/s)
- OpenAI — GPT-4o model page (128,000-token context window)
- SAP Business AI — official product page
- SAP Generative AI — official product page
- Anthropic (Claude documentation) — Context windows: what counts, sizes by model (checked 2026-11-14)
- SAP Help Portal — Orchestration service: document grounding as a configurable RAG pipeline module
- SAP Learning — Selecting a suitable model in generative AI hub (SAP Note 3437766: models, token conversion rates, rate limits, deprecation)
- Microsoft Learn — Overview of Copilot in Fabric (Azure OpenAI Service, Fabric capacity consumption)
- SAP Help Portal — Generative AI hub in SAP AI Core, model access and metering overview
Full card with the Consultant plan — €19.90/month. What the full card adds: the full decision framework · the SAP vs Snowflake / Databricks / Fabric comparison · the common pitfalls and their fix · the cheat sheet · the architecture schemas · the code blocks · the facts worth quoting.