Zenaique

KV cache growth when a reasoning trace spans thousands of thinking tokens

Predict output·Medium·4.0 · 0·~2 min·Asked atMicrosoftNVIDIASierra
Attempt it
A reasoning request prefills 512 prompt tokens, then autoregressively generates 4,000 thinking tokens followed by 300 answer tokens. KV cache stores one key/value pair per generated token (prefill included in the cache before decode). How many tokens total sit in the KV cache after generation completes?
TL;DR

KV cache holds 512 prefill + 4,000 thinking + 300 answer tokens = 4,812 total after generation completes.

Memory aid
Sign in to see the mnemonic that makes this stick.
Easy to grasp

Think of the KV cache like a growing notebook where the model writes down every word it has read or generated so it does not re-read from scratch. The prompt pages go in first, then every thinking word, then every answer word. Count all pages in the notebook — not just the thinking chapter.

Concept explanation~2 min read

Everything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example. Click to expand.

Long chain-of-thought breaks serving economics through KV cache growth. This predict-output question tests whether you count every token in the sequence — not just the visible answer.

We walk prefill, decode, and the arithmetic behind 4,812.

What the KV cache stores

During autoregressive generation, transformers cache key and value projections for prior tokens so each new token attends to history without recomputing past K/V. Cache length T grows by one per processed token.

Prefill phase processes the prompt in parallel (often batched attention), populating cache for all prompt positions. Decode phase generates one token at a time, appending to cache each step.

Accounting every segment in this scenario
Common counting mistakes
Why this matters for reasoning serving
Sign in to unlock the full deep dive.

Situations where this technique stops working.

Sign in to see when this approach fails.

2–4 min · Everything important, quickly.

Sign in to see the quick scan of the deep dive.
python
# KV cache token count after full generation
prefill = 512
thinking = 4000
answer = 300
total_kv_tokens = prefill + thinking + answer  # 4812

Real products, models, and research that use this idea.

  • OpenAI o-series thinking tokens bill separately because they extend decode length and cache footprint.
  • vLLM and TensorRT-LLM paging evict KV blocks when long CoT blows per-request memory.
Sign in to see more production examples.

What an interviewer would ask next. Try answering before peeking at the approach.

QEstimate HBM for T=4812 on a 70B GQA model with concrete dims.
A

Plug L, H_kv, d_h, b into the bytes formula and convert to GB.

1 more follow-up an interviewer would ask next. Sign in to reveal them.

Red flags & common mistakes

The phrases that signal junior thinking. Click to expand.

Most common mistake

Counting only thinking tokens (4,000) or only generated tokens (4,300) while omitting the 512-token prefill.

Sign in to see all red flags and common mistakes.

60 second bullets to scan on the way to the call.

  • Prefill tokens enter cache before decode

  • Each decode step appends one token

Sign in to unlock the revision sheet.

Primary sources. Browse if you want the original framing.

Similar questions

Same topic, related formats. Practice these next.

4 curated
Next question
Match each RL algorithm trait to PPO or GRPO.
Match pairs·Medium