← All guides

Check KV Cache Size in llama.cpp and Ollama: Read the Log

Our VRAM pages calculate the KV cache from a model's config. This page shows how to read what your runtime actually reserved. In llama.cpp, one log line starting with llama_kv_cache: size = gives the total in MiB, the number of cells and the layer count. Ollama 0.34 runs llama.cpp's own server underneath, so its server log shows the same line. We read both projects' source on 2026-09-12 for every line and flag below.

Bottom Line

  • The number is in the startup log. Look for llama_kv_cache: size = … MiB (N cells, L layers, …). The cell count is the context the cache holds.
  • Ollama shows the same line. Ollama 0.34.0 runs llama.cpp’s llama-server underneath and logs at verbosity 4.
  • llama-server may shrink your context without a visible message. --fit is on by default. Its “context size reduced” line prints only at -lv 4.
  • llama-server defaults to 4 slots. n_ctx is a pool shared by 4 conversations. Set -np 1 for one user.
  • Sliding-window models print two caches. gpt-oss shows a full cache and a small SWA cache. --swa-full doubles the total.
  • Ollama multiplies context by parallel slots. OLLAMA_NUM_PARALLEL=4 at 32K sends -c 128000.

The Lines to Read in llama.cpp

We read these format strings from llama.cpp master on 2026-09-12. Your build prints the same fields if it is recent.

Log line starts withWhat it tells youSource file
llama_context: n_ctx =Total context the runtime reservedsrc/llama-context.cpp
llama_context: n_ctx_seq =Context per sequence (per slot)src/llama-context.cpp
llama_kv_cache: size =KV total in MiB, cells, layers, seqs, K and V sizes and typessrc/llama-kv-cache.cpp
llama_kv_cache: CUDA0 KV buffer size =KV memory on each devicesrc/llama-kv-cache.cpp
llama_kv_cache_iswa: creating non-SWA KV cache, size =Cells in the full-attention cachesrc/llama-kv-cache-iswa.cpp
llama_kv_cache_iswa: creating SWA KV cache, size =Cells in the sliding-window cachesrc/llama-kv-cache-iswa.cpp
llama_context: CUDA0 compute buffer size =Scratch memory, separate from the KV cachesrc/llama-context.cpp

The full size = line has this shape:

llama_kv_cache: size = <MiB> MiB (<cells> cells, <layers> layers, <seqs>/<streams> seqs), K (<type>): <MiB> MiB, V (<type>): <MiB> MiB

Check three fields against your plan:

  1. cells — is it the context you asked for?
  2. K and V type — is it f16, or the q8_0 you set with -ctk and -ctv?
  3. device — does the KV buffer size line say CUDA0, or CPU?

A CPU KV buffer on a GPU machine means the cache lives in system RAM. That is a speed problem even when the model fits.

The Exit Table: llama-server’s Memory Breakdown

When llama-server stops, it prints a table headed memory breakdown [MiB]. The columns are total, free, self, model, context and compute, plus unaccounted. The row for each device reads self = model + context + compute + unaccounted.

The context column holds the KV cache for that device. It is the fastest way to see the split across two GPUs.

Trap 1: --fit Shrinks the Context Quietly

--fit defaults to on. It adjusts arguments you did not set so the model fits free device memory. -c defaults to 0, which means “use the model’s trained context.”

So if you omit -c on a 262K-context model, --fit can cut the context until the model fits. The floor is --fit-ctx, default 4096. The line that reports the cut is:

context size reduced from <N> to <M> -> need <X> MiB less memory in total

That line uses LOG_TRC, which is verbosity level 4. The default verbosity is 3. At default settings the cut happens and the line does not print.

Three ways to see or stop it:

Do thisResult
Read llama_context: n_ctx =Shows the context you actually got
Start with -lv 4Prints the --fit decisions, including the cut
Set -c 32768 (or your value)--fit logs “context size set by user … no change” and leaves it alone

If you set -c and the model does not fit, --fit changes layer placement instead. Watch for layers moving to the CPU.

Trap 2: llama-server Reserves Context for 4 Users

llama-server’s --parallel default is -1, meaning auto. The server source sets auto to 4 slots with a unified KV cache.

With a unified cache, n_ctx is one pool that the 4 slots share. n_ctx_seq is what one conversation can use. If you run one chat client, start with -np 1.

This matters most for the sliding-window cache, because its size scales with the slot count. See the next section.

Trap 3: Sliding-Window Models Have Two Caches

gpt-oss alternates full-attention and 128-token sliding-window layers: 18 of each across 36 layers. llama.cpp builds two caches for it.

The source sizes the SWA cache as the window times the sequences, plus the micro-batch, rounded up to a multiple of 256. It is capped at the full context:

size_swa = pad_256( min(n_ctx, n_swa × n_seq + n_ubatch) )

With the default n_ubatch of 512, that gives:

SetupSWA cells
One sequence (-np 1)128 + 512 = 640 → 768
llama-server default (4 slots, unified)512 + 512 = 1,024
--swa-fullsame as the full cache

What the arithmetic predicts for gpt-oss 120B at 131,072 cells, f16 cache. This is our calculation from the model’s config.json (8 KV heads, head dim 64), not a captured log. Each layer costs 2 KiB per cell.

CacheCellsLayersPredicted size
Full-attention131,072184,608 MiB
SWA, 4-slot default1,0241836 MiB
SWA with --swa-full131,072184,608 MiB

So a normal start should print about 4.6 GiB of KV cache. If your log shows about 9.0 GiB, a full-size SWA cache is on. llama.cpp prints a warning when that happens: using full-size SWA cache. The memory side of this model is covered in how much VRAM gpt-oss 120B needs at full context.

Ollama: the Same Lines, One More Multiplier

We read Ollama’s source at release v0.34.0 (published 2026-09-05). Its scheduler starts llm.NewLlamaServer, which runs llama.cpp’s upstream llama-server binary. The launch flags include:

-c <context × parallel>  -np <parallel>  --log-verbosity 4

Two consequences follow from that.

  1. The llama_kv_cache lines appear in Ollama’s server log. Verbosity 4 also prints the --fit trace lines.
  2. The cache holds context × OLLAMA_NUM_PARALLEL. The default parallel count is 1. At 32K and a parallel count of 4, the cache holds 128,000 cells.

Ollama always passes -c, so --fit does not cut the context. It changes layer placement instead.

Step 1 — check the context and the CPU split:

ollama ps

The columns are NAME, ID, SIZE, PROCESSOR, CONTEXT and UNTIL. CONTEXT is per request. Anything other than 100% GPU under PROCESSOR means part of the model runs on the CPU.

Step 2 — read the log:

SystemCommand
macOScat ~/.ollama/logs/server.log
Linux (systemd)journalctl -u ollama --no-pager --follow --pager-end
Dockerdocker logs <container-name>
Windows%LOCALAPPDATA%\Ollama\server.log

Step 3 — know the default context. Ollama’s docs set it by VRAM: 4K below 24 GiB, 32K from 24 to 48 GiB, 256K at 48 GiB and above. A 16GB card starts at 4K. Agent and coding tools need far more; Ollama’s docs recommend at least 64,000 tokens. Set it with OLLAMA_CONTEXT_LENGTH=64000 ollama serve.

Ollama forces one slot for some model families: mllama, qwen3vl, qwen3vlmoe, qwen35, qwen35moe, qwen3next, lfm2, lfm2moe, nemotron_h, nemotron_h_moe and nemotron_h_omni. For those, OLLAMA_NUM_PARALLEL does not multiply the cache.

When the Number Is Too Big

The log showsFix
More cells than you needSet -c or OLLAMA_CONTEXT_LENGTH lower
4 slots on a single-user box-np 1, or unset OLLAMA_NUM_PARALLEL
f16 K and V-ctk q8_0 -ctv q8_0, or OLLAMA_KV_CACHE_TYPE=q8_0; see KV cache quantization
A SWA cache the size of the full cacheRemove --swa-full
CPU KV buffer on a GPU machineLess context, or a card with more VRAM; see how much VRAM for 128K context

If the cache is right and still does not fit, the model needs more memory than the card has. The 16GB VRAM picks and the lowest-KV-cache models are the next pages to read.

FAQ

How do I see how much KV cache llama.cpp allocated?

Read the startup log. The line that starts with llama_kv_cache: size = prints the total in MiB, the number of cells, the number of layers, the sequence count, and the K and V sizes with their data types. A separate line per device prints the KV buffer size, for example CUDA0 KV buffer size. A model with sliding-window attention prints two of these, one per cache. The cell count is the context the cache holds.

How do I check the KV cache in Ollama?

Run ollama ps for the CONTEXT column, then read the server log for the llama_kv_cache lines. Ollama 0.34.0 starts llama.cpp's llama-server as its runner and passes log verbosity 4, so those lines appear in the log. On macOS the log is ~/.ollama/logs/server.log. On Linux with systemd, run journalctl -u ollama --no-pager --follow --pager-end.

Why is my llama-server context smaller than the model supports?

The --fit option is on by default. If you do not set -c, llama-server starts from the model's trained context and may reduce it to fit free memory, down to a floor of 4096 tokens. The line that reports the reduction is logged at trace level, which the default verbosity of 3 hides. Read the n_ctx line, run with -lv 4, or set -c yourself.

Why does llama-server allocate context for 4 users?

Its --parallel default is -1, meaning auto, and auto sets 4 slots with a unified KV cache. The n_ctx line is the size of the shared pool and n_ctx_seq is the context per slot. For a single user, set -np 1 so the whole pool serves one conversation.

Why does Ollama use more KV cache when OLLAMA_NUM_PARALLEL is set?

Ollama passes -c as the context length multiplied by the parallel count. At a 32K context and OLLAMA_NUM_PARALLEL=4, llama-server receives -c 128000 and sizes the cache for 128,000 tokens. The CONTEXT column in ollama ps still shows the per-request context. Ollama forces one slot for some model families, including qwen35 and nemotron_h.

See Also

Need OpenClaw fixed live?

Remote rescue sessions for gateway, auth, tunnel, VPS, and model access problems.

See Rescue Session

Read next

Lowest KV Cache Local LLM (2026): gpt-oss 20B Wins
A league table of current local models ranked by KV cache per 128K context, computed from their published config.json files. gpt-oss 20B costs 3.0 GiB. Qwen3-8B costs 18 GiB. A 21B model is six times cheaper on cache than an 8B one, and no model card says so.
How Much VRAM Does gpt-oss 120B Need at Full 128K Context? About 4.5 GiB of Cache, Not 9
gpt-oss 120B holds its entire 131,072-token window in roughly 4.5 GiB of KV cache, because half its layers only ever attend to 128 tokens. Here is the arithmetic from the model's own config.json, and the machines that fit the ~70GB total.
llama.cpp KV Cache Quantization: q8_0 vs q4_0 vs f16 (2026)
llama.cpp and Ollama KV cache quantization: what q8_0 vs q4_0 vs f16 cost in VRAM, quality, and speed. --cache-type-k/-v flags, OLLAMA_KV_CACHE_TYPE, the flash-attention panic, and the silent f16 fallback.
Context Window Traps (2026): Why Your Local Agent Breaks After 10 Prompts
Ollama's default context is far below what an agent harness needs. The system prompt and tool schemas alone eat 15-20K tokens, so a 4-8K window silently truncates your tools. How to check it, set it, and budget the KV cache VRAM.