Measuring Real Context Capacity for Local Inference on Low-Memory Devices

    Nominal context length doesn't equal usable context length. This article walks through a hands-on measurement on a 3.9GB, swap-less device: a ~697MB lightweight model rated for 128K context could only reliably run about 58K tokens, while pushing toward 128K would exceed the memory budget by roughly 818MiB and get the process killed. It explains how to derive per-token KV cache cost from the model architecture, measure the real slope via linear fitting of resident memory across context lengths, and map the result back to the device's actual memory budget, ending with practical deployment recommendations.

    Measuring Real Context Capacity for Local Inference on Low-Memory Devices

    TL;DR: Nominal Context Length Is Not Usable Context Length

    In local inference, context length is often treated as just another configuration knob: if the model card says 128K max, it seems you only need to set 128K in the startup arguments. On low-memory devices, that assumption doesn’t hold. A hands-on test on a device with 3.9GB of RAM and no swap showed that a lightweight model of roughly 697MB, nominally supporting a 128K context, could only run stably at about 58K tokens. Forcing it toward the nominal value pushes the KV cache about 818MiB beyond the available memory budget, and the process gets killed by the system for running out of memory.

    That 58K figure isn’t a rough guess — it’s derived from a complete chain of reasoning: first, read the model architecture to confirm how many attention layers actually store KV; second, fix the weights and thread count, vary only the context length, measure the process’s resident memory at several context settings, and take the linear slope as the per-token KV byte cost; finally, multiply the slope back to the target context and compare it against the device’s available memory, fixed overhead, and safety margin. The measured slope was about 12,339.5 bytes/token, while the theoretical value calculated from the architecture is 12,288 bytes/token — a difference of roughly 0.4%. This shows the nominal 128K isn’t made up: it genuinely comes from the model architecture. But “architecturally supported” and “actually runnable on the device” are two different things.

    1. Capacity Boundaries Are Determined by Four Components

    For local inference on low-memory devices, you can’t just look at the model file size. The model file typically represents only the weights and some metadata, while runtime memory usage includes at least:

    1. Fixed weight memory: the resident portion after model parameters are loaded;
    2. Runtime overhead: the inference engine, memory allocator, temporary buffers, thread stacks, etc.;
    3. KV cache: the portion that grows linearly with context length and concurrency;
    4. Safety margin: headroom reserved for memory fragmentation, allocation spikes, and other system processes.

    Of these, the part that actually varies with context length — and most easily overwhelms the device — is the KV cache. Determining how much context a device can handle boils down to answering two questions: after accounting for fixed weights and runtime overhead, how much memory is left for the KV cache, and how many bytes does the KV cache grow per additional token?

    2. Understand the Architecture First: Not All Layers Store KV

    The first step isn’t downloading the model — it’s reading the model configuration. In this lightweight model’s config, the total layer count is 16, but not all layers are full-attention layers: 6 are full-attention layers and the other 10 are convolution-style layers. Only full-attention layers store Key/Value caches; convolution layers produce no KV cache in the traditional sense. The config also specifies 8 KV heads, a head_dim of 64, and a maximum position encoding of 128,000.

    Calculating per-token KV bytes from the architecture:

    1. KV bytes per token =
    2. 2 (K and V)
    3. × 8 (KV heads)
    4. × 64 (head_dim)
    5. × 2 (bytes per FP16 element)
    6. × 6 (full-attention layers that actually store KV)
    7. = 12,288 bytes

    That’s roughly 12KiB per token. The KV cache for 128K tokens works out to:

    1. 12,288 × 128,000 ≈ 1,572,864,000 bytes ≈ 1.47GiB

    This is far smaller than the “tens of gigabytes for 128K context” many people imagine — precisely because only 6 of the 16 layers actually store KV, while the other 10 are convolution layers that don’t contribute to KV cache growth. That’s why you can’t estimate memory from “many layers” or “large nominal context” alone; you have to go back to the configuration structure.

    3. Then Measure the Slope: Vary Only the Context, Measure Resident Memory

    Hand calculation is only a paper value. Engineering practice requires real measurement, because memory allocation involves alignment, temporary buffers, metadata, and runtime differences. The method: fix the model weights, thread count, batch size, and concurrency; vary only the context length parameter; pick six different context settings; record the inference process’s peak resident memory after it stabilizes at each setting; then run a linear fit on “context length vs. memory usage.” The slope is the per-token KV byte cost.

    Pseudocode:

    1. for ctx in [4096, 8192, 16384, 32768, 49152, 65536]:
    2. proc = start_inference(context_length=ctx)
    3. wait_until_ready(proc)
    4. run_warmup(proc)
    5. peak_mem = sample_peak_resident_memory(proc)
    6. record(ctx, peak_mem)
    7. stop(proc)
    8. slope = linear_fit(context_lengths, peak_mems)
    9. kv_bytes_per_token = slope

    The measured slope came out to about 12,339.5 bytes/token, roughly 0.4% off the theoretical 12,288 bytes/token. An error of this magnitude is reasonable, typically coming from memory alignment, runtime buffers, and sampling noise. Multiplying this slope back to a target context gives the KV cache budget. For example, 58,474 tokens corresponds to:

    1. 58,474 × 12,339.5 ≈ 721,554,688 bytes ≈ 688MiB

    That’s roughly the upper bound of memory available for the KV cache on this device.

    4. Map It Back to the Device: The Gap Between 688MiB and 1.47GiB

    The device has 3.9GB of total memory but no swap partition. No swap means that once memory is exceeded, the system can’t “buy time” by paging out — it typically kills the process outright. Measured available memory on the device was about 2,121MB. After deducting fixed weights, runtime, and temporary buffers, the budget left for the KV cache is roughly 688MiB, corresponding to about 58K tokens.

    A 128K context, calculated with the measured slope, needs about 1.47GiB of KV cache — roughly 1,506MiB. Compared to the 688MiB KV budget, that’s an overrun of about 818MiB. This gap isn’t something you can “squeeze a bit harder” to close: on a low-memory device with no swap, exceeding the budget means the process gets killed. Therefore, a model nominally rated at 128K can only run stably at about 58K tokens on this device — not 128K.

    5. Practical Engineering Recommendations

    First, build a budget table before deployment. Split total memory into four columns — weights, runtime, KV cache, and safety margin — instead of looking only at the model file size. Second, start stress-testing the context parameter from a conservative value, increase it step by step, and record peak resident memory and error logs. Third, concurrency multiplies the KV cache, so you must account for “per-request context × concurrency” together. Fourth, if you adopt KV quantization or layer-wise offloading, re-measure the slope — don’t directly reuse FP16 figures. Fifth, keep a safety margin to avoid the process being killed by memory fragmentation or transient allocation spikes. Sixth, when reproducing this in cloud or monitored environments, collect process-level memory metrics and check system logs to confirm whether out-of-memory kills occurred.

    Conclusion

    The context capacity of local inference on low-memory devices is not the maximum position encoding on the model card — it’s the joint result of four factors: the architectural theoretical value, the measured slope, the device’s available memory, and the safety margin. In this test, only 6 of 16 layers store KV, putting per-token KV at about 12.3KB; on the 3.9GB swap-less device, after fixed overhead only about 688MiB of KV budget remains, corresponding to roughly 58K tokens; 128K would require about 1.47GiB, an overrun of roughly 818MiB, and therefore cannot run stably. Calculate the architecture first, measure the slope second, and map it back to the device last — that’s the reliable way to avoid “nominally supported, actually killed.”