Longer full-attention chats occupy more KV-cache storage. A dynamic cache allocates memory as the sequence grows; a static cache reserves capacity ahead of time, so process memory may stay flat during the chat. For a full-attention model with fixed cache precision, doubling stored tokens roughly doubles this cache payload; doubling independent concurrent conversations doubles it again.
Separate the weights from conversation state
Weights describe the model. The KV cache describes computations associated with tokens in a particular sequence. Hugging Face's caching documentation explains how retaining earlier keys and values avoids recomputing them when generating later tokens.
This distinction explains a common surprise: a model loads successfully, answers a short question, then runs out of room during a document-heavy session. The same weight file now needs more room for conversation state. A smaller weight quantization frees space; adjust cache precision separately.
Start your worksheet with two separate entries, weight memory and cache memory. Keep runtime buffers and normal applications in additional rows. That makes it possible to identify whether the fix should be a smaller model, a shorter window or fewer simultaneous sessions.
Calculate the cache payload
For ordinary full attention with matching key and value dimensions, use:
KV bytes = 2 × L × Hkv × D × B × T × S
L = number of attention layers
Hkv = number of key/value heads
D = dimension of each head
B = bytes per cached element
T = cached tokens per sequence
S = independent sequences
The leading two counts keys and values. Use the architecture's KV head count rather than substituting its query head count. The equation estimates payload, excluding allocator overhead and other runtime memory.
Here is a worked example with an illustrative architecture: 32 layers, eight KV heads, head dimension 128 and an FP16 cache using two bytes per element. The per-token payload is 2 × 32 × 8 × 128 × 2 = 131,072 bytes, or 128 KiB. At 8,192 tokens, that becomes 131,072 × 8,192 = 1,073,741,824 bytes, exactly 1 GiB.
| Tokens per sequence | One sequence | Four independent sequences |
|---|---|---|
| 4,096 | 0.5 GiB | 2 GiB |
| 8,192 | 1 GiB | 4 GiB |
| 16,384 | 2 GiB | 8 GiB |
| 32,768 | 4 GiB | 16 GiB |
Moving from one 8,192-token sequence to four 32,768-token sequences multiplies this payload by 4 × 4 = 16. That is why a model that behaves well in a short chat can struggle when several agents inspect large files together.
The table assumes full attention, equal-length independent sequences and no prefix sharing. Sliding-window and other attention designs need architecture-specific accounting. Read the model configuration before treating this example as your model's requirement.
Distinguish reserved capacity from live tokens
Interpret the memory graph using the runner's cache allocation strategy. Hugging Face documents dynamic caches that grow with the sequence and static caches that reserve capacity in advance. Sliding-window layers can stop growing when their retained window fills.
For your runner, record memory just after loading, after a short request and during a long one. If the footprint is already large at the first step, investigate configured capacity and preallocation. If it grows with the input, investigate the live cache and retained conversations. Compare the observed pattern with the configured cache strategy before investigating a leak.
Ollama's November 2026 server code selects initial context defaults of 4,096, 32,768 or 262,144 tokens at effective VRAM thresholds below 23 GiB, 23 to below 47 GiB, and 47 GiB or more. Its user-facing documentation describes those as the below-24, 24-to-below-48 and 48-plus GiB hardware tiers. Use an explicit window for your experiment and check the loaded value with ollama ps.
Inside a running Ollama chat, /set parameter num_ctx 8192 sets the context. For a foreground server on macOS or Linux, after stopping an existing server using the port, set the baseline explicitly:
OLLAMA_CONTEXT_LENGTH=8192 OLLAMA_NUM_PARALLEL=1 ollama serve
For the four-request comparison, restart with OLLAMA_NUM_PARALLEL=4. A macOS app needs these variables set through launchctl setenv followed by an app restart; a Linux systemd service needs them in its service environment. The context guide covers per-request overrides.
Change cache precision only after fixing the budget
Ollama's FAQ lists f16 as the default KV cache type. It offers q8_0 at approximately half the memory and q4_0 at approximately a quarter, with a precision tradeoff. Its documented quantized-cache path requires Flash Attention.
For a controlled foreground-server comparison, keep the model and context fixed and start with:
OLLAMA_FLASH_ATTENTION=1 OLLAMA_KV_CACHE_TYPE=q8_0 \
OLLAMA_CONTEXT_LENGTH=8192 OLLAMA_NUM_PARALLEL=1 ollama serve
Those ratios are approximate, since quantized blocks carry overhead. For the example's 1 GiB FP16 payload, half is about 0.5 GiB; include block overhead and runtime allocations in the process budget. Test long-distance facts and exact numbers after changing precision, and record the backend actually used.
First remove unnecessary context and excessive concurrent work. Then compare cache precision. Otherwise you can spend time tuning a huge cache for information the task never needed.
Try it on your workload
Run three cases with fixed weights: one short chat, one long chat and four concurrent long chats. Agents on one local GPU share its throughput, and requests beyond its serving capacity wait in line. For each, record configured context, input and output tokens, memory before loading, peak memory, actual overlap and complete response time.
Save four request files named request-1.json through request-4.json, each with your model and a different document in this shape:
{"model":"YOUR_LOCAL_MODEL","prompt":"YOUR_DOCUMENT_AND_QUESTION","stream":false,"options":{"num_ctx":8192}}
Run one file for the single-request cases. After configuring four parallel requests, submit the group from a macOS or Linux shell:
for i in 1 2 3 4; do
curl -sS http://localhost:11434/api/generate \
-H 'Content-Type: application/json' \
--data-binary "@request-$i.json" > "result-$i.json" &
done
wait
Use independent fictional documents so accidental reuse of the same prefix does not dominate the result. Include a fact near the beginning and an exception near the end, and score whether the answer preserves both. Save each request and answer.
Repeat with the smaller window your task can tolerate, then with Q8 cache if supported. Change one variable per pass. Keep the configuration that meets the correctness rule and fits the actual concurrency, rather than choosing the lowest memory reading in isolation.
How to decide
- Cap context when the extra history does not improve accepted answers.
- Cap simultaneous sessions when independent caches exhaust the remaining memory.
- Use cache quantization only when the long-context checks still pass; reducing weight size alone may leave the main issue untouched.
Run four document agents without reserving four local caches
Four document agents working at once on qwen/qwen3-32b cost about 13 cents in tokens on TrustedRouter across the requests below. This lets you handle an occasional agent burst without reserving its full cache capacity on your own machine.
On 2026-11-22, the live catalog listed approximately $0.100 per million input tokens and $0.301 per million output tokens. Suppose each agent makes ten requests, each using 25,000 input and 2,500 output tokens. Per agent, that is 250,000 input and 25,000 output tokens. Combined input = 4 × 10 × 25,000 = 1,000,000; output = 4 × 10 × 2,500 = 100,000. Cost = 1 × $0.100 + 0.1 × $0.301 = $0.1301, about 13 cents.
On one local GPU, those agents share throughput, and requests beyond its parallel capacity wait in line; through a per-token API, they can run together for the same token bill as sequential work. Count repeated context in each request's input budget. Prices change; check current prices and agent setup. For approved conversations, encrypted inference and zero data retention depend on model and provider. Local remains better for steady single-user work or conversations that must stay offline.
Sources
- Ollama: generation requests and runtime options
- Hugging Face: cache-memory formula and precision
- Hugging Face: dynamic, static and sliding-window caches
- Ollama: parallelism, cache types and server environment settings
- Ollama: documented context tiers and loaded context display
- Ollama: context-default selection in the server