The AI Gazette

Listen to this story

5 min · read by an AI voice

Local modelsContext and memory

Together AI with Ollama explain what breaks the prefix cache in long agent sessions

A coding agent sends the whole conversation again on every turn, and prefix caching is what keeps that affordable. In a joint post, Together AI, which runs an inference service, and Ollama, which connects coding harnesses to open models, set out what each side has to get right so a session stays fast at turn ninety.

Every step an agent takes is a new request that carries everything so far: the system prompt, every tool definition, the task, every file read and every command run. What comes back is usually a tool call a few dozen tokens long. A model handles a request in two phases. Prefill reads the whole prompt in one parallel pass and is limited by compute; decode then writes the answer one token at a time and is limited by memory bandwidth. An agent turn is almost all prefill, so for agents the speed of a model mostly means how fast it gets through a long history.

The engine avoids redoing that work by keeping the intermediate results for earlier tokens, the KV cache, and prefix caching keeps them across requests: turn 7 is turn 6 plus a little, so only the new part needs processing. The post gives the idealized arithmetic. Ten turns without caching process 1 + 2 + ... + 10 = 55 units of prompt, against 10 with it; a hundred turns process 5,050 units against 100. The catch is that a prefix cache matches only from the start of the prompt up to the first token that differs. Adding to the end costs nothing extra, while changing a single character near the top means processing the whole session again.

Most of the harness advice follows from that rule. A tool loaded during the session should be added at the end of the conversation rather than to the tool list near the top, although some models' chat templates fail on a tool definition in mid-conversation. Values that change every turn, such as a timestamp, the git status or the working directory, do not belong in the system prompt; they go once into the task message. Ollama found that GLM-5.3-Flash writes the reasoning effort into the first system block, so raising effort mid-task re-processes the whole session; the post suggests picking an effort level at the start, or compacting first. A cut-down tool result should say it was cut, for example lines 1 to 100 of 640, or the model edits code it never saw. And a subagent that inherits the full history reuses the parent's cache but still pays to attend over it and holds GPU memory the whole time, so a reviewer that needs two files may be better off with a short brief.

On the engine side, the cache only helps if the next turn reaches the machine that holds it. Together routes either by a key the client sends, such as prompt_cache_key, or by hashing the prefix and picking the node with the longest cached match; the second way is one more reason to keep the system prompt identical across sessions, so a new session can find its setup already cached. A busy warm node may still lose to an idle cold one. While an agent waits minutes for tests, the cache can move to CPU memory and come back for the next turn. Cache memory is handed out in small blocks as needed (PagedAttention), the running batch is rebuilt on every pass (continuous batching), and prefill and decode run on separate clusters with the cache shipped between them over RDMA. The post's point for harness builders is that memory for the cache, not compute, usually limits how many requests a GPU can serve at once.

The same rule turns up in OpenAI's guide for its new GPT-6.2 models, covered above, which asks applications to keep the start of each request unchanged so earlier reasoning and cached input stay usable. A provider can route every turn perfectly and still lose the cache when the harness changes the first line of the prompt.

From other editions

Say hi.