freedzhost.cloud

Long context on a 16 GB card: KVMem, and the build we run

A coding agent's conversation grows to 100,000 tokens and more. A normal server keeps the attention cache of all of it on the card, and slows down as it grows. KVMem, an open-source extension of llama.cpp, keeps the history in system RAM and brings back to the card only the blocks relevant to the current step. We measured it against plain llama.cpp on our own cards, then built our own version for our machine.

How these figures were obtained. Measured on our own machines (see the protocol), usually a single run, with speeds as reported by the server itself; we do not publish raw logs. Treat them as orders of magnitude on this hardware, not as guarantees on yours. Figures from outside are labelled with their source.

What you get. Measured here: 48 recall checks out of 48 at 44k and 130k tokens; at 103k tokens, 150.9 tokens per second against 132.1 for plain llama.cpp; 4.9 GB of VRAM freed. And a 177B mixture-of-experts model served from a single 16 GB card.

1. The idea

Finished blocks of the attention cache go to system RAM. At each step, KVMem picks the blocks that matter for the current question and places them, in order, in a bounded working set on the card. The paper behind it reports that a 32k resident window is essentially lossless against the full 256k history.

llama-kvmem-server -m model.gguf -c 262144 --kvmem-budget 8192 \
  --kvmem-gen-reserve 16384 --spec-type draft-mtp

One generation cannot exceed --kvmem-gen-reserve tokens, thinking included. Size it for your longest answer.

2. Measure before switching

Our rule: KVMem for agents (long contexts), plain llama.cpp for chat and short calls. Two presets, no automatic switch.

3. What our build changes, and why

Traps

Skip scripts/apply-patches.sh after the submodule update and the build fails deep inside, on missing members, which looks like a wrong submodule version. It is not. Running it twice is safe.

A comment placed between two lines of a \ continuation silently cuts the command: our CMake call lost its CUDA flags and bash -n said the file was fine. Only running the script caught it; the test now checks the arguments CMake really receives.

Some model families are not supported (sliding-window attention, for instance): they stay on plain llama.cpp.

Sources

Found this useful? Tell us what to measure next.