中文

Local inference · Hardware reportVersion of 30 April 2026 · data to 27 April

Gemma 4 · Apple Silicon · integrating Claude Code and Copilot CLI

The Real Bottleneck Is KV Cache and Prefill, Not the Context Limit

This report works out what hardware is needed to run Gemma 4 locally behind an agentic CLI tool. The conclusion first: in an agentic workflow what determines how it feels is prefill speed and KV cache footprint, not how much context the model can accept. That is also why the 26B-A4B mixture-of-experts model, with only 4B parameters active, is the sweet spot for local agentic work.

  • 60–70%KV cache saved by Gemma's 5:1 attention
  • ÷2throughput roughly halves for each further 25% offloaded to CPU
  • ~99 slatency of one tool-call cycle on an M4 Pro with 24 GB
  • 5–7×how much more a comparable PC costs to run around the clock

About these figures

Measurements of tokens per second, KV cache and power draw come from community sources: llama.cpp, Unsloth, LLMCheck, Hardware Corner and r/LocalLLaMA. Combinations that were not measured are marked as estimates. None of this is purchasing advice, and prices and models change over time.

First, a Correction: Gemma 4 Is Not Named 1B/4B/12B/27B a common mix-up

The Gemma 4 released by Google DeepMind on 2 April 2026 is named E2B, E4B, 26B-A4B and 31B. The 1B, 4B, 12B and 27B sizes belong to Gemma 3 (2025 into early 2026), which is still available from Ollama. The tables below cover both generations.

Commonly saidGemma 4 SKUArchitectureMax context
1BE2BDense with PLE128K
4BE4BDense with PLE128K
12BNo equivalent; nearest is 26B-A4BMixture of experts — 128 experts, 8 active256K
27B31B dense, the flagshipDense256K

PLE is per-layer embeddings. E2B and E4B perform like dense models but carry more parameters than the name suggests; in effective terms they are about 2.3B and 4.5B.

GGUF Weight Sizes Unsloth Dynamic 2.0

ModelQ4_K_MQ8_0BF16
Gemma 4 E2B (~2.3B effective)~1.6 GB~2.6 GB~4.6 GB
Gemma 4 E4B (~4.5B effective)~3.0~5.0~9.0
Gemma 4 26B-A4B MoE16.926.950.5
Gemma 4 31B dense18.332.661.4
Gemma 3 12B6.6~1324
Gemma 3 27B14.1–15.1~2854

KV Cache: It Can Be Cut to a Third Gemma's 5:1 local-to-global attention

KV size is 2 × L × H_kv × T × d_head × bytes_per_element, where bytes per element is 2 for FP16, 1 for Q8_0 and 0.5 for Q4_0. In practice, enabling --cache-type-k q8_0 -fa on in llama.cpp, or OLLAMA_KV_CACHE_TYPE=q8_0 in Ollama, takes the KV cache for a 32K context from roughly 15 GB down to about 5 GB.

  • 60–70%The saving from Gemma 3 and 4's 5:1 local-to-global attention, with a 1024 sliding window, against pure global attention.
  • ×0.5Q8_0 KV quantisation halves it outright.
  • ~30%Flash Attention shrinks the activation buffer, and Gemma 4's hybrid local/global attention depends on its sliding window.

Unified Memory Needed on macOS weights plus KV at FP16 with Flash Attention, plus 4–6 GB for the OS

Quantisation (weights)8K32K128K256K
26B-A4B Q4 (16.9 GB)24243232
26B-A4B Q8 (26.9 GB)32484864
31B Q4 (18.3 GB)24323248
31B Q8 (32.6 GB)48486496
31B BF16 (61.4 GB)9696128192

Three Known Traps on Apple Silicon Metal and Flash Attention

  • It simply hangs

    Ollama with Gemma 4 and Flash Attention

    v0.20.3

    • Anything over about 500 tokens of prompt hangs the whole thing.
    • Codex CLI's system prompt is around 27K tokens, so it almost always triggers this.
    • The fix is to connect to llama.cpp directly with -fa on -ctk q8_0 -ctv q8_0.
  • Bandwidth went backwards

    The M3 Pro trap

    • Apple cut M3 Pro memory bandwidth from 200 GB/s to 150 GB/s.
    • For some users that made it slower than an M2 Pro on LLM work.
  • A bonus

    The MLX backend

    • On models below 14B it is 20–87% faster than llama.cpp.
    • Ollama 0.19 and later enable it automatically on Macs with 32 GB or more, improving decode by around 93% for most models.

For Comparison: NVIDIA VRAM and Speed Q4_K_M · 4K context · tokens per second

Card8B14B26B MoE31B dense
RTX 3060 12 GB4223–29OOMOOM
RTX 4060 Ti 16 GB503560–70OOM
RTX 4080 16 GB755090–110OOM
RTX 4090 24 GB10475–95140–1507.8 *
RTX 5090 32 GB130–150100–120180+35

* Running 31B Q4 on a 4090 forces part of the model into system RAM, and a workstation with multi-channel DDR5 and a 64-core CPU beats it at 8.8 tok/s. That single cell is the most important in the table: it shows that not fitting is not a small slowdown but a change of order.

What Insufficient VRAM Costs the rule for hybrid inference

Take Qwen 3 8B Q4 on an RTX 4060 with 8 GB: offloading 25 of 37 layers drops it from 40.58 to 8.62 tokens per second — 4.7 times slower — and all it buys is VRAM falling from 7.2 GB to 4.8 GB. As a rule, each additional 25% offloaded to CPU roughly halves throughput, and an agentic workflow with many rounds of tool calling multiplies that gap.

  • 30 seconds becomes 2–3 minutesWhat a 30-second tool-call cycle actually feels like when VRAM is short.
  • ~4 tok/sSpeed with num_gpu=0, CPU only — effectively unusable in an agentic loop.

Claude Code CLI and Copilot CLI both take a local model, over different protocols

ItemClaude CodeCopilot CLI
Native Ollama supportYes, from 0.14 in January 2026Yes, bring-your-own-key, April 2026
ProtocolAnthropic MessagesOpenAI Chat Completions
Minimum context suggested64K128K
Tool callingRequiredRequired, with streaming
Offline modePartialFull, with COPILOT_OFFLINE=true
Suggested local modelsGLM-4.7-flash, Gemma 4 26B-A4Bqwen3-coder, glm-5

How Local Models Score at Agentic Work tool-calling success and multi-step ability

ModelSizeTool callingMulti-step
GLM-4.7-flash30B MoE98%Strong
Qwen3-Coder 30B-A3B30B MoE93%Very strong (SWE 71.3)
Devstral-2 24B24B88%Moderate to strong
Gemma 4 31B Dense31B85%Strong (τ2 86.4)
Gemma 4 26B-A4B26B MoE85% *Moderate to strong
Qwen3 14B14B75%Moderate
Llama 3.1 8B8B50%Weak

Known Tool-Calling Problems in Ollama each one breaks the whole agentic flow

  • Tool calling with Qwen 3.5 35B-A3B is entirely broken on Ollama — the renderer and parser misalign on <think> tags and tool-call prefixes, and repeat_penalty and presence_penalty are silently ignored (ollama/ollama#14493). Connect to llama.cpp directly with --jinja instead.
  • Streaming tool calls with Gemma 4: v0.20.3's streaming path puts tool_calls into the reasoning channel, so Codex and Claude Code fail to parse them entirely. v0.20.5 or later is needed.
  • The default context is far too small: Ollama defaults to num_ctx = 2048, nowhere near enough for Claude Code, whose system prompt is around 27K. Set OLLAMA_CONTEXT_LENGTH=131072 or specify it in the Modelfile.
  • CUDA 13.2 has quality problems with Gemma 4 GGUF; Unsloth recommends dropping to 12.8.

What One Tool-Call Cycle Actually Costs this is where the feel comes from

SetupLatencyConditions
Mac M4 Pro, 24 GB~99 sllama.cpp with Gemma 4 26B Q4, including 27K tokens of prefill
DGX Spark, 128 GB~52 sBlackwell GB10 with Ollama and Gemma 4 31B Q4, via Codex CLI
Cloud Sonnet 4~12 sat high reasoning; Opus 4.5 is around 15 s

Adjust the timeout

Claude Code's default stream_idle_timeout_ms of 600,000 — ten minutes — is only barely enough for local inference. In practice raise it to 1,800,000, thirty minutes, to cover prefill on a Mac. One further piece of community advice: pin your llama.cpp version, since a 3.3× speed regression has appeared between builds.

Mac mini Configurations assuming it runs continuously

TierConfigurationTaiwan priceRuns
EntryM4, 16 GB / 256 GBNT$19,900An 8B agent
Entry plusM4, 16 GB / 512 GBNT$26,9008B, with room for several models
Mid ★M4, 24 GB / 512 GBNT$33,90014B Q4 with 32K context
HigherM4 Pro 12-core, 24 GB / 512 GB, 273 GB/sNT$46,90026B-A4B Q4
Higher plusM4 Pro 14-core / 20-core GPUNT$53,90026B-A4B Q4 and above
Ideal dev boxM4 Pro 14-core / 20-core GPU, 48 GB / 1 TBNT$66,90030B at Q5 or Q8 — over budget

As of 30 April 2026 the Mac mini is still on the M4 and M4 Pro released in October 2024. Education pricing takes off roughly NT$2,000–4,500 depending on model. On laptops: the M5 Max does prompt processing about 2.3 times faster than the M4 Max and decodes tokens about 28% faster, and its new neural accelerators make a marked difference to time-to-first-token — which for agentic use is exactly where the bottleneck sits.

Running Costs and Three-Year Total half inference, half idle · NT$3.5 per kWh

MachineIdle WInference WMonthly powerThree-year total
Mac mini M4, 16 GB3–435–45NT$60NT$22,000
Mac mini M4 Pro, 24 GB25–3078–92NT$140NT$52,000
PC with RTX 4060 Ti 16 GB~80~280~NT$400NT$52,400
PC with RTX 4090 24 GB80–110380–450~NT$750NT$110,000
PC with RTX 5090 32 GB~110~500~NT$900NT$130,000+

For an assistant that runs continuously

For the same money a Windows machine can win on raw specification, but costs five to seven times as much to run around the clock. Between NT$20,000 and NT$50,000 the Mac mini has almost no competition: quiet, low-draw, and markedly cheaper over three years than a PC at the same price. The limitation to note is that an external GPU is not an option — the only way to more usable memory is a model with more of it, and the choice is fixed at purchase.

The middle option worth recommending

The M4 with 24 GB and 512 GB, at NT$33,900, runs 14B Q4 with 32K context and an agent entirely on the GPU, for about NT$2,200 of electricity over three years. If the budget stretches to the NT$46,900 M4 Pro 12-core, its 273 GB/s of bandwidth takes 14B decode from 25 to 50 tokens per second — the point at which an agent goes from usable to smooth, and the largest single step in perceived difference anywhere on the curve.