- Category: C. AI Engineering & LLM Ops
- English term: Context Length and KV Cache
- Related: Context Engineering · Chunking · Quantization · Inference Offloading · Local-First (Local Pipeline)
General Meaning
LLMs process input text as sequences of tokens. The maximum combined number of input and output tokens a model can handle in a single call is called its context length (or context window).
During autoregressive generation, Transformer models store the intermediate Key and Value tensors computed for past tokens in memory so they do not have to be recomputed for every new token generated. This memory buffer is called the KV Cache (Key-Value Cache), and its size grows proportionally with context length and batch size, consuming a substantial share of GPU VRAM or unified memory.
Why It Matters
- Accelerating token generation: Retaining the KV cache avoids recomputing attention over all preceding tokens every time a new token is emitted.
- Managing memory fit on local hardware: Calculating the sum of model weights plus KV cache is essential to ensure the workload fits within physical memory.
- Preventing costly model reloads: In serving engines like Ollama, changing the context length setting between requests triggers memory reallocation and a full model reload, causing severe latency spikes.
How the Site Factory Uses It
When running local models via Ollama on Mac mini (16GB unified memory) and Mac Studio machines, the Site Factory strictly standardizes num_ctx across jobs. On Mac mini running gemma4:12b, num_ctx: 4096 is the standard baseline (MQ_OLLAMA_OPTIONS), and engine/llm.py enforces {"num_ctx": 4096, "use_mmap": true} on every call because passing a different num_ctx forces Ollama to unload and reload the model, stalling subsequent jobs in the queue.
If token consumption reaches num_ctx - 16 or generation stops with finish reason length, the wrapper sets the near_limit flag to warn downstream logic. In engine/extract.py, the piece_size function dynamically calculates input character chunk size by subtracting system prompt and output budget from num_ctx, while large-context probes (such as 65,536 tokens in tools/hermes_a2a_execution_probe.py) are warmed up and executed exclusively on high-memory Mac Studio machines.
Caveats & Operational Rules
- Never expand context length blindly to the model maximum: Setting
num_ctxto a model's nominal limit (such as 128K) unconditionally wastes gigabytes of memory on KV cache alone, triggering out-of-memory crashes or heavy disk swapping. - Changing
num_ctxcauses queue stalls: When a single serving process handles sequential jobs, a request with a mismatchednum_ctxstalls the queue for several seconds while memory is freed and weights are reloaded. - Watch out for silent prompt truncation: When an input exceeds
num_ctx, some serving runtimes silently truncate the beginning of the prompt and continue inference without raising an error.
Related Terms
References
- Book: Chip Huyen, "AI Engineering", Chapter 7 (Inference Optimization and KV Cache)
Verified: 2026-11-08