← Blog

How much does a long context cost in a local RAG? 512 vs 2k vs 4k vs 8k tokens

Agile Store · 2 December 2026 · 17 min read

  • RAG
  • Language models (LLM)
  • Tesla P40 24 GB
  • Tesla V100 16 GB
  • Tesla V100 32 GB
  • 0
  • 0

Short answer: a long context costs above all prefill (prompt processing) time and VRAM; it changes the decode (generation) rate much less. Raising the context budget from 512 to 8192 tokens (measured prompts of 258 to 4,098 tokens) lengthened the estimated prefill time 6–⁠9× on the V100 and about 14× on the P40 and raised VRAM usage by about 1.6 GiB, while the generation rate fell by only 5–⁠9%. These are two different bottlenecks that are easy to confuse if you look only at tokens/s.

We sell used data-⁠centre cards, and the figures below come from our own measurements. In the previous post in this series I compared the P40 and the V100 at a single context length. This time I keep the card fixed and change the context length, to show what happens when your RAG starts injecting more and more fragments (chunks) from retrieval into the prompt.

One guiding example: the same document, four context lengths

Imagine a typical company RAG scenario: a user asks a question, the system pulls passages from the vector database and injects them into the prompt together with the question. The more fragments you pull in (2, 4, 8, 16), the longer the prompt that reaches the model. In our benchmark this is exactly the situation: a context built from real passages of the scifact-⁠pl corpus, trimmed to four thresholds: 512, 2048, 4096 and 8192 tokens.

The guiding model: Bielik-⁠11B-⁠v2.3-instruct (SpeakLeash), a Polish model that we actually use in deployments of this kind. Tested in two quantisations, Q4_K_M (the main series, the practical choice for production) and Q8_0 (the second series, to check whether higher-⁠bit quantisation changes the picture), on three cards: P40 24 GB, V100 16 GB, V100 32 GB, through Ollama, at the full power limit. NVIDIA driver 580.173.02, num_ctx equal to the context threshold; we did not record the Ollama version, the Flash Attention setting, the KV cache type or num_batch. Generation in every run is num_predict=128 answer tokens: this matters, because in a moment I will work out the energy cost of one answer from it.

Three caveats to start with. First: this is one model, one corpus and one backend (Ollama), one unit of each card, and each data point is a single run. The figures below describe exactly this configuration; I do not generalise them to other models or inference engines. Second, and more important for reading the tables: the thresholds 512/2048/4096/8192 are the context budget to which we trimmed the passages. The actual prompt length measured by Ollama (prompt_eval_count) was half that: 258, 1026, 2050 and 4098 tokens respectively, i.e. exactly half the threshold plus two tokens. A tokeniser mismatch would hardly produce such an exact ratio, but we do not know where it comes from: we did not record the rule by which the prompt was built, nor the Ollama server log that would show whether a prompt was truncated. In the tables I keep the threshold labels, because that is how the series is named, but I do all the time calculations below on the measured tokens. The "@8192" column therefore corresponds in practice to about 4.1k prompt tokens. I do not use it to extrapolate the time for an actual 8k-⁠token prompt: the pp tok/s changes with the prompt length and the backend, so such a point has to be measured separately, and it is not in this series. Third: in the text I separate three kinds of figures: measured (pp, tg, VRAM, peak power from telemetry), derived (estimated prefill time, energy per answer, calculated from the measured ones) and hypotheses (explanations of mechanisms that this test does not isolate).

Prefill (prompt processing): this is where context costs the most

Let us start with what grows the most with the context: prefill, i.e. prompt processing (pp, tokens/s). This is the stage at which the model "reads" the whole injected context before it starts generating the first token of the answer.

Card Quant pp @512 pp @2048 pp @4096 pp @8192
P40 Q4_K_M 462.3 563.4 540.6 506.7
V100-⁠16 Q4_K_M 803.5 1638.2 1816.3 2132.9
V100-⁠32 Q4_K_M 781.1 1722.7 1770.7 2138.4
P40 Q8_0 444.9 548.9 530.3 497.8
V100-⁠16 Q8_0 979.4 1951.3 2038.5 1719.3
V100-⁠32 Q8_0 1098.3 1885.7 2079.6 2353.1

The first thing that stands out: pp on the P40 practically stands still regardless of the context length (462–⁠563 tok/s in Q4_K_M), while on both Voltas it grows clearly from 512 to 4096–⁠8192 tokens. The hypothesis I consider most likely has two parts. Prefill processes the prompt in large matrix batches, so it is largely limited by compute power, and Volta has Tensor Cores, which Pascal (the P40) does not: this fits the V100's prefill advantage over the P40, which generally widens with prompt length and reaches about 4–⁠5× at the 8192 threshold, apart from the V100-⁠16 Q8_0 anomaly. The rise of pp with length on the Voltas looks more like a fixed per-⁠request cost spread over more tokens: in Q4_K_M the V100 times fit a constant of about 0.2 s plus about 2,400 tok/s for the additional tokens (increments between neighbouring points range from about 1,800 to 2,900 tok/s), while on the P40 the rate for the additional tokens falls slowly (from about 610 to 480 tok/s) as attention over a longer context gets more expensive. Our test does not isolate this: we did not profile the kernels or change the backend, so this is a conclusion from the shape of the curves, not a measured mechanism.

One anomaly that is visible in the table and that I do not want to hide: the V100-⁠16 in Q8_0 at the 8192 threshold drops to 1719 tok/s, although in the other three Volta series pp is highest at this threshold (both P40 series peak at the 2048 threshold). Each point in this series is a single run, without repeats, so I cannot say whether this is a feature of the unit or the noise of a single measurement. I do not build any conclusion on it.

Why this matters for your RAG: if you pull in many fragments (strong retrieval, a wide context), the difference between the cards in just "reading" the prompt becomes more and more significant, not only in generation, which I will come to in a moment.

Estimated prefill time: a derived value, not a direct measurement

The benchmark CSV does not directly measure time to first token (TTFT) end to end. Here I calculate a derived value: estimated prefill time = ctx_tokens / pp_tok_s, the measured number of prompt tokens (258/1026/2050/4098, not the threshold label) divided by the processing rate, i.e. how many seconds the processing of the context alone takes before the model starts generating. In this test it is the main component of TTFT, but not the full TTFT: it leaves out network overhead, queuing in Ollama and any model loading time. We did not measure these overheads and I do not assume they are the same for all cards and runs, so compare the figures below with each other as the time of prefill alone, not as the user's experience with a stopwatch in hand.

Card Quant prefill @512 prefill @2048 prefill @4096 prefill @8192
P40 Q4_K_M 0.56 s 1.82 s 3.79 s 8.09 s
V100-⁠16 Q4_K_M 0.32 s 0.63 s 1.13 s 1.92 s
V100-⁠32 Q4_K_M 0.33 s 0.60 s 1.16 s 1.92 s
P40 Q8_0 0.58 s 1.87 s 3.87 s 8.23 s
V100-⁠16 Q8_0 0.26 s 0.53 s 1.01 s 2.38 s
V100-⁠32 Q8_0 0.24 s 0.54 s 0.99 s 1.74 s

This table shows most clearly where a long context costs. At 512 tokens the difference in estimated prefill time between the P40 and the V100 is a fraction of a second. At the 8192 threshold (a measured ~4.1k prompt tokens) the P40 in Q4_K_M needs more than 8 seconds for prefill alone, while both Voltas manage in under 2 seconds. That is a more than 4-⁠fold difference exactly where the user is waiting idly, before the first token appears at all.

An observation from practice (an opinion, not a measurement): in RAG demos, customers more often notice the time of "silence" before the first letter than the rate at which the text streams, which flows faster than anyone can read anyway. If your RAG injects a lot of context, it is prefill, not tg, that is the main component of how "fast" the system feels.

Decode (generation): here the context costs almost nothing

The contrast with prefill is clear. Decode, i.e. generation (tg, tokens/s), falls with a growing context, but very gently.

Card Quant tg @512 tg @2048 tg @4096 tg @8192 drop 512→8192
P40 Q4_K_M 27.90 27.54 26.63 25.53 −8.5%
V100-⁠16 Q4_K_M 79.84 74.90 76.18 72.53 −9.2%
V100-⁠32 Q4_K_M 73.82 73.09 71.48 68.63 −7.0%
P40 Q8_0 18.13 18.07 17.79 17.20 −5.1%
V100-⁠16 Q8_0 55.79 55.63 54.21 52.66 −5.6%
V100-⁠32 Q8_0 54.17 52.66 51.32 50.33 −7.1%

In all six card/quant combinations the tg drop between 512 and 8192 tokens falls within 5–⁠9%: on our units we observed a consistent pattern here. A hypothesis consistent with the prefill picture: generation is limited by memory bandwidth (reading the KV cache at every step), not by compute power: a growing context adds a little work at each decoding step, but not as much as the one-⁠off processing of the whole prompt at the start. Our test does not isolate this: it is a conclusion from the shape of the curves, not from profiling.

What follows from this: if you optimise RAG for "how fast the model answers", intuition tells you to shorten the context. The data show that shortening the context mainly saves prefill time and barely touches the rate at which the answer itself streams. If your problem is a long wait for the first token, trimming retrieval to fewer fragments is the cheapest first step: on the P40 in Q4_K_M, halving the context from the 8192 to the 4096 threshold cuts the estimated prefill from 8.09 s to 3.79 s, although switching to a V100 at the 8192 threshold cuts it further, to 1.92 s. Check answer quality after trimming, because fewer fragments can leave out the passage that holds the answer.

VRAM: it grows linearly, but not dramatically

Card Quant VRAM @512 VRAM @2048 VRAM @4096 VRAM @8192
P40 Q4_K_M 6.57 GiB 6.88 GiB 7.27 GiB 8.18 GiB
V100-⁠16 Q4_K_M 6.88 GiB 7.18 GiB 7.57 GiB 8.49 GiB
V100-⁠32 Q4_K_M 6.88 GiB 7.18 GiB 7.57 GiB 8.49 GiB
P40 Q8_0 11.30 GiB 11.61 GiB 12.00 GiB 12.91 GiB
V100-⁠16 Q8_0 11.61 GiB 11.91 GiB 12.30 GiB 13.09 GiB
V100-⁠32 Q8_0 11.61 GiB 11.91 GiB 12.30 GiB 13.22 GiB

The jump from the 512 to the 8192 threshold is about 1.6 GiB of additional VRAM in Q4_K_M on each of the three cards, and about 1.5–⁠1.6 GiB in Q8_0: a substantial amount, but it fits on a 16, 24 or 32 GB card. This increase is consistent with a larger KV cache reservation for a larger configured context window, and in our runs we set num_ctx to the threshold value. In Ollama, shortening the prompt while keeping the context window and parallelism unchanged does not reduce the KV cache reservation; lowering num_ctx can reduce that reservation. Total VRAM use also includes other buffers and allocations. Here you can clearly see where VRAM starts to hurt: the V100-⁠16 GB in Q8_0 at the 8192 threshold takes 13.09 GiB, leaving only about 3 GiB of headroom (a little less in practice). If you want to run an embedder for retrieval on the same card at the same time, check whether it fits within that headroom. The P40 24 GB and the V100-⁠32 GB have much more margin here.

Energy per answer: a simple proxy from peak power

The peak power draw (peak_pwr) in our benchmark is one value per card×quant series: the highest reading of the nvidia-smi sidecar (sampling every 1 s) over the whole run, not a separate measurement per context. It is a telemetry reading, not a catalogue value; I do not use it to draw conclusions about the actual average draw or compliance with the power limit. We did not measure energy with a meter or integrate the power series over time. So I calculate the energy cost of one answer as a simple proxy: the highest power sample observed, multiplied by the time the card spends generating those 128 answer tokens: a rough comparative value between cards, not a measurement or a guaranteed upper bound on the actual energy. And that time grows with the context, because tg falls slightly.

Card Quant Generation energy proxy @512 Generation energy proxy @8192 increase
P40 Q4_K_M 1053 J 1151 J +9.3%
V100-⁠16 Q4_K_M 395 J 435 J +10.1%
V100-⁠32 Q4_K_M 434 J 467 J +7.6%
P40 Q8_0 1446 J 1524 J +5.4%
V100-⁠16 Q8_0 552 J 585 J +6.0%
V100-⁠32 Q8_0 585 J 630 J +7.6%

(Calculated as peak_pwr × 128/tg_tok_s: a derived value that assumes the card works at peak draw for the whole of the generation time. Sampling at 1 Hz does not guarantee capturing the actual maximum between samples, so treat these figures as a rough comparative proxy, not as a measurement or a ceiling.)

Two things are worth emphasising. First, this proxy covers generation only, and there the increase due to the longer context is moderate (5–⁠10%), because it follows the small drop in tg. Prefill is not in it, and prefill is where a long context costs the most: with the same proxy (peak power × estimated prefill time), the P40 in Q4_K_M adds about 130 J at the 512 threshold but about 1,860 J at 8192, so the whole answer goes from about 1,180 J to about 3,000 J; on the V100-⁠16 it goes from about 470 J to about 910 J. Second: the energy proxy for the P40 in Q4_K_M is 1053–⁠1151 J per answer, i.e. about 2.7× more than for the V100-⁠16 (395–⁠435 J), despite a similar peak draw (229 W vs 247 W). The difference comes from time: the P40 needs almost three times as long to generate the same 128 tokens. A slower card does not save energy, because it pays for it with working time, not with lower draw.

An observation from choosing cards for contracts with a limited electricity budget in the server room (an opinion, not a measurement): customers often look only at the card's TDP in the specification, assuming that a lower TDP means cheaper running. Our estimates indicate that in inference what counts is the energy per generated answer, not the card's instantaneous power.

What follows for designing a RAG system

Let us gather this into one practical decision map.

If your RAG injects a short context (a threshold up to about 2k, i.e. ~1k measured prompt tokens, a few fragments): the difference between the cards in estimated prefill time is small (under 2 seconds even on the P40). Here what decides is mainly tg, i.e. the speed at which the answer itself streams, and there the V100 clearly wins, as I showed in the previous article in this series.

If you inject a long context (thresholds of 4k–8k, i.e. ~2–⁠4k measured prompt tokens, wide retrieval): prefill time starts to dominate the wait for the answer, and here the P40 loses the most: more than 4× slower than Volta at the 8192 threshold. If you cannot switch to a V100, the first move is to cut the number of fragments from retrieval: it costs nothing and works immediately, as long as answer quality holds up with fewer passages.

If VRAM is the bottleneck (because alongside the LLM you want to keep an embedder, or you plan a context longer than 8k): the P40 24 GB and the V100-⁠32 GB provide headroom that is almost exhausted on the V100-⁠16 GB in Q8_0 at 8k tokens.

If the electricity bill matters: do not look at the peak draw or the card's TDP in the specification: estimate the energy per answer. A slower card with a similar peak draw can cost considerably more to run, because it works longer on the same task.

Limitations of these data

I will repeat what I noted at the start, because it is worth it: this is one model (Bielik-⁠11B-⁠v2.3), one corpus (scifact-⁠pl), one inference engine (Ollama) and one unit of each card. Another model with a different architecture (e.g. a different ratio of attention layers to feed-⁠forward layers) or another backend (vLLM, TGI) may give a different scaling profile with the context, especially since some inference engines use other KV cache management strategies that directly affect how the cost of prefill grows with the prompt length. The estimated prefill time in this article is a value derived from pp, not an end-⁠to-⁠end TTFT measurement; the energy per answer is a rough proxy from sampled peak power (not an upper bound, not a meter reading). Each point is a single run, without repeats: differences of a few per cent (e.g. between the V100-⁠16 and the V100-⁠32) may fall within measurement noise; with differences of 3–⁠4× I consider that unlikely, but there are no repeats in this series that would settle it. The explanations of the mechanisms (compute vs memory bandwidth) remain hypotheses: this test does not isolate them. The threshold labels in the tables are the context budget, and the measured prompt length was half that: take this into account before you translate these figures to your own retrieval size; we have no data point for prompts longer than ~4.1k tokens. Do not extrapolate these figures to other models without checking for yourself.

Conclusion

Context length in a local RAG is not a parameter that "just works" regardless of how many fragments you add from retrieval: it is a separate cost, paid mainly in prefill, before the first token appears, and on a slower card it grows much faster than on a faster one. If you are building a RAG system and looking only at generation tokens/s, you are looking at half the picture: the other half waits silently before the model even starts to speak.

Sources

All the figures in this article come from our own measurements, carried out on our own test bench (not from manufacturers' datasheets or external publications).

  1. LLM inference measurements (Bielik-⁠11B-⁠v2.3-instruct, Ollama, Q4_K_M and Q8_0 quants, context 512–⁠8192 tokens) on the P40 24 GB, V100 16 GB and V100 32 GB cards: full power limit, a uniform measurement methodology for all three cards. Measurement date: 2026-⁠09-⁠06.
  2. The estimated prefill time and the energy-⁠per-⁠answer proxy in this article are our own calculations based on the raw measurement data from point 1 (number of prompt tokens, processing and generation rate, peak power draw from telemetry), clearly marked in the text as derived values, not separate direct measurements.

Full documentation of the methodology and the raw measurement data are available on request: write to us.

FAQ

Which stage is affected more as the context grows: prefill or generation?
In our test, clearly prefill. The estimated prompt processing time on the P40 (Q4_K_M) grows from 0.56 s at the 512 threshold to 8.09 s at 8192, and on the V100-⁠16 from 0.32 s to 1.92 s. The generation rate falls by only 5–⁠9% over the same range.
How much does the generation rate drop with a long context?
With an 8192-⁠token context budget holding about 4.1k prompt tokens, the tg drop against the 512 threshold was within 5–⁠9% in all six card×quant combinations (e.g. P40 Q4_K_M: 27.90 → 25.53 tok/s). We did not measure a prompt that fills 8k tokens; the extra cost comes from reading a longer KV cache, so expect a larger drop there.
How much more VRAM does an 8192 context use than 512?
About 1.6 GiB in Q4_K_M and 1.5–⁠1.6 GiB in Q8_0 on each of the three cards: e.g. V100-⁠16 Q8_0: 11.61 GiB → 13.09 GiB. Ollama's KV cache reservation depends on the configured context window and parallelism; shortening the prompt alone does not reduce it. In this series we set num_ctx to the threshold value. Total VRAM use also includes other buffers and allocations.
Does a long context increase energy use?
For generation alone, our proxy (peak power × generation time) rises only 5–⁠10%, following the small drop in tg. Including prefill in the same proxy, the estimated energy per answer at the 8192 threshold is about 1.7–⁠2.6× that at 512 (P40 Q4_K_M: about 1,180 J → 3,000 J). The difference between cards is large either way: generation alone takes about 1053–⁠1151 J per answer on the P40 Q4_K_M against 395–⁠435 J on the V100-⁠16.
When does the P40 vs V100 difference in prefill become noticeable?
With a short context (a threshold up to ~2k) the estimated prefill time is under 2 s even on the P40. In our Q4_K_M at the 4096 threshold the estimated prefill took 3.79 s on the P40 vs 1.13 s on the V100-⁠16 (~3.4×), and at 8192, 8.09 vs 1.92 s (~4.2×). With a long context, prompt processing alone becomes a significant part of the wait before generation.

Related products