# What Uses Memory at Inference Time?

> Which parts of a GPU's memory a model uses while it answers: the weights, the KV cache and working memory, and how teams fit more users, for your AI interview.

**In short:** While a model answers, the graphics card holds the model's weights and the KV cache, a store of past tokens that grows with every word of every chat. The weights stay fixed, so the number of chats open at once, and how long each one runs, decides whether everything fits.

AI and LLM interview question Q074, Transformer architecture. Advanced, common (our estimate). From Venkat R. Murty's free Interview Kit: https://designervenkat.online/interview/ai/what-uses-memory-at-inference-time

## Say it in 30 seconds

The graphics card holds two big things while a model answers. The first is the weights, the model's learned numbers, which stay the same size. The second is the KV cache, a notebook of past tokens that grows with every word of every chat. Working memory for each step is reused. So when I size a server, I add up the weights and the cache for the chats I want at once, then check that it fits.

## Say it in 2 minutes

Inference is the moment a trained model answers. The model's weights are loaded into GPU memory first, and they stay there. NVIDIA's 2023 guide puts a 7 billion parameter model in 16-bit numbers at about 14 GB, and that size is fixed no matter what the user types.

The second big user is the KV cache. For every token of every chat, the model keeps key and value lists for each layer, so it does not redo that work. The cache grows with each word, and each chat in the batch gets its own. One 4,096-token chat is about 2 GB on that same model.

The third part is working memory, the temporary numbers made during each step. It is reused from one step to the next, but it still needs room. Add the three and you get the peak the card must hold. If the total is too big, the server runs out of memory and requests fail.

So I size it in this order. Count the weights, count the cache for the chats I want at once, and leave a margin. If it doesn't fit, I use quantisation for smaller weights, a model with fewer cached heads, or a paged cache that wastes less space. Each one has a cost, so I test quality after the change.

## What is inference memory, in plain words?

Inference is the step where a trained model answers. Training is the long, earlier step that built it. In this answer, memory means the memory on the graphics card (GPU) that runs the model. It has nothing to do with an agent's long-term notes.

Think of a call centre desk. The desk, the phone system and the software are in place before the first call, and they never change size. Each open call adds a folder of notes, and the folder grows until the call ends. A busy desk fills up because of the open folders.

In an AI system, the desk is the model's weights and each open chat is a KV cache. When a bank's card support assistant has four long chats open at once, the card holds one copy of the model and four caches beside it.

- **Token:** A small piece of text, about a word or part of one, that the model reads and writes.
- **Weights:** The model's learned numbers, called parameters. Loaded once, they stay the same size.
- **KV cache:** A notebook of key and value numbers for each past token. It grows as the chat grows.
- **Batch:** The group of chats the model works on together in one step.
- **Quantisation:** Storing each number in fewer bits than before, to save memory.

## What takes up memory while the model answers?

NVIDIA's 2023 guide names two main contributors to GPU memory: the model weights and the KV cache. Working memory and some overhead make up the rest.

The weights come first. Hugging Face gives a rule of thumb for 16-bit numbers: about 2 GB for every billion parameters. A 7 billion parameter model therefore needs about 14 GB just to load. For shorter inputs, under 1,024 tokens, the weights are what mostly fills memory.

> "In effect, the two main contributors to the GPU LLM memory requirement are model weights and the KV cache." (NVIDIA, 2023)
>
> "existing systems struggle because the key-value cache (KV cache) memory for each request is huge and grows and shrinks dynamically." (The vLLM paper, 2023)

*Diagram, Where the memory goes while a model answers: The weights are loaded once and stay the same size. The cache grows with every token of every chat, and working memory is reused at each step. The sum has to fit on the card.*
1. The weights take a fixed block of the card.
2. Each open chat adds its own cache, and the cache keeps growing.
3. Working memory for each step sits on top, and is reused.
4. The total must fit on the card, or the server cannot serve all its chats.

Working memory is the third part. It holds the temporary numbers made during each step, and that space is reused from one step to the next. The server still needs room for it. Some memory also goes to the software that runs the model. No formula covers that, so leave a margin.

## Why does the KV cache get so big?

The KV cache keeps a key list and a value list for every layer and every past token. That way the model does not redo the work at each new word. Its size grows with five things: the number of layers, the hidden size (how wide the model is), the bytes per number, the number of tokens, and the number of chats open at once.

NVIDIA writes the total as batch size times sequence length times 2 times the number of layers times the hidden size times the bytes per number. The 2 is for keys and values. Each chat in a batch gets its own cache, so the total grows with the number of chats.

Take a bank's card support assistant. It runs a 7 billion parameter model in 16-bit numbers on a 24 GB card. The weights take about 14 GB, which leaves 10 GB. One 4,096-token chat needs about 2.15 GB of cache, so only four long chats fit, and working memory needs room too. Ten chats at once would need about 21.5 GB of cache, and the card would run out.

Here is the same sum in Python, in decimal gigabytes (1 GB = 1,000,000,000 bytes). The last line checks the formula against NVIDIA's example, which is about 2 GB.

```python
# A 7B-parameter model in 16-bit numbers (2 bytes each), with the shape NVIDIA used in its 2023 Llama 2 7B example:
# 32 layers, hidden size 4096. Sizes are decimal gigabytes (1 GB = 1,000,000,000 bytes).
PARAMS = 7_000_000_000
LAYERS = 32
HIDDEN = 4096
BYTES = 2
CARD_GB = 24
CHAT_TOKENS = 4096

weights = PARAMS * BYTES / 1e9
# Keys and values (2), for every layer, every token, every chat.
per_chat = 2 * LAYERS * HIDDEN * BYTES * CHAT_TOKENS / 1e9
room = CARD_GB - weights
chats = int(room // per_chat)
print(f"Weights: {weights:.1f} GB")
print(f"Cache for one 4,096-token chat: {per_chat:.2f} GB")
print(f"Room left after the weights: {room:.1f} GB")
print(f"Long chats that fit: {chats}")
print(f"Ten chats need: {10 * per_chat:.1f} GB of cache")
# Sanity check: NVIDIA's example, 1 x 4096 x 2 x 32 x 4096 x 2 bytes, is about 2 GB.
print(f"NVIDIA's example in GiB: {1 * 4096 * 2 * 32 * 4096 * 2 / 2**30:.1f}")
```

Output:

```text
Weights: 14.0 GB
Cache for one 4,096-token chat: 2.15 GB
Room left after the weights: 10.0 GB
Long chats that fit: 4
Ten chats need: 21.5 GB of cache
NVIDIA's example in GiB: 2.0
```

The simple formula assumes every attention head keeps its own key and value lists. Grouped-query attention, which the GQA paper introduced, uses an intermediate (more than one, less than number of query heads) number of key-value heads, so the cache per token is smaller.

## How do teams fit more chats on one card?

There are three levers, and each one has a cost. Smaller weights through quantisation: Hugging Face notes that it trades memory against accuracy, so the team tests quality before it switches. A smaller cache through fewer heads, which is set in the model's design. You get it by choosing a model built that way, not by changing a running one. And a paged cache, which stores the cache in small blocks, like pages, so the vLLM paper says it wastes close to nothing.

Some serving software also caps how much of the card it may use. vLLM, for example, has a setting for the fraction of GPU memory it can use, from 0 to 1.

*Diagram, Three levers to fit more chats on one card: Each lever attacks one part of the sum: the weights, the cache per chat, or the waste in the cache. Every route ends in the same check, because the total must still fit on the card. Test quality after each change.*
1. Fewer bits per number make the weights smaller.
2. A model with fewer key and value heads has a smaller cache.
3. Storing the cache in small blocks wastes less space.
4. Smaller weights leave more room for chats.
5. Fewer heads means fewer numbers stored per token.
6. Less wasted space means more chats per card.

> **Tip:** Asked how to fit more users, don't jump to a bigger GPU. Do the sum first, then name the lever you would pull and what it costs in quality or effort.

## What does the interviewer want to hear?

**A strong answer covers**

- [ ] Name the two big parts: the weights and the KV cache.
- [ ] Say the weights are fixed, and the cache grows with tokens and chats.
- [ ] Add working memory and the peak it creates.
- [ ] Do the fit check: the total against the card's size.
- [ ] Name the levers, quantisation, fewer cache heads and a paged cache, with their costs.
- [ ] Admit the numbers depend on the model, the precision and the batch.

**Common traps**

- Trap: "Only the model size matters." Better: Model size sets the weights. Users and context length set the cache.
- Trap: "The KV cache is a fixed cost." Better: It grows with every token and with every chat open at once.
- Trap: "Buy a bigger GPU first." Better: Count first. Smaller weights or a smaller cache may fit the same card.

## Can you spot the gap?

**Spot the gap:** Which sizing note says the assistant fits without counting the cache?

A. Weights: about 14 GB for a 7 billion parameter model in 16-bit numbers.
B. Cache: about 2.15 GB per 4,096-token chat, so about four long chats fit beside the weights.
C. Weights only: 14 GB. The card has 24 GB, so the assistant fits.
D. Working memory is usually smaller than the cache, and it is reused, so we keep a margin for it.

**Answer:** C. The third note counts only the weights, then says the assistant fits. The cache for each chat and the working memory are missing, so the real limit is much lower.

**Quick check:** A bank's assistant runs a 7 billion parameter model on a 24 GB card, with ten long chats at once. What should the team check first?

A. Whether the model's name is the newest one
B. Whether the cache for ten chats fits in the space left after the weights
C. How fast the model writes tokens per second
D. Whether the temperature is set low

**Answer:** B. Ten chats need about 21.5 GB of cache, and only about 10 GB is left after the weights. Speed and temperature do not change how much memory is used.

## Can you answer these from memory?

**Flash cards**

- **What uses memory at inference time?** The model weights and the KV cache, plus working memory for each step.
- **Do the weights change size during a chat?** No. They load once and stay the same size for every chat.
- **What makes the KV cache grow?** Each new token, each chat open at once, and more layers.
- **Rule of thumb for weights in 16-bit?** About 2 GB for every billion parameters.
- **What does quantisation trade?** Less memory for some accuracy. Test quality after the change.
- **What is the fit check?** Weights, plus the cache for every open chat, plus working memory, must fit on the card.

## Likely follow-up questions

- Q055 What is the KV cache, and why does it speed up generation? https://designervenkat.online/interview/ai/what-is-the-kv-cache
- Q319 What is PagedAttention / vLLM? (full answer coming)
- Q311 What is quantization? https://designervenkat.online/interview/ai/what-is-quantization
- Q327 GPU basics: why does memory bandwidth limit decoding? (full answer coming)
- Q328 Does a 70B model fit on one 80GB GPU? (full answer coming)

## Learn it in depth

- [Inference Engineering](https://designervenkat.online/learn/ai-engineering/inference-engineering)
