Context size in KoboldCpp: memory, ContextShift and the KV cache

Context size is how many tokens the model can see at once. KoboldCpp's default is 16384 and it reserves 256 extra. Every token of context costs memory in the KV cache: in our tests a tiny model needed 51 MiB at 4K and 1539 MiB at 128K, and real 8B to 14B models need much more, so use the calculator below. A larger context does not slow generation for short prompts, but long prompts take longer to process. Set it no higher than the model's training length and what fits in your memory, and use the same value in KoboldAI Lite or SillyTavern.

Checked against KoboldCpp 1.124.2 · updated 13 Nov 2026
  • lab-lnx: Linux test VM · 4 vCPU · 15 GB · no GPU
  • sw-vulkan: Software Vulkan (Mesa lavapipe) · reports exact VRAM allocations, speeds meaningless
How we test
On this page
  1. What context size means
  2. How much memory context needs
  3. Try your own model
  4. Does a bigger context make it slower?
  5. Choosing your KoboldCpp context size
  6. Set the same context in Lite and SillyTavern
  7. ContextShift and fast-forward: why long chats do not reprocess
  8. SmartCache: switch chats without reprocessing
  9. Quantized KV cache (—quantkv)
  10. Sliding-window models (Gemma and others)
  11. Going past the training length: RoPE

What context size means

A model reads and writes tokens, pieces of words. The context size is the number of tokens it can hold in view at once: everything you send plus the reply it writes. When a chat is longer than that, the oldest text no longer fits and is cut.

Tokens are not words, and the ratio depends on the text and the model. With the Qwen2.5 tokenizer, a simple story sentence came to 1.07 tokens per word, and technical text with numbers and names came to 2.20 tokens per word F68. So there is no single rule of thumb.

Everything in the prompt uses the window. In KoboldAI Lite the order is: Memory, then World Info entries that matched, then the story, with the Author’s Note added near the end. What is left over is the room for the reply (Lite).

  1. Memory and World Infoalways kept; they come first
  2. Story or chat so farthe oldest part is cut first when the window is full
  3. Author's Noteadded near the end
  4. Room for the replythe reply length counts against the same window

How much memory context needs

The model keeps a KV cache: for each token it has read, two values (a key and a value) for each attention layer. In words:

2 (keys and values) × layers × context tokens × KV heads × head size × bytes per value

KoboldCpp allocates the context you ask for plus 256 tokens F06:

Requested context and allocated cells (n_ctx in the log)
You ask for (--contextsize)KoboldCpp allocates (n_ctx)
4,0964,352
6,1446,400
16,38416,640
32,76833,024
131,072131,328

A worked example from our logs: Qwen2.5-0.5B has 24 layers and 2 KV heads of 64 values. At 16384 requested that is 16640 cells, and the log prints the line below, which equals the formula exactly F10:

KV cache line, 16384 requestedR-run1-lnx-nocuda-ctx-16384 · lab-lnx · KoboldCpp 1.124.2 (F10)
llama_context: n_ctx                 = 166401llama_kv_cache: size =  195.00 MiB ( 16640 cells,  24 layers,  1/1 seqs), K (f16):   97.50 MiB, V (f16):   97.50 MiB2
  1. llama_context: n_ctx = 16640: 16384 + 256 (F06).
  2. llama_kv_cache: size = 195.00 MiB ( 16640 cells, 24 layers, 1/1 seq: 2 × 24 × 16640 × 2 × 64 × 2 bytes = 195 MiB.
Qwen2.5-0.5B KV cache at three context sizes: logged by KoboldCpp and calculated
Context you ask forCells allocated (+256)Logged KV buffer sizeCalculated
4,0964,35251.00 MiB51.00 MiB
16,38416,640195.00 MiB195.00 MiB
131,072131,3281539.00 MiB1539.00 MiB
32,768, KV q4_033,024108.84 MiB108.84 MiB

For real models the cache is much bigger, because they have more layers and more KV heads. These sizes are calculated by the same code as the calculator; Llama 3.1 8B matches the sizes KoboldCpp logged (1,056 MiB at 8K, 4,128 MiB at 32K) F36, and the others are calculated from their file headers.

KV cache in GB at five context sizes, f16, calculated
Model8K16K32K64K128K
Llama 3.1 8B Instruct
32 layers, 8 KV heads × 128
1.03 GB
q8_0: 561 MB
2.03 GB
q8_0: 1.08 GB
4.03 GB
q8_0: 2.14 GB
8.03 GB
q8_0: 4.27 GB
16.0 GB
q8_0: 8.52 GB
Mistral Nemo Instruct 12B
40 layers, 8 KV heads × 128
1.29 GB
q8_0: 701 MB
2.54 GB
q8_0: 1.35 GB
5.04 GB
q8_0: 2.68 GB
10.0 GB
q8_0: 5.33 GB
20.0 GB
q8_0: 10.6 GB
Qwen2.5 14B Instruct
48 layers, 8 KV heads × 128
1.55 GB
q8_0: 842 MB
3.05 GB
q8_0: 1.62 GB
6.05 GB
q8_0: 3.21 GB
12.0 GB
q8_0: 6.40 GB
24.0 GB
q8_0: 12.8 GB
Gemma 2 9B It
42 layers, 8 KV heads × 256, sliding window
2.11 GB
q8_0: 1.12 GB
3.42 GB
q8_0: 1.82 GB
6.05 GB
q8_0: 3.21 GB
11.3 GB
q8_0: 6.00 GB
21.8 GB
q8_0: 11.6 GB
Qwen3 30B-A3B (MoE)
48 layers, 4 KV heads × 128
792 MB
q8_0: 421 MB
1.52 GB
q8_0: 829 MB
3.02 GB
q8_0: 1.61 GB
6.02 GB
q8_0: 3.20 GB
12.0 GB
q8_0: 6.39 GB

On the CPU, the cache lives in system RAM, so context raises RAM use. On our small test model, peak memory rose from 616 MB at 2K to 2,123 MB at 128K F11:

Peak RAM by context sizelab-lnx (Linux, 4 vCPU), 0.5B Q4_K_M model, KoboldCpp 1.124.2, 13 Nov 2026CPU lab, 0.5B model: relative differences only
  • 2K615.7 MB
  • 8K688.3 MB
  • 32K975.6 MB
  • 128K2122.7 MB
Data
ItemValue (MB)
2K615.7
8K688.3
32K975.6
128K2122.7

Download CSV · Results: R-run1-lnx-nocuda-ctx-2048, R-run1-lnx-nocuda-ctx-8192, R-run1-lnx-nocuda-ctx-32768, R-run1-lnx-nocuda-ctx-131072-q4

These are lab numbers for a tiny model and do not carry over to others; the calculator does the arithmetic for yours.

Try your own model

Choose a model and move the context slider. The result shows the KV cache and whether everything still fits.

1. Model

Layers, KV heads and tensor sizes were read from the real file headers on Hugging Face. File size shown is the download size.

2. Your hardware
Reserve and safety margin

Defaults: 512 MB for the desktop on Windows and Linux (0 if the card drives no display), and KoboldCpp's autofit padding of 1024 MB. What this assumes

3. Context and cache

Llama 3.1 8B Instruct Q4_K_M · 16K context · KV f16

Fits completely

All 32 layers fit on the GPU, with the context and buffers.

calculated from the model's tensor sizes; compute buffers are estimates calibrated on two measured models (F36, F42).

GPU memory 6.06 GB of 10.5 GB usable of 12 GB
010.5 GB usable of 12 GB
  • Model weights 3.90 GB
  • KV cache 2.03 GB
  • Compute buffer 132 MB
  • Free 4.44 GB
System RAM 959 MB of 29.0 GB usable of 32 GB
029.0 GB usable of 32 GB
  • Weights on CPU 693 MB
  • Host compute buffer 267 MB
  • Free 28.1 GB

Launch values

  • --gpulayers 33
  • --contextsize 16384

KoboldCpp counts one extra layer for the output layer, so --gpulayers 33 puts all 32 repeating layers on the GPU (F65). Leaving --gpulayers on auto (-1) lets autofit choose; it keeps a 1 GB padding and may offload fewer layers than this calculation. GPU layers guide

koboldcpp --model Meta-Llama-3.1-8B-Instruct-Q4_K_M.gguf --gpulayers 33 --contextsize 16384

Weights 4.58 GB · 32 layers · 8 KV heads × 128 · KV cache 2.03 GB

Does a bigger context make it slower?

Not for short prompts. On our CPU test, generation speed did not fall as the context grew F11:

Generation speed by context sizelab-lnx, 0.5B Q4_K_M, short prompt, KoboldCpp 1.124.2, 13 Nov 2026CPU lab, 0.5B model: relative differences only
  • 2K18.1 tok/s
  • 8K18.6 tok/s
  • 32K21.6 tok/s
  • 128K22.1 tok/s
Data
ItemValue (tok/s)
2K18.1
8K18.6
32K21.6
128K22.1

Download CSV · Results: R-run1-lnx-nocuda-ctx-2048, R-run1-lnx-nocuda-ctx-8192, R-run1-lnx-nocuda-ctx-32768, R-run1-lnx-nocuda-ctx-131072-q4

The differences are run-to-run noise on a shared machine, not a speed-up. What costs time is a long prompt, which must be read before the first word of the reply, and reprocessing a whole chat when the cache cannot be reused (below). We have not measured the same on a GPU yet.

Choosing your KoboldCpp context size

In this order:

  1. Not above the model’s training length. The log prints it as n_ctx_train. If you ask for less, KoboldCpp says so with a harmless line: n_ctx_seq (2304) < n_ctx_train (32768) -- the full capacity of the model will not be utilized. That only means you chose a smaller window.
  2. Small enough that all layers still fit on the GPU: GPU layers. Lower the context before lowering layers.
  3. The same number in your client, see below.

The range KoboldCpp accepts is 256 to 524288. Outside it, it exits at once with error: argument --contextsize/--ctx-size/-c: must be within [256, 524288] and loads nothing F22. The default has moved several times, which is why old guides disagree: 2048 (1.20), 4096 (1.70), 8192 (1.96), 16384 (1.115), 12288 (1.115.2) and 16384 again (1.122). shows the current value.

Our guidance, not a measured rule:

UseTypical context
Short chat with a character4096 to 8192
Long roleplay or story8192 to 16384
Documents, summaries, code16384 and up, as far as your memory allows

Set the same context in Lite and SillyTavern

KoboldAI Lite and SillyTavern each have their own maximum context and maximum output settings. KoboldCpp does not set them for you, and they can differ. We saw that Lite sends its own context size, not the server’s F61. A client set higher than the server gets its prompt trimmed by the server; one set lower leaves memory unused. Set both to the same number. In Lite the field is in the settings; for SillyTavern see the SillyTavern guide when it is published.

ContextShift and fast-forward: why long chats do not reprocess

Two features keep a long chat fast:

  • Fast-forward reuses the start of the prompt that is unchanged since the last turn, so KoboldCpp only reads the new text.
  • ContextShift (since 1.48, on by default) handles a full window: instead of reprocessing, it drops the oldest tokens from the cache and slides the rest along.

Both need the beginning of the prompt to stay the same. turns off ContextShift. turns off both. The launcher has them on the Context tab.

We ran a scripted 32-turn chat at 2048 context and counted the tokens KoboldCpp processed each turn, from its own log F66. The window fills at turn 20.

Tokens processed per turn in a 32-turn chat, context 2048
Default (ContextShift on)43232323232323749
--noshift432323194619401946194325,729
--nofastforward439531871196719671967196743,714
Memory edited at turn 2143232323194623232,672
--quantkv q8_043232323232323749
Prompt tokens processed over the whole chatlab-lnx (CPU), 0.5B Q4_K_M, context 2048, 32 turns, KoboldCpp 1.124.2, 14 Nov 2026CPU lab, 0.5B model: relative differences only
  • Default749.0 tokens
  • Memory edited once2672.0 tokens
  • --noshift25729.0 tokens
  • --nofastforward43714.0 tokens
Data
ItemValue (tokens)
Default749.0
Memory edited once2672.0
--noshift25729.0
--nofastforward43714.0

Download CSV · Results: R-r2-lnx-ctx-default, R-r2-lnx-ctx-memory-edit, R-r2-lnx-ctx-noshift, R-r2-lnx-ctx-nofastforward

With the defaults, a turn reads only the 23 new tokens. The log shows what happens when the window is full:

ContextShift at worktest-data/r2-lnx-2026-11-14/ctx/default.log · lab-lnx · KoboldCpp 1.124.2 (F66)
[Context Shifting: Erased 102 tokens at position 22]1
  1. [Context Shifting: Erased 102 tokens at position 22]: The oldest turn is dropped from the cache; nothing is recomputed. Position 22 is just after the Memory.

Notice that the Memory edit cost one full reprocess, then shifting resumed. Anything that changes the start of the prompt does that. These make a chat reprocess, and what avoids them:

  • World Info or lorebook entries inserted near the top. Entries that appear and disappear change the early prompt. Insert them lower, or keep the set of active entries stable.
  • Random or time-based text (macros, a clock in the prompt) near the top. Move it to the end or remove it.
  • Editing an old message. Everything after the edit is new to the cache. Expected: the cost is one reprocess.
  • Changing Memory. One full reprocess, as measured above.
  • A sliding-window model such as Gemma: it cannot shift at all, see below.
  • ContextShift or fast-forward turned off: or . Remove the flag.
  • Switching between chats with different starts: see SmartCache below.

SmartCache: switch chats without reprocessing

With two chats open, each turn replaces the cache with the other chat, so every switch reprocesses everything. (since 1.104) keeps snapshots of the cache in RAM so returning to a chat is quick. It needs fast-forward. We alternated two chats for 8 turns each F66:

Two alternating chats, 8 turns each, context 2048
No SmartCache48, 24, then 125, 125, 226, 226 … 731, 7316,064693
--smartcache 248, 24, then 22 every turn380726

In the log, each return to a chat prints SmartCache Match of 1.00 restored from slot 0. Two slots cost about 33 MB more RAM with chats of up to about 800 tokens; a slot holds a cache snapshot, so the cost grows with the chat. The older is superseded by ContextShift and not recommended.

Quantized KV cache (—quantkv)

stores the cache at lower precision: f16 (the default), bf16, q8_0, q5_1 or q4_0. Named values work since 1.112, and the old numbers 0 to 3 are still accepted. It needs flash attention, which is on by default F09; without it only the K cache is quantised.

On our small model, at 32K of context, peak RAM went from 976 MB (f16) to 794 MB (q8_0) and 698 MB (q4_0), and the cache itself was 108.8 MiB with q4_0 F10. In the calculator above, q8_0 is about half and q4_0 about a quarter of the f16 size.

Does it break ContextShift? Different sources disagree, so we tested: on 1.124.2, on the CPU, with --quantkv q8_0, shifting worked exactly as with the default (23 tokens a turn once full, Context Shifting lines in the log) F66. We did not test a GPU, and we have not measured whether answers get worse.

To undo it, use --quantkv f16 or remove the flag.

Sliding-window models (Gemma and others)

Models such as Gemma use sliding-window attention (SWA): most layers only look at the last few hundred tokens. KoboldCpp turns SWA on automatically (since 1.114), which makes the cache much smaller, and the price is that ContextShift is disabled F67:

Gemma 3 270M: SWA ontest-data/r2-lnx-2026-11-14/ctx/gemma270m-default.log · lab-lnx · KoboldCpp 1.124.2 (F67)
SWA Mode is ENABLED!1Context shifting is DISABLED!2llama_kv_cache_iswa: creating non-SWA KV cache, size = 2304 cells3llama_kv_cache_iswa: creating     SWA KV cache, size = 1152 cells4
  1. SWA Mode is ENABLED!: Chosen automatically for this model.
  2. Context shifting is DISABLED!: Long chats reprocess instead of shifting.
  3. llama_kv_cache_iswa: creating non-SWA KV cache, size = 2304 cells: The three full-attention layers keep the whole context.
  4. llama_kv_cache_iswa: creating SWA KV cache, size = 1152 cells: The 15 window layers keep only window + batch + 128 cells.

In the same 32-turn chat, the SWA model processed 1,967 tokens every turn once the window was full, and with it processed 24, at a cost of memory: peak RAM 499 MB with SWA and 629 MB without, at only 2048 context. The window cache size follows the batch size: 896, 1152 and 1664 cells at batches of 256, 512 and 1024, and it does not grow with the context. adds padding that controls how far you can rewind before a forced reprocess. Pick --noswa only if you need shifting more than you need the memory. This is one small model; the calculator uses the same rule for others, and says so.

Going past the training length: RoPE

Models can be stretched past the context they were trained on by scaling the position encoding (RoPE). KoboldCpp does this automatically from the model’s trained context. You can set it by hand with (a frequency scale, then a base), or tell it the model’s native length with . We have not tested how well models hold up past their training length (the needle-in-a-haystack runs are not done), so we publish no advice beyond this one: leave it automatic unless a model’s card says otherwise, and treat anything above the training length as lower quality.

Frequently asked questions

What is the best context size for KoboldCpp?

The largest one that fits in your memory with all layers on the GPU and that your model was trained for. For chat 8192 to 16384 is plenty for most people; KoboldCpp's default is 16384. A bigger number costs memory, not speed, and a number above the model's training length can make answers worse. Use the calculator to see what your card can hold.

What is the maximum context size?

KoboldCpp accepts 256 to 524288. Outside that range it exits with an error before loading. The useful maximum is lower: the model's training length, which is printed as n_ctx_train in the log, and the memory you have.

Does a bigger context make KoboldCpp slower?

Not for short prompts. On our CPU test, generation speed was 18.1 tokens per second at 2K context and 22.1 at 128K. What takes time is processing a long prompt, and reprocessing a whole chat when ContextShift or fast-forward cannot reuse the start of it.

Why does KoboldCpp reprocess my whole chat every turn?

Something near the start of the prompt changes each turn, or ContextShift is off. In our test a full chat processed about 23 tokens a turn with ContextShift and about 1,946 with --noshift. A changed Memory costs one full reprocess. A sliding-window model such as Gemma cannot shift at all, unless you start it with --noswa.

Should I quantize the KV cache?

Only when memory is the problem. --quantkv q8_0 roughly halves the cache and q4_0 cuts it to about a quarter. On our CPU test with ContextShift it behaved exactly like the default. Users report crashes on some GPUs when the context fills, so test it with a long chat before you rely on it.

Why is the context bigger than the number I set?

KoboldCpp adds 256 tokens to what you ask for. A request for 4096 shows n_ctx = 4352 in the log. The extra room is used up by the cache, so memory figures are slightly larger than a simple calculation.

What is the KV cache?

It is the memory of the conversation so far. For every token the model has read it stores two values (a key and a value) per layer, so it does not have to recompute them for each new word. It grows linearly with the context size.