AI & Agents

Local LLM Context Windows: Memory Limits, KV Cache Sizing, and MCP Retrieval

A local LLM context window defines how many tokens a self-hosted model can evaluate in memory simultaneously. Expanding context length causes linear growth in key-value cache memory and quadratic computation during prompt prefill, frequently triggering GPU out-of-memory errors. Sizing your hardware correctly and integrating external MCP retrieval keeps inference fast and accurate without overloading local VRAM.

Tom Langridge 16 min read Updated
Local LLM context windows require balancing model weight storage, KV cache allocation, and external retrieval architecture.

What Is a Local LLM Context Window?

Doubling a local model's context window does not merely double computational work; it inflates the attention key-value cache linearly while quadratic attention calculations saturate memory bandwidth. When an 8-billion parameter model loads into GPU memory, its static weights occupy roughly five gigabytes of VRAM at 4-bit quantization, but expanding its sequence length to 131,072 tokens allocates another sixteen gigabytes solely for the KV cache. On consumer GPUs with sixteen to twenty-four gigabytes of memory, this sudden memory explosion either triggers immediate out-of-memory crashes or forces execution off the GPU and into system RAM, dropping generation throughput from sixty tokens per second to a crawl.

A local LLM context window is the maximum sequence length (in tokens) a locally hosted model can process in memory at once, bounded by available GPU VRAM and attention KV cache size.

Understanding this boundary requires separating static storage from dynamic runtime memory. When you download a model file from Hugging Face or pull an image through Ollama, the file size reflects the parameter weights. In a 4-bit quantized 8B model (such as Llama 3.1 8B Instruct in Q4_K_M), those weights take up approximately five gigabytes on disk and roughly the same footprint in GPU VRAM upon initial load.

However, loading the weights into memory only prepares the model for computation. The moment you transmit a prompt, the inference engine allocates dynamic working memory across two operational phases:

  1. The Prefill Phase: The engine ingests all prompt tokens simultaneously. During this phase, every token computes attention scores against every other token in the prompt, constructing intermediate attention matrices and calculating key and value vectors.
  2. The Generation Phase: The engine generates subsequent tokens autoregressively, one token at a time. To predict each new token, the model must attend to all previous tokens in the sequence.

On hosted consumer interfaces like Claude, file uploads and prompt budgets operate within cloud infrastructure constraints. As detailed in Anthropic's file upload documentation, standard chats accept twenty files at five hundred megabytes each, while projects accept files up to thirty megabytes each with no fixed file count cap, bounded solely by the total context window (checked on Anthropic's help page, 2026-09-14). When users exceed those boundaries in cloud workspaces, the vendor rejects the request.

For self-hosted deployments running on frameworks like llama.cpp, Ollama, LM Studio, or vLLM, there is no remote cluster to absorb excess volume. The physical hardware limits of your local workstation dictate whether an inference job completes in two seconds, stutters across twenty seconds, or crashes with a CUDA out-of-memory error. Teams planning persistent agent architectures often review storage for AI agents to understand how local compute pairs with scalable remote persistence.

KV Cache Sizing: The Mathematics of Memory Allocation

The primary consumer of dynamic memory during extended sequence inference is the key-value (KV) cache. In transformer architectures, calculating attention requires multiplying the query vector of the current token by the key vectors of all preceding tokens, scaling the product, and multiplying by the value vectors.

Recomputing key and value vectors for every preceding token at each generation step would impose cubic time complexity over the full generation loop. To avoid this overhead, inference runtimes store the computed key and value tensors for every token in GPU memory. As the conversation or input document lengthens, this cache expands token by token.

The mathematical formula to calculate the memory required for an unquantized 16-bit KV cache is:

Memory (bytes) = 2 * layers * kv_heads * head_dim * context_len * bytes_per_element * batch_size

Each variable in this equation represents a concrete architectural dimension:

  • 2: Accounting for two distinct tensors, one storing keys and one storing values.
  • layers: The total count of transformer decoder layers in the model architecture (for example, 32 layers in Llama 3.1 8B, 48 layers in Qwen 2.5 14B, or 80 layers in Llama 3.1 70B).
  • kv_heads: The number of key-value attention heads. In older Multi-Head Attention designs, the key-value head count equaled the query head count. Modern architectures use Grouped-Query Attention (GQA), where multiple query heads share a single key-value head. In Llama 3.1 8B, 32 query heads map to 8 KV heads, cutting the KV cache memory footprint fourfold.
  • head_dim: The dimensional size of each individual attention head, generally 128 (calculated as hidden size 4,096 divided by 32 query heads).
  • context_len: The active sequence length in tokens, combining prompt input tokens and generated response tokens.
  • bytes_per_element: The data precision: 2 bytes for standard FP16 or BF16 floating-point values, 1 byte for 8-bit quantized cache (Q8_0), and half a byte for 4-bit quantized cache (Q4_0).
  • batch_size: The number of concurrent inference sequences running simultaneously (typically 1 for personal workstations).

For a standard 8B parameter model with 32 layers, 8 KV heads, and a head dimension of 128 running at 16-bit precision, processing a sequence of 131,072 tokens requires:

2 * 32 * 8 * 128 * 131,072 * 2 bytes = 17,179,869,184 bytes = 16.0 GiB

At 131,072 tokens of context, a 16-bit KV cache requires approximately sixteen gigabytes of additional VRAM for a standard 8-billion parameter model. That sixteen gigabytes sits directly on top of the five gigabytes required for the model weights and roughly two gigabytes allocated for CUDA scratchpads and intermediate activations.

The reference table below illustrates how the KV cache scales across common context lengths for an 8B model with Grouped-Query Attention:

Context Length FP16 KV Cache Q8_0 KV Cache Q4_0 KV Cache Total VRAM (8B Q4 Model + FP16 Cache) Total VRAM (8B Q4 Model + Q8 Cache)
4,096 (4k) 0.50 GiB 0.25 GiB 0.13 GiB 6.9 GiB 6.7 GiB
8,192 (8k) 1.00 GiB 0.50 GiB 0.25 GiB 7.4 GiB 6.9 GiB
16,384 (16k) 2.00 GiB 1.00 GiB 0.50 GiB 8.4 GiB 7.4 GiB
32,768 (32k) 4.00 GiB 2.00 GiB 1.00 GiB 10.4 GiB 8.4 GiB
65,536 (64k) 8.00 GiB 4.00 GiB 2.00 GiB 14.4 GiB 10.4 GiB
131,072 (128k) 16.00 GiB 8.00 GiB 4.00 GiB 22.4 GiB 14.4 GiB

Quantizing the KV cache to 8-bit precision (Q8_0) halves the memory requirement while maintaining high numerical fidelity. However, as model size increases to 14B or 70B parameters, the memory barrier grows severe:

  • 14B Model (48 layers, 8 KV heads): At 131,072 tokens, an FP16 KV cache consumes twenty-four gigabytes of VRAM alone. Combined with nine gigabytes for Q4_K_M model weights, running 131,072 tokens requires thirty-four gigabytes of VRAM, surpassing the twenty-four gigabytes capacity of an RTX 4090.
  • 70B Model (80 layers, 8 KV heads): At 131,072 tokens, an FP16 KV cache demands forty gigabytes of VRAM. Adding forty gigabytes for Q4_K_M weights pushes total memory past eighty gigabytes, requiring dual professional workstation cards like the NVIDIA A100 or H100.
Hardware memory architecture and KV cache sizing for local language models

Why Expanding Local Context Degrades Speed and Output Quality

Expanding context length does not just test memory capacity. It fundamentally alters the computational characteristics and retrieval reliability of the model.

Memory Bandwidth Bottlenecks and Prefill Latency

During the initial prompt prefill phase, the GPU processes the entire input sequence simultaneously. Self-attention requires every token to compare against every preceding token, creating quadratic computational complexity relative to sequence length.

A prompt of 4,096 tokens generates sixteen million attention comparisons. When prompt length expands to 100,000 tokens, the arithmetic volume expands into billions of comparisons. While modern optimizations like FlashAttention reduce intermediate memory allocations by tiling computations across GPU high-speed SRAM, the raw arithmetic intensity remains substantial.

On an NVIDIA RTX 4090, prefilling a prompt of 2,000 tokens takes roughly 150 milliseconds. When the prompt expands to 100,000 tokens of raw text, Time-To-First-Token stretches to between twenty-five and sixty seconds. The local user sits waiting for nearly a minute before the first character appears on the screen.

When total memory requirements exceed physical VRAM, modern operating systems and runtimes spill execution into system memory:

  • PCIe Bus Throttling: On Linux and Windows desktops, llama.cpp or vLLM offload layers or cache allocations to system DDR5 RAM across the PCIe bus. PCIe 4.0 x16 provides roughly thirty-one gigabytes per second of bandwidth, whereas GDDR6X on an RTX 4090 delivers over one thousand gigabytes per second. Shuffling tensors back and forth across the PCIe bus collapses generation speed from seventy tokens per second to two tokens per second.
  • Unified Memory Limits on Apple Silicon: Mac Studio or MacBook Pro systems sharing unified memory avoid PCIe bottlenecks, but memory bandwidth remains bounded. Long context prefill still exhibits noticeable latency spikes compared to short, focused interactions.

Attention Degradation and the Lost in the Middle Effect

Even if your workstation possesses sufficient memory to hold 131,072 tokens without offloading, output quality often deteriorates over long sequences.

Technical benchmarks show that attention fidelity drops as context windows expand. Long-context models experience sharp accuracy degradation around 32k tokens. While the model accepts 131,072 tokens without an explicit error code, its practical comprehension behaves unevenly across the sequence.

This behavior stems from the well-documented lost-in-the-middle phenomenon. Self-attention mechanisms assign strong positional weighting to the very beginning of the prompt (where system prompts and identity instructions reside) and to the very end of the prompt (the immediate user query). In long-context windows, content placed in the middle of the context window is frequently ignored.

When practitioners stuff fifty technical documents or legal contracts into a single local prompt, the model regularly hallucinates details or overlooks explicit instructions buried in the middle thirds of the document payload. The expanded context window gives an illusion of comprehensive ingestion while delivering inconsistent retrieval accuracy.

How to Configure Context Limits in Local Runtimes

Most local inference engines ship with conservative default context lengths to prevent catastrophic out-of-memory errors on entry-level hardware. Simply loading a model that advertises 131,072 tokens does not mean your runtime actually uses that length.

Configuring Ollama Context Parameters

By default, Ollama sets num_ctx to 2,048 or 4,096 tokens, depending on available system memory detected at launch. If you send a document of 15,000 tokens to Ollama without adjusting parameters, the runtime silently truncates the prompt, discarding your text without raising a warning.

To expand context length in Ollama, create a custom Modelfile:

FROM llama3.1:8b
PARAMETER num_ctx 32768
PARAMETER temperature 0.2

Build the new model definition from your terminal:

ollama create llama3.1-32k -f Modelfile

Alternatively, specify num_ctx dynamically in API calls to the Ollama endpoint:

{
  "model": "llama3.1:8b",
  "prompt": "Summarize the architectural constraints detailed below...",
  "options": {
    "num_ctx": 32768
  }
}

Verify your active context allocation by inspecting running instances with ollama ps.

Optimizing llama.cpp and vLLM for Long Sequences

When running llama.cpp from the command line, control the context allocation with explicit flags:

  • Set Context Size: Use -c 32768 (or --ctx-size 32768) to specify sequence length.
  • Enable FlashAttention: Pass --flash-attn to enable tiled attention calculations, reducing peak working memory during prompt prefill.
  • Quantize the KV Cache: Use -ctk q8_0 -ctv q8_0 to quantize both key and value tensors to 8-bit precision. This cuts KV cache memory consumption by half, allowing an 8B model to run 65,536 tokens on a sixteen gigabyte GPU without spilling into system RAM.

For high-throughput inference using vLLM on Linux servers:

python3 -m vllm.entrypoints.openai.api_server   --model meta-llama/Llama-3.1-8B-Instruct   --max-model-len 32768   --gpu-memory-utilization 0.92   --kv-cache-dtype fp8

vLLM implements PagedAttention, which allocates memory for the KV cache in discrete, non-contiguous blocks rather than requiring massive contiguous chunks of VRAM. This avoids memory fragmentation and allows denser concurrency.

Workstation Hardware Sizing Matrix

Match your local context targets to physical hardware specifications:

  • Twelve Gigabytes VRAM (RTX 4070, RTX 3060 with twelve gigabytes): Accommodates 8B models at 16,384 tokens with standard FP16 KV cache, and 32,768 tokens with Q8_0 cache quantization. Attempting 65,536 tokens will trigger immediate memory exhaustion.
  • Sixteen Gigabytes VRAM (RTX 4080, RX 7800 XT): Supports 8B models comfortably at 32,768 tokens in FP16, or 65,536 tokens with Q8_0 quantization.
  • Twenty-Four Gigabytes VRAM (RTX 4090, RTX 3090, RTX 6000 Ada): The practical sweet spot for single-GPU local development. Runs 8B models at full 131,072 tokens in FP16, or 14B models (such as Qwen 2.5 14B) at 32,768 tokens with Q8_0 cache.
  • Unified Memory (Apple Silicon 64 or 128 gigabytes): Provides ample memory headroom to load 70B models at 32,768 tokens or 8B models at 131,072 tokens without out-of-memory errors, though token generation speeds depend on memory bus width.
Fastio features

Connect Local LLM Agents to Intelligent Workspace Storage

Give your local models and agent frameworks access to an indexed document workspace through Fastio's remote MCP server, avoiding VRAM crashes and lost context. Monthly plans start with a 14-day free trial (credit card required).

How to Scale Past Memory Limits with External MCP Retrieval

Trying to solve large corpus analysis by continuously inflating local context windows is an inefficient engineering approach. In software architecture, context windows serve as reasoning registers, not persistent storage databases. Shoving entire folders of documentation, technical specifications, or codebase repositories into a 131,072 token local context window wastes memory, causes long prefill pauses, and risks the lost-in-the-middle failure mode.

The professional pattern decouples document storage and retrieval from the model's active attention window using the Model Context Protocol (MCP).

Intelligent Workspaces and the Remote MCP Pattern

Instead of mounting hundreds of files into local memory, store your source documentation, engineering specs, and project assets in a dedicated Fastio workspace.

Fastio provides shared org-owned workspaces with built-in intelligent workspace retrieval. When Intelligence is enabled on a workspace, incoming documents are automatically indexed for full-text and semantic search without requiring you to configure external vector databases, design chunking strategies, or maintain local embedding pipelines.

Files can be imported directly or connected through Cloud Sync. Cloud Sync connects Google Drive, Dropbox, Box, and OneDrive folders to a workspace, one-way or two-way, on a schedule or on demand.

Your local assistant, IDE coding agent, or orchestration framework connects to the workspace through Fastio's remote MCP server using the Streamable HTTP transport:

  • Interactive Applications and General Clients: Connect Claude desktop, chat assistants, or personal agent runtimes to https://mcp.fast.io/mcp/tools. Sign-in runs via OAuth in the browser to choose permission levels and workspace access.
  • IDE Coding Agents and Terminal Tools: Connect Claude Code, Cursor, Cline, or OpenCode to https://mcp.fast.io/mcp/code. Setup instructions are documented at the Fastio MCP documentation.
  • Headless Frameworks and Automated Scripts: Pass a scoped API key as an Authorization: Bearer <api key> header on the connection to https://mcp.fast.io/mcp/tools or https://mcp.fast.io/mcp/code.

The Targeted Retrieval Loop

With the remote MCP server connected, your local agent executes a lean query loop:

  1. User Asks a Complex Question: The user inquires about a specific configuration requirement across a library of hundreds of internal documents.
  2. Agent Calls MCP Search: Instead of loading all documents into memory, the local model issues a search query over MCP to Fastio's workspace search.
  3. Workspace Returns Ranked Excerpts: Fastio executes hybrid search across the workspace and returns only the 3 to 5 most relevant excerpts, totaling roughly 4,000 to 8,000 tokens.
  4. Local LLM Synthesizes Answer: The local model ingests the focused excerpts within a standard 8,192 or 16,384 token context window, generating an accurate response with clear document citations.

This architectural approach delivers concrete operational advantages:

  • Conserves Local VRAM: Because the local context window remains within 16,384 tokens, the KV cache consumes less than two gigabytes of memory, leaving ample VRAM for model execution on consumer cards like the RTX 4070.
  • Eliminates Prefill Pauses: Evaluating 4,000 tokens takes roughly 250 milliseconds, eliminating the prolonged latency delays typical of massive prompt windows.
  • Bypasses Attention Degradation: Providing only highly relevant excerpts circumvents the lost-in-the-middle trap entirely.
  • Maintains Team Auditability: Workspaces support per-file version history and a detailed activity log that records uploads, edits, moves, and deletions, keeping human contributors and autonomous agents coordinated.
  • Access Boundaries: Role-based permissions set per organization, workspace, and share maintain strict boundaries across different project groups. To evaluate workspace indexing for your agent pipeline, review Fastio pricing to explore plan options.

Sources

References used to verify factual claims in this guide.

  1. 1 Redis: LLM Context Windows Accessed

    Long-context models experience sharp accuracy degradation around 32k tokens.

  2. In long-context windows, content placed in the middle of the context window is frequently ignored.

Frequently Asked Questions

How much VRAM is required for a 128,000-token context window locally?

For an 8-billion parameter model (such as Llama 3.1 8B in Q4_K_M quantization), you need roughly twenty-two gigabytes of VRAM to run a full 131,072 token context window with standard 16-bit KV cache. The model weights require approximately five gigabytes, runtime activations consume about two gigabytes, and the 16-bit KV cache allocates sixteen gigabytes. You can reduce this footprint to roughly fourteen gigabytes total VRAM by quantizing the KV cache to 8-bit precision (Q8_0).

Why does increasing context window slow down local LLMs?

Expanding the context window increases computational work quadratically during the prompt prefill phase, resulting in prolonged Time-To-First-Token delays. Furthermore, when the expanding KV cache exceeds physical GPU VRAM, the runtime offloads layers or tensors across the PCIe bus into system RAM, where memory bandwidth is roughly one-tenth the speed of GPU memory, collapsing generation throughput.

How do I give local LLMs access to large documents without running out of memory?

Use retrieval rather than stuffing complete documents into active context. Index your documents in a searchable repository or workspace (such as Fastio with Intelligence Mode enabled) and connect your local agent via Model Context Protocol (MCP). The model queries the workspace over MCP and retrieves only the 3 to 5 most relevant passages, keeping local context usage within 8,192 tokens while querying gigabytes of documentation.

What is the default context length in Ollama, and how do I change it?

Ollama defaults `num_ctx` to 2,048 or 4,096 tokens depending on system configuration, silently truncating larger inputs. To change it, create a custom Modelfile with `PARAMETER num_ctx 32768` and run `ollama create <name> -f Modelfile`, or pass `{"options": {"num_ctx": 32768}}` in API requests.

What is the difference between FP16 and quantized KV cache?

An FP16 KV cache stores attention key and value vectors at 16-bit floating-point precision (2 bytes per value). Quantizing the KV cache to Q8_0 stores vectors at 8-bit precision (1 byte per value), cutting cache VRAM consumption by half with virtually undetectable loss in perplexity. Q4_0 cuts memory by three-quarters but can introduce noticeable degradation on complex reasoning workloads.

What is the lost-in-the-middle problem in long-context models?

The lost-in-the-middle phenomenon occurs when language models recall information positioned at the very beginning and very end of a prompt with high accuracy, but fail to attend to facts located in the middle. In long prompts, information situated between the 40,000 and 80,000 token marks is frequently overlooked during generation.

Related Resources

Fastio features

Connect Local LLM Agents to Intelligent Workspace Storage

Give your local models and agent frameworks access to an indexed document workspace through Fastio's remote MCP server, avoiding VRAM crashes and lost context. Monthly plans start with a 14-day free trial (credit card required).