Making open models work for long-horizon agentic workloads
By Parth Sareen and Zain Hasan (Together AI)
Coding agents have an odd traffic pattern. Every time the agent takes a step, the harness sends the model the entire conversation so far: the system prompt, every tool definition, your request, every file the agent has read, and every command it has run. What comes back is usually a tool call a few dozen tokens long. Then it happens again, with everything from last time plus a little more.
This large input sequence length (ISL) and small output sequence length (OSL) is characteristic of most agentic workloads.If the GPU had to work through that whole transcript from scratch on every turn, long sessions would get slower and more expensive with every step.
Prefix caching is what saves most of that work.The problem is that if you’re not careful it’s also very easy to break the prefix cache.
We come at this problem from opposite ends.Ollama works on harness integrations which means deciding what goes into each request.Together AI works on the inference engine which means deciding what happens when that request arrives.
We kept finding that much of our work on each side is about protecting the cache from things the other side can do to it, so we wanted to write it all down in one place to help agent and infra builders alike!
Every request goes through two phases.The first is prefill: the engine reads the whole prompt in one big parallel pass and produces the first token.GPUs are good at this because it’s a lot of math done all at once which maximizes arithmetic intensity.
Prefill determines how long you wait before any new tokens appear.The second is decode: the model writes the rest of the answer one token at a time, and each new token requires reading back and attending through everything before it.
Decode is limited by memory bandwidth more than by compute, and it determines how fast the text streams out.
A normal chat response spends time in both phases while an agent turn is mostly prefill, because the input is the whole session and the output is usually a short tool call.Reasoning models add thinking tokens on top of that, sometimes a lot of them, but as a session grows into the tens of thousands of tokens the input still dominates.
Each turn the input gets longer, and the output stays roughly the same size — a few tool calls.
So for agents, “how fast is this model?” mostly means “how fast can it get through a long historical account of previous turns?” The way to process a task with lots of previous turns quickly is to just cache it and read it back in rather than recomputing.
The easiest way to picture the KV cache (or even the prefix cache, for that matter) is reading.Imagine that to read each new word on a page, you had to start again from the top.The further down the page you got, the slower you’d go, because you’d keep having to reread everything you’d already read.
That’s a model with no cache.The cache solves this within a single request: the engine keeps the work it did on earlier tokens (stored as intermediate key, value pairs) and computes only the new one.
Prefix caching does the same thing across requests. Turn 7 of an agent session is turn 6 plus a bit more, so if the engine still holds turn 6’s work, it only has to process the new part.
The arithmetic is simple.If each turn adds the same amount, ten turns without caching process 1 + 2 + … + 10 = 55 units of prompt.With caching, they process 10.At a hundred turns, which isn’t unusual for a coding task, it’s 5,050 versus 100.
Real turns vary in size, so treat these as idealized numbers.A cached session isn’t completely free either, since every new token still has to attend to the cached ones.But the pattern holds: without caching, the work you redo grows with the square of the session length.
The catch is that a prefix cache matches from the beginning of the prompt up to the first token that’s different, and stops there. Everything after that first difference has to be processed again.
Adding to the end of the prompt costs nothing extra. Changing anything earlier, even a single character, means re-processing everything after it. If the change is near the top, you’ve effectively thrown away the whole session and now must rebuild the cache. Almost everything else in this post follows from that one rule.
The harness decides where that first change lands. The engine decides whether the cache is still there when the next request shows up. We’ll start with the harness.
On the Ollama side, we shipped the ability to connect your favorite harness through the ollama launch command. So you can connect harnesses like Claude Code, Codex, Pi (and more) with open models like DeepSeek-V4.1-Flash and GLM-5.3 and maintain your existing workflows.
Connecting a harness to an API might seem easy, but there are many layers of translation and varying expectations between a harness and a model. This often is the cause of cache breakages (incurring time and monetary costs), unexplainable agent behavior, and