Skip to main content

Understanding Prefix Caching in Hybrid LLMs

· 87 min read
Vadim Nicolai
Senior Software Engineer

What is prefix caching: one agent conversation through Qwen3.5-0.8B with real token IDs and chained SHA-256 block names, why reusing cached keys and values is exact, what full-attention, sliding-window and recurrent layers must keep to resume, one agent retry measured on an M4 Pro with llama.cpp, where llama.cpp saved its checkpoints, where vLLM, SGLang and llama.cpp save states, cache-aware routing, and the resume-point cost in 19 open models

The whole post on one page: one real agent prompt traced through Qwen3.5-0.8B's chat template, token IDs and chained block names, why reuse is exact, what each kind of layer must keep, and the measured retries.

An agent sends its whole conversation again on every tool call: the system prompt, the tool schemas, every earlier tool result and reply, and then the one new message at the end. Prefix caching is what makes that affordable. The server keeps the keys and values it computed for the previous request and runs only the tokens it has not seen yet.

On a model where every layer is full attention, that works at almost any token. I replayed an agent retry in llama.cpp on Qwen3-0.6B: a 20-turn chat, cut back to 5, 10, 15 or 19 turns, then a different tool result. Every time, it computed only the 514 new tokens.

Then I ran the same retries on Qwen3.5-0.8B, where 18 of the 24 layers are Gated DeltaNet layers that keep one fixed-size state instead of every token's keys and values. With llama.cpp's default settings, the retries recomputed between 5 and 5,600 tokens the server had already processed, depending on how far back the last state it had saved was. Through the raw completion endpoint, where llama.cpp saves states only near the end of each prompt, a retry at token 9,000 of a 12,000-token prompt recomputed all 9,064 tokens: 2.46 s, against 52 ms on the dense model. Cold, the hybrid was the faster of the two, 3.46 s against 5.53 s for all 12,000 tokens.

This is not a llama.cpp quirk. A recurrent state cannot be rewound, and a sliding window has already dropped the tokens a resume point needs, so every serving stack has to decide where to save them and how much memory to spend on it. And these layers are spreading: of the 19 open models I compare in this article, 12 need such a saved resume point, including 9 of the 13 first listed in 2026.

More concretely, this article walks through:

  1. How prefix caching works for full attention, from scratch: why reusing cached keys and values is exact, and why a cache block's identity has to hash every block before it, as in Narwhal, the open inference server from Athrael's GPU inference course.
  2. How a fleet of engines finds a cached prefix. In a replay of an agent trace over 4 engines, cache-aware routing reused 79.3% of the prompt tokens, against 50.6% for round-robin.
  3. Why sliding-window and recurrent layers (Gated DeltaNet, KDA, Mamba-2) can only resume where a window or a state was saved, with a from-scratch DeltaNet recurrence resumed from a snapshot, and what vLLM, SGLang and llama.cpp save for them.
  4. What one resume point costs in 19 open models, from their config.json files, and measurements on an M4 Pro.

If you already know how prefix caching works for full attention, feel free to jump straight to Section 4 for the layers that break it, or to Section 7 for the architecture comparison.

Prefix caching across 19 open LLMs: the size of one saved resume point (recurrent state snapshot or sliding-window tail) per sequence, from each model's config.json, sorted by first listing; 12 of 19 need one

Understanding LLM Inference: Why Token 32,768 Costs 20x Token 1

· 61 min read
Vadim Nicolai
Senior Software Engineer

What is LLM inference: one token traced through the Qwen3-4B forward pass with every tensor shape and real token IDs, prefill and decode with a growing KV cache, the generation loop in code, the bytes and FLOPs that make decode memory-bound on an M4 Pro, and what the KV cache adds at long context

This week brought two very different answers to the same problem. Qwen4 uses Gated DeltaNet in 30 of its 40 layers. Amazon's Ergo, a 1.1T-parameter open-weight model, uses sliding-window plus grouped-query attention. The problem both are solving: every token an LLM generates re-reads the keys and values of every earlier token, in every layer.

In my experience, building something from scratch is still the best way to see what it costs. So I built the decode step in numpy and measured the tax on my MacBook (M4 Pro, 24 GB): six attention designs, one Qwen3-4B-shaped model, contexts from 0 to 32,768 tokens. Nothing was downloaded; random weights at real model shapes time exactly like trained ones. The full script is at the end.

Every token you generate re-reads the entire conversation: what token 32,768 costs in units of token 1, at 32 concurrent users

What token 32,768 costs in units of token 1, with 32 users sharing the server:

  1. Multi-head attention (MHA): 78x, and an 18.0 GiB KV cache per user.
  2. Grouped-query attention (GQA, 8 KV heads), the default of most open models: 20x, 4.5 GiB.
  3. Multi-head latent attention (MLA), DeepSeek's design: 6.4x, 1.27 GiB.
  4. A 3:1 DeltaNet hybrid, Qwen4's layer ratio: 4.9x, 1.15 GiB.
  5. A 4K sliding window, the building block of Ergo and Gemma: 3.4x, 0.56 GiB.
  6. Gated DeltaNet (linear attention) alone: 1.0x, 0.04 GiB.

Then I ran the config.json of 19 open models through the same arithmetic and drew every one of their layers. Per 128K-token conversation, Llama 3.1 70B keeps 40 GiB of cache, Amazon Ergo 8.1 GiB, Qwen4 2.6 GiB and Nemotron 3.5 Lightning 0.79 GiB. Of the ten models from the second half of 2026, five keep some full-attention layers and five have none at all. I see a trend :) It is Raschka's 50/50 split, re-derived from config.json alone.

I think the KV cache, not FLOPs, is now the main design constraint of open LLMs. Every architecture trend of 2026 shrinks the bytes kept per token; none of them makes the matrix multiplications cheaper.

One thing the first version of this post got wrong: sparse attention does not skip the old tokens. Its indexer still scores every one of them through a small 128-dim key, and at 128K context that scan is 44 to 90% (!) of what a sparse model reads per token. I built that one from scratch too.

Btw, for a single user the tax is only 1.6x on a GQA model at the bandwidth floor. It explodes when a server batches users, which is what everyone paying for inference does. More concretely, the rest of this post walks through:

  • the measurements behind each number, and the bandwidth floor under them,
  • an architecture gallery of what each design stores per token,
  • the 19-model simulation, with a map of every layer,
  • the indexer, measured from scratch,
  • the full-to-hybrid trend, and how this week's releases read through it,
  • what a disaggregated server moves and prices, checked against Narwhal's docs and code.

I made a lot of figures for this one. The one-page explainer at the top is what inference is, from scratch: one token traced through a real model (Qwen3-4B's config shapes and its tokenizer's IDs), prefill vs decode with the KV cache growing step by step, the loop in code, and why decode cost comes down to bytes divided by memory bandwidth (it is an SVG, so zoom in as far as you like).

Context7 LangGraph Test: Claude Code on APIs It Never Saw

· 12 min read
Vadim Nicolai
Senior Software Engineer

I gave Claude Code three tasks on upstash-redis 1.9.0, an SDK release from November 2026. That release is newer than the model's training, and the SDK was not installed where the agent worked. With web access denied and no docs source, it passed 0 of 9 runs. With Context7, it passed 7 of 9. In the baseline, 6 runs declined to write code and 3 wrote an older answer. On an API it already knew (LangGraph 1.2's interrupt), it passed every time either way. In that Context7 LangGraph test, Context7 confirmed the answer for about 6 cents more per run. Setup details are below.

Context7 LangGraph test results: 0 of 9 runs passed without Context7 and 7 of 9 with Context7 on upstash-redis 1.9.0

Agent Eval Harness: 6 Essential ADLC Steps to Build First

· 17 min read
Vadim Nicolai
Senior Software Engineer

Six of the eight agent vendors I surveyed ship an evaluation runner as product code. IBM, Salesforce, AWS, Databricks, Google and Microsoft each give you a command or function that runs your agent against a test set and scores it. Only one of them writes down the rule that makes the runner useful, and that rule lives in a sample skill, not in the product.

AI Agent Design: 5 Essential Scoping Rules in the ADLC

· 10 min read
Vadim Nicolai
Senior Software Engineer

AI agent design is the phase of the agent development lifecycle (ADLC) with the least product code behind it. In part 01 I read the lifecycle pages and public repos of eight vendors. For design, only Salesforce ships an artifact you can write and check in. Everywhere else, design survives only as runtime settings: a tool list, an autonomy mode, a call or spend cap.

How AWS threat-designer decides its LangGraph agent is done: a deterministic validate node, not the model, and a quiet floor of 25 threats

· 8 min read
Vadim Nicolai
Senior Software Engineer

Nothing the model says ends a threat-designer run. When the agent stops calling tools, it has not finished. It has only reached a plain Python function that decides whether it may finish.

Through a precondition buried in a tool, that function also rejects any catalog smaller than 25 threats (constants.py L124). I found that rule nowhere stated as a product requirement.

My claim is that this is the right design for deciding who controls the exit. It is also a crude way to define what "done" means. If you believe the model should orchestrate its own loop, you will disagree with the first half.

How GitLab runs a stateless LangGraph agent on a custom checkpointer

· 14 min read
Vadim Nicolai
Senior Software Engineer

GitLab Duo's agent service, the Duo Workflow Service behind GitLab Duo Agent Platform, writes every LangGraph checkpoint into GitLab's own Postgres, yet the service has no connection to that database. Each checkpoint read and write travels down the user's own gRPC stream. The client (an IDE, the CLI or Workhorse) makes the HTTP call to GitLab and hands back the answer.

A LangGraph agent service can keep nothing about a session between requests if a custom LangGraph checkpointer writes into the database the product already has. Inside a request, it still holds the whole run in memory.

The Duo Workflow Service (DWS) pays for this with a second copy of LangGraph's persistence layer. That price is right for GitLab. For most teams it is wrong, for the reason in the last section.

On 2026-11-21 I measured 60 big-company public repos for LangGraph depth. Of the 18 deep users, GitLab's ai-assist ranked second, with 56 production files importing LangGraph and 61 checked-in graphs. Five of the 18 run their product on a saver they wrote, and only GitLab relays checkpoint writes through the client.

Everything I say about DWS comes from reading ai-assist's main branch as of 2026-11-23. I did not run DWS, did not call GitLab's API, and measured no latency.

How Kibana pauses a LangGraph agent without a checkpointer

· 13 min read
Vadim Nicolai
Senior Software Engineer

Elastic's human-in-the-loop tutorial compiles the graph with a MemorySaver and resumes with a Command that carries the user's choice (Rengifo (2026)). Elastic's shipped Agent Builder agent uses neither a checkpointer nor a resume Command.

On 2026-11-21 I measured 60 big-company public repos and found 18 that use LangGraph deeply. Kibana is the only one of them that pauses for a human without a checkpointer. The 16 others that pause all use interrupt and a resume Command.

I also collected 1,187 AI-engineering postings on 2026-11-22. The 94 that mention LangGraph ask for human-in-the-loop or durable execution 28% of the time, against 9.8% of the other postings. Those counts lean low, for two reasons: a mention is often one item in a "LangGraph, CrewAI or similar" list, and I truncated long postings.