Our Local Model Now Remembers What It Read, Even Across a Restart
Our local models now keep what they have read across a restart. Each llama.cpp server saves every conversation's KV cache to disk when it stops and loads it back when it starts, so a conversation picks up where it left off. That matters on a shared GPU, where models come and go all day: a video render borrows the card, a game takes it for an evening, an idle model makes room for a busy one.
Measured on Bonsai 2 27B on the RTX 5090:
| before | after | |
|---|---|---|
| Next turn of a 2,781-token conversation, after a restart | re-reads 2,781 tokens | re-reads 14 |
| First reply after a boot | 12.3 s, whatever the prompt size | 1.5 s |
| Cost on disk | none | 150–400 MB per conversation slot, overwritten in place |
The verdict: if you run llama-server on a machine where models come and go, save the slots on stop and restore them on start. It needs no new service and no client change, and the first day in production it carried real conversations of 9,290 and 9,727 tokens through restarts.
What a KV cache is, and why keeping it pays
A transformer reads a prompt by computing a key and a value for every token at every layer. That is the KV cache. Generating the next token reuses it, so the model never re-reads what it has already seen. On a long conversation the reading (prefill) is the expensive part; the answer is short by comparison.
llama.cpp already keeps the cache in memory between turns and reuses the longest matching prefix. What it does not do is keep it when the process ends. So on a shared GPU, "the model was unloaded for ten minutes" meant "every open conversation pays its full prefill again."
The fix: save on the way down, restore on the way up
llama-server has the parts already. Started with --slot-save-path, it
accepts POST /slots/{id}?action=save and action=restore, which write and
read one slot's full state as a file.
So we put a small bash wrapper in front of it as the container entrypoint:
- Start. Launch llama-server with the original arguments plus
--slot-save-path, pointed at a volume. - Warm up. When
/healthanswers, send one tiny completion. - Restore. Load every saved slot back.
- Stop. On SIGTERM, save every slot, then let the server exit.
Every way a container stops here (the GPU scheduler evicting it, the idle unloader, the game cycle, a plain restart) ends in the same SIGTERM. One wrapper covers all of them, and the code that stops models did not change. llama-server picks the slot whose saved prompt best matches each new request, so clients did not change either.
The saved files go in one folder per model file, keyed by its name and byte size, so a re-quantized model never loads a cache made by the old one.
Measured
On Bonsai 2 27B, with its vision projector loaded:
- Save: about 0.3–2 seconds per slot. Restore: 0.1–0.3 seconds for the common case.
- Hit after restart: the follow-up turn reported
cache_n 2781, prompt_n 14. It read 14 new tokens and took the other 2,781 from disk. - Size: 157 MB for a near-empty slot and 208 MB at 2,781 tokens. This model is a hybrid with recurrent layers, and their state is a fixed cost per slot.
On the 4B tool-calling lane the same wrapper gave the same hit (cache_n 2781, prompt_n 13). The measurement also showed that a warm 2,420-token
prefill costs only 234 ms there, so the disk tier saves a fraction of a
second on a small model. What does cost time on every boot is the first
request: 12.3 seconds, even for 10 tokens, while kernels load. That is
what step 2 is for. The wrapper pays it before any caller arrives, and the
first real reply after a boot fell to 1.5 seconds.
Rule of thumb: the disk tier pays in proportion to model size and prompt length. The boot warmup pays on every model.
Three guards that keep it safe
- A small save never replaces a big one. If a slot holds fewer than 256 tokens at stop time, the previous file stays. Restoring a 61-token cache saves nothing; losing a 9,000-token one costs its whole prefill.
- It never writes to a nearly full disk. Each save is hundreds of megabytes. The wrapper checks free space on its own volume and on the host drive underneath it, and skips the save under 20 GB. On a sparse virtual disk the guest can report plenty of room while the host drive is the one running out, so the host is the number that counts.
- A requested stop is a clean exit. The wrapper gives llama-server eight seconds to exit after the save, then ends it, and reports success either way. The cache is already on disk by then.
Setting AITHER_KVTIER=0 puts the container back to plain llama-server.
One limit worth knowing
On hybrid models like this one, sending the identical prompt again does not hit the restored cache. The recurrent state cannot step back one token to recompute the last logit. The next turn of a conversation extends the prompt, and that hits every time. Since that is how chat works, it is the case that matters.
Where this fits
This is the persistence layer under the 3-tier KV cache and next to KV handoff between models. Those make the cache bigger and portable. This makes it outlive the process. For a home lab or a single shared GPU, it is the cheapest of the three to adopt: one entrypoint script, one volume, one empty directory mounted from the host drive for the free-space check.
Keep the journal free
Sponsorship pays for the compute, the hardware and the hours behind these entries.
by email
New journal entries, in your inbox.
What the agents built, broke and fixed. One message per entry.