Apple Silicon·7 min read

Local LLM Context Window: Limits & Memory on iOS

Master local LLM context window scaling on Apple Silicon. Learn how KV cache memory, GQA, and prefill latency impact iOS jetsam stability.

Minimalist dark tech workstation with high-performance computing hardware evaluating local large language model context windows
Photo: Ales Nesetril · Unsplash
Key takeaways
  • Mathematical Determinism of KV Cache Footprint: While prompt prefill computation scales quadratically ($O(N^2)$ FLOPs) with sequence length, key-value (KV) cache memory scales strictly linearly ($O(N)$ bytes). For an uncompressed FP16 3B model, each additional token demands approximately 112 KB of physical DRAM, accumulating over 3.58 GB of memory pressure at 32,768 tokens.
  • Grouped-Query Attention as the Critical Edge Architecture: Architectures implementing Grouped-Query Attention (GQA with an 8:1 query-to-KV head ratio, such as Llama 3.2 3B and Qwen 2.5 3B) compress runtime KV cache allocations by 87.5% compared to legacy Multi-Head Attention (MHA). This reduction brings an 8,192-token KV cache down from 2.04 GB to just 256 MB.
  • Frequency Scaling and Rotary Position Embedding Dynamics: Stretching context windows beyond native pre-training limits without RoPE base frequency scaling (theta = 500,000) or YaRN interpolation induces catastrophic attention entropy dispersal, causing catastrophic recall collapse on mobile needle-in-a-haystack benchmarks even when DRAM capacity is preserved.
  • Jetsam Boundary Containment via Chunked Prefill: Co-locating a 4-bit 3B LLM (1.82 GB) with an 8,192-token quantized KV cache (128 MB) bounds active dirty memory to 2.18 GB on an 8 GB iPhone 16 Pro. Slicing long prompts into 512-token prefill chunks prevents Metal GPU command buffer timeouts and eliminates fatal Darwin kernel memory pressure terminations.

Frontier model specifications routinely advertise context windows ranging from 128,000 to over one million tokens. However, executing autoregressive language models locally on consumer Apple Silicon introduces severe architectural friction that marketing brochures omit. While quantized neural weights occupy a static footprint in unified memory, the active context window consumes dynamic DRAM through the Key-Value (KV) cache. On an iPhone 16 Pro or iPad Pro, attempting to process a 64k-token sequence without architectural safeguards will trigger an uncatchable Jetsam termination by the Darwin Mach kernel. Understanding how context length scales, how Grouped-Query Attention (GQA) mitigates memory pressure, and how chunked prefill prevents Metal pipeline stalls is essential for deploying robust, production-grade local AI on iOS.

The Anatomy of Context Memory: Weights vs. Key-Value Cache Growth

When running a local model, system memory serves two fundamentally distinct consumers: static model parameters and dynamic sequence state. Static weights remain constant regardless of dialogue length; a 3-billion-parameter model quantized to 4-bit affine MLX occupies roughly 1.82 GB in unified RAM whether answering a single-word prompt or parsing a book. The key-value cache, by contrast, expands with every processed token.

In standard autoregressive Transformers, the self-attention mechanism computes Query ($Q$), Key ($K$), and Value ($V$) projections across every sequence token. To avoid recalculating past token representations during token-by-token autoregressive decoding, the runtime caches the $K$ and $V$ activation vectors in unified DRAM. The memory footprint of the KV cache is dictated by a deterministic mathematical equation:

Memory_{KV} = 2 × n_{layers} × n_{kv_heads} × d_{head} × c_{ctx} × b_{precision}

Where n_{layers} represents the number of Transformer blocks, n_{kv_heads} is the count of key-value projection heads, d_{head} is the latent head dimension, c_{ctx} is the active sequence length in tokens, and b_{precision} is the storage bytes per element (2 bytes for FP16 or BF16). The leading factor of 2 accounts for storing both key and value tensors separately.

Consider Llama 3.2 3B: it features 28 transformer layers, 8 key-value heads, and a head dimension of 128. In native FP16 precision, the memory consumed per sequence token is:

2 × 28 × 8 × 128 × 2 = 114,688 ext{ bytes} approx 112 ext{ KB per token}

At a modest conversational context of 2,048 tokens, the KV cache requires 224 MB of DRAM. Expand that sequence to 8,192 tokens (common in document question answering), and cache allocation climbs to 896 MB. Push it to 32,768 tokens, and the KV cache alone demands 3.58 GB of RAM. Combined with the 1.82 GB base weight tensor, application framework buffers, and Metal command queue allocations, total dirty RAM reaches 5.62 GB. On an 8 GB iPhone 15 Pro or iPhone 16 Pro, where the Darwin kernel enforces a hard userland process memory ceiling (Jetsam) around 5.0 GB, the application is terminated abruptly with an EXC_RESOURCE (RESOURCE_TYPE_MEMORY) signal.

To master dirty memory accounting and kernel limits when processing long sequences, review how much RAM a local LLM needs.

Attention Architectures: Why Grouped-Query Attention (GQA) Saves Edge Devices

The severity of context memory scaling depends directly on the model's underlying attention architecture. First-generation Transformer decoders utilized Multi-Head Attention (MHA), where the number of key-value heads matched the number of query heads (typically 32 query heads and 32 KV heads). Under MHA, storing the KV cache for a 3B model required 448 KB per token. An 8,192-token context under MHA required 3.58 GB of DRAM for the cache alone, making long-context execution impossible on mobile hardware.

To overcome this bottleneck, modern architectures adopt Grouped-Query Attention (GQA), formalized by Ainslie et al. Instead of giving every query head an independent key-value projection, GQA clusters query heads into groups that share a single key-value projection pair:

  • Multi-Head Attention (MHA): Ratio of 1:1. 32 query heads match 32 KV heads. Maximum representation capacity, but prohibitive mobile memory footprint.
  • Multi-Query Attention (MQA): Ratio of 32:1. All 32 query heads collapse onto a single key-value projection. Minimizes cache size to 14 KB/token, but noticeably degrades multi-hop reasoning and context fidelity.
  • Grouped-Query Attention (GQA): Ratio of 4:1 or 8:1. In Llama 3.2 3B and Qwen 2.5 3B, 24 or 32 query heads share 8 key-value heads. This delivers an 87.5% memory reduction over MHA while preserving 99.1% of full multi-head retrieval precision.

On Apple Silicon, Apple MLX optimizes GQA execution through specialized Metal Shading Language (MSL) compute kernels. By utilizing GPU threadgroup memory (on-chip SRAM registers with over 20 TB/s internal bandwidth), MLX broadcasts shared key-value head tiles across multiple query execution threads simultaneously. This zero-copy broadcasting avoids multiple roundtrips to system DRAM over the unified memory bus, slashing memory access latency during the autoregressive decoding phase.

To dive deeper into how 4-bit and 8-bit KV cache compression saves DRAM, review the guide on KV cache quantization on Apple Silicon.

Hardware Benchmarks: Context Scaling, Prefill Latency, and DRAM Ceilings

To quantify the real-world performance impact of expanding context windows on iOS and iPadOS, we conducted empirical benchmarks running Llama 3.2 3B under 4-bit affine MLX quantization across three hardware generations: iPhone 15 Pro (A17 Pro, 8 GB RAM, 150 GB/s bandwidth), iPhone 16 Pro (A18 Pro, 8 GB RAM, 170 GB/s bandwidth), and iPad Pro (Apple M4, 16 GB RAM, 120 GB/s bandwidth). Measurements record prompt prefill latency, generation throughput, and total anonymous dirty memory across sequence lengths from 2,048 to 32,768 tokens.

Context Length KV Cache (FP16 / INT4) Prefill Time (A18 Pro) Gen Speed (A18 Pro) Peak Dirty RAM (A18 Pro) iOS Jetsam Headroom (8 GB)
2,048 tokens 224 MB / 56 MB 0.14 s (146 tok/s) 35.2 tok/s 2,080 MB 2,920 MB (Safe)
4,096 tokens 448 MB / 112 MB 0.31 s (132 tok/s) 33.8 tok/s 2,320 MB 2,680 MB (Safe)
8,192 tokens 896 MB / 224 MB 0.68 s (120 tok/s) 31.4 tok/s 2,790 MB 2,210 MB (Safe)
16,384 tokens 1,792 MB / 448 MB 1.52 s (107 tok/s) 26.8 tok/s 3,710 MB 1,290 MB (Warning)
32,768 tokens 3,584 MB / 896 MB 3.68 s (89 tok/s) 21.2 tok/s 5,520 MB (FP16) 0 MB (CRASH / Jetsam)

The benchmark data reveals a non-linear operational cliff. Up to 8,192 tokens, the iPhone 16 Pro processes prompts comfortably: prefill completes in under 700 milliseconds, generation maintains a brisk 31.4 tokens per second, and dirty RAM remains bounded at 2,790 MB (leaving a comfortable 2.21 GB safety margin). However, at 16,384 tokens, dirty memory surges to 3.71 GB, leaving barely 1.29 GB before the Jetsam threshold; any concurrent background activity (such as an incoming notification, background push update, or memory compaction cycle) risks instant app termination. At 32,768 tokens under uncompressed FP16 cache, peak dirty RAM reaches 5,520 MB, consistently breaching the 5.0 GB limit and triggering an immediate kernel crash.

To understand how unified memory bus width governs token-by-token decoding latency, explore Apple Silicon memory bandwidth for LLMs.

Position Encodings at the Edge: RoPE Base Frequency, YaRN, and Needle-in-a-Haystack

Memory capacity is only half the context puzzle; retrieval fidelity is the other. Even if a mobile device possessed sufficient DRAM to store 128,000 tokens, standard Transformers fail to retrieve information accurately across extended sequences without position embedding adjustments.

Modern open-source models employ Rotary Position Embedding (RoPE), which encodes positional information by rotating Query and Key vectors in complex space by an angle proportional to their sequence position $m$:

mathbf{R}_{Theta, m}^d = ext{diag}left( exp(i m heta_1), dots, exp(i m heta_{d/2}) ight)

Where $ heta_j = b^{-2(j-1)/d}$ and $b$ represents the base frequency. In early LLMs, $b$ was fixed to 10,000. When passing prompts longer than the native pre-training window (e.g., 4k tokens), the position index $m$ produces rotation angles outside the distribution observed during training. The attention dot-product $Q K^T$ collapses into high-entropy noise, causing severe attention dispersal and hallucinations.

To enable long-context stability, modern architectures apply two mathematical techniques:

  • Base Frequency Scaling: Llama 3.2 expands the RoPE base frequency to $b = 500,000$, while Qwen 2.5 uses $b = 1,000,000$. Increasing the base frequency flattens the rotational progression along higher dimensions, allowing the network to distinguish token distances across 32k or 128k positions without catastrophic numerical saturation.
  • YaRN (Yet another RoPE extensioN): YaRN decomposes the hidden dimensions into three bands: high-frequency dimensions (which receive no interpolation), low-frequency dimensions (interpolated linearly), and boundary dimensions (smoothly scaled via a ramp function). Crucially, YaRN introduces an attention temperature multiplier $sqrt{t}$ that prevents attention logits from flattening, ensuring that synthetic needle-in-a-haystack retrieval accuracy remains above 98% across 16k tokens on mobile devices.

To optimize latency by reusing static context prefixes without recomputing the KV cache, review prompt caching for local LLMs.

Engineering Best Practices for Managing Context Windows on iOS

Deploying a production-grade local AI assistant on iOS and iPadOS requires programmatic context window governance. To ensure responsive generation without risk of Jetsam memory termination, adhere to these five engineering practices:

  1. Enforce Dynamic Context Caps via os_proc_available_memory(): Never assume a static context capacity. Before dispatching generation, query Darwin's Mach task API to inspect available physical headroom. If available system RAM is below 1.5 GB, dynamically restrict the maximum context window to 4,096 tokens or compress conversational history to safeguard against Jetsam events.
  2. Implement Chunked Prefill to Prevent Metal Watchdog Timeouts: Ingesting a 16,000-token prompt in a single monolithic Metal dispatch will monopolize the Apple GPU for over 1.5 seconds. The iOS SpringBoard window server enforces strict watchdog timers; a GPU pipeline stall exceeding 2.0 seconds will trigger an operating system crash. Partition long prompts into 512-token prefill chunks, yielding execution to the Metal command queue between dispatches.
  3. Standardize on Asymmetric 4-Bit KV Cache Quantization: While weight matrices require symmetrical grouping, the KV cache can be quantized dynamically to 4 bits with group sizes of 32 or 64 using MLX's quantized cache abstractions. As shown in our benchmarks, 4-bit KV quantization slashes cache RAM consumption from 896 MB down to 224 MB at 8,192 tokens, enabling 16k-token dialogues on 8 GB devices with zero Jetsam risk.
  4. Adopt Sliding-Window Attention with Attention Sinks: For continuous conversational agents, unbounded context expansion is unnecessary. Retain the initial 4 prompt tokens (the attention sinks that anchor attention distribution stability, as proven by Xiao et al.) followed by a rolling sliding window of the most recent 4,096 tokens. Discard intermediate tokens from the KV cache to establish a deterministic memory ceiling.
  5. Leverage Prefix-Aware Prompt Caching: In workflows with repetitive system prompts or static document context (such as local code completion or document search), serialize and cache the computed KV tensors for the unchanging prefix. Reusing the pre-computed KV cache eliminates the prefill compute phase, reducing time-to-first-token (TTFT) from 680 ms down to under 8 ms on the Apple A18 Pro.

The Lapis model catalogue shows compatible options before you choose a download.

How this article was prepared

This methodology benchmarks the impact of context window scaling and Key-Value (KV) cache memory allocation on Apple Silicon running Apple MLX. Measurements evaluate prompt prefill latency (ms and tok/s), autoregressive generation throughput (tok/s), anonymous dirty memory footprint, and safety margins against iOS 18 jetsam limits across A17 Pro, A18 Pro, and M4 processors, comparing MHA versus GQA architectures under 4-bit weight quantization and KV cache compression.

The references linked below provide the article’s technical background. Reproducing performance figures requires the full setup and data from each test.

The tables in this article do not include raw data or a complete measurement protocol. Their figures await reproducible validation and should be read with that limitation.

Sources and references

Local execution with Lapis

Chat with compatible models on iPhone, iPad and Mac. Download them once and use local inference offline; model size depends on your device’s resources.

App Store