LocalLLM Forum — Run it locally. Own your models.

Prompt caching with llama.cpp: real-world TTFT gains

E posted by ElifKowalski #prompt-caching#ttft#llama-cpp 3 posts 389 views
E

I run a RAG assistant over a few thousand internal documents, and the system prompt plus retrieved chunks meant every request was re-processing 4-6k tokens before the model wrote a single word. Prompt caching fixed that. Numbers from my 4070 Ti Super with an 8B Q5_K_M:

scenario                       prompt tokens   TTFT (cold)   TTFT (cached)
system prompt only                       842        1.9 s          0.21 s
system + 3 retrieved chunks             5100        8.7 s          0.38 s
system + full document                  12400       19.4 s          0.61 s

The config that got me there:

./llama-server -m model-q5_k_m.gguf -c 16384 --cache-reuse 256 --slots 1

Notes that took me too long to figure out:

  • --cache-reuse 256 lets the server reuse KV chunks even when the prompt shifted — essential for RAG, where retrieved chunks land in different positions between turns.
  • Caching is per-slot. With --slots 4 and rotating users you will get far fewer hits unless requests happen to land on the same slot.
  • The cached TTFT is not free accuracy-wise: shifted-chunk reuse is exact, but check your cache_reuse granularity against how much your prompt actually varies.

From 8.7s to 0.38s on the common path is the difference between "this is broken" and "this is fine" for interactive use.

RTX 4070 Ti Super 16GB · Poznań · fine-tuning on weekends
K
E ElifKowalski replied
…cache-reuse 256 lets the server reuse KV chunks even when the prompt shifted…

Good write-up. One caveat I hit with --cache-reuse on longer contexts: the chunk-reuse matching is greedy, so if your retrieved chunks are similar to each other (near-duplicate passages from the same document), the server can reuse a KV block from the wrong position and quality degrades subtly rather than failing loudly. I now hash my chunks and drop near-dups at retrieval time — which I should have been doing anyway.

Also worth measuring: with -fa on and a q8_0 V-cache, the memory cost of keeping several slots' KV caches warm is much lower than the old f16 numbers suggest. I keep 4 warm slots in 11GB of headroom on the 3090 box.

2× RTX 3090 · Fedora · llama.cpp nightly
D
K Kasimir replied
…the chunk-reuse matching is greedy…

The per-slot point is the one that bites multi-user setups. On my 2× 3090 home server I run --slots 3 and family members hit it at random times; my cache hit rate went from about 70% with one slot to under 20% with three, because everyone's session lands on a different slot each turn.

Partial fix: pin each client to a slot by keeping the connection alive and using the id_slot parameter on the completion request. Crude, but it got my hit rate back to ~60%. The proper fix upstream is LRU slot selection with cache-affinity awareness — there is an open PR, go thumbs it up.

2× 3090, P40 hoarder. The cluster grows.
Want to join the discussion? Register or log in to reply. Register Log in
Reply
Ctrl+Enter to submit
post 1 of 3