Home / use case

How long context slows Ollama: qwen3-coder 30B from 1K to 100K

At a glance
Hardware
Surface Laptop Ultra with RTX Spark N1X and 64 GB, on AC power, Balanced plan
Models & engines
Ollama 0.40.11 with qwen3-coder:30b Q4_K_M fully on the GPU, flash attention on
context-curve.ps1, run once per cache type via OLLAMA_KV_CACHE_TYPE
Key gotcha
Calibrating characters per token on too short a slice would have pushed the 100K prompt past the context, and Ollama would have cut it in half without an error.

What they built

The author ran one question after prompts of seven sizes, from 935 to 96,926 tokens, three runs each, against qwen3-coder:30b in Ollama on his Surface Laptop Ultra's RTX Spark GPU, with the context window fixed at 102,400 tokens so only the filled cache changed.

He compared the results to a memory-bandwidth prediction from an earlier post, once with the default f16 KV cache and once with the 8-bit q8_0 cache, and published the PowerShell script that runs the test.

The result turns a single long-context measurement into a curve you can use to estimate how much slower a model gets as a conversation grows.

Setup

  • Surface Laptop Ultra with RTX Spark N1X and 64 GB, on AC power, Balanced plan
  • Ollama 0.40.11 with qwen3-coder:30b Q4_K_M fully on the GPU, flash attention on
  • 256-token replies at temperature 0 with thinking off; a random first line on each prompt so the prompt cache can't help
  • context-curve.ps1, run once per cache type via OLLAMA_KV_CACHE_TYPE

How it works

  1. Each cache type runs in its own ollama serve, and the server log line for llama_kv_cache confirms the cache size (9,600 MiB for f16, 5,100 MiB for q8_0).
  2. The script calibrates characters per token on the same text the largest prompt uses, records Ollama's own timings, and samples GPU clocks with nvidia-smi.
  3. A least-squares line through the seven means gives the fixed cost per token and the added cost per 1,000 cached tokens.

Results & numbers

  • With f16, time per token was 11.8 ms plus 0.39 ms per 1,000 tokens of context (R² 0.999), within 7% of the bandwidth prediction on slope; decode fell from 80 tokens/s at 1K to 20 at 97K.
  • q8_0 added 0.62 ms per 1,000 tokens, was never faster than f16, and was 26–32% slower from 30K to 97K tokens, while saving 4.4 GiB at 102,400 tokens.
  • Prompt processing dropped from about 2,400 tokens/s up to 8K tokens to 795 at 97K, with first token at 122 seconds for the largest prompt.

Gotchas

  • Calibrating characters per token on too short a slice would have pushed the 100K prompt past the context, and Ollama would have cut it in half without an error.
  • Stopping nvidia-smi can leave a half-written CSV line, which crashed the final step of one run; the parser now skips unreadable lines.
  • The 3.3 ms gap in fixed cost is the author's best guess at MoE routing, kernel launches and sampling, not something he profiled.
“Long conversations cost linearly, not suddenly.”— Steef-Jan Wiggers

Do it yourself

  1. Fix num_ctx to the largest size you want to test so only the prompt length changes.
  2. Start ollama serve with the KV cache type you want (OLLAMA_KV_CACHE_TYPE=f16 or q8_0) and flash attention on, and check the cache line in the server log.
  3. Send prompts of increasing size with a random first line, a fixed reply length, temperature 0 and thinking off.
  4. Record Ollama's prompt_eval and eval durations and compute tokens per second for each run.
  5. Fit a straight line to milliseconds per token against context length to estimate speed at any conversation length.
  6. Keep f16 unless you need the memory that q8_0 saves.
Source: Steef-Jan Wiggers (DEV Community) · Published 2026-10-29 · Added 2026-10-30. This write-up summarizes the original source; see it for full details.

Similar setups

Cutting a local Qwen coding agent's wait on a 16GB Mac with a memory hook

A developer running a coding agent on qwen3.5:9b through Ollama replaced its always-loaded memory index with a hook that attaches only relevant memory lines, and cut median answer time from 90.4s to 56.4s.

DEV Community (JustJinoIT) · 2026-12-02

Getting OpenCode to work with a local Ollama model by fixing the 4,096-token context

OpenCode on a local Ollama model ignored files, invented tasks or looped, because each ~23k-token request was silently cut to fit Ollama's default 4,096-token context on a 16 GB GPU.

DEV Community (Davidrs) · 2026-11-27

Testing a reasoning-effort fix on a local Qwen 27B for build-fix tasks

A developer ran a pre-registered overnight test on a local Qwen3.8 27B in Ollama to see if dropping a hidden reasoning-effort paragraph from the prompt helps it patch failing builds; the run was valid but scored 4/10 against a 5/10 bar.

DEV Community (hexisteme) · 2026-11-25

PatchPal TinyAgent: a coding agent on a 4B model and a 6GB laptop GPU

WiseProbe adapted its PatchPal coding agent to run Gemma 4 E4B or Qwen3.5 4B through Ollama on a 6GB RTX A1000, using handoff-note compaction and capped file reads to work inside a 16K or 32K window.

WiseProbe Blog · 2026-11-25