Open Model Agent Workload Optimization: Prefix Caching and Harness-Engine Collaboration Strategy

Together AI and Ollama have unveiled the necessary optimization strategies on both the harness (client) and inference engine (server) sides to maximize prefix caching efficiency in long-horizon agent workloads. Agents resend the entire conversation history at each turn, resulting in a long input sequence length (ISL) and a short output sequence length (OSL), making the prefill stage dominant. Caches are only valid from the beginning of the prompt to the first point of difference, so any changes to top elements like system prompts or tool definitions invalidate the entire cache. The harness should place tool definitions at the bottom of the prompt and avoid variable elements like timestamps, while the engine should route identical session requests to the GPU holding the cache.

Specificity of Agent Workloads: Prefill-Centric Traffic Patterns

Coding agents exhibit fundamentally different traffic patterns than typical chat. At each turn, the system prompt, all tool definitions, user requests, the files read so far, and all executed commands are resent. The return is usually a single tool call of several dozen tokens. Thus, the input sequence length (ISL) is large and the output sequence length (OSL) is small, which characterizes most agent workloads.

"Coding agents send the entire conversation history to the model at every step. This includes the system prompt, all tool definitions, requests, read files, and executed commands. The return is usually a single tool call of several dozen tokens."

Inference is divided into two stages. In the prefill stage, the engine reads the entire prompt in a single parallel pass and generates the first token. The GPU can handle massive computations simultaneously, maximizing arithmetic intensity, making it strong in this stage. In the decode stage, tokens are generated one by one, and each token must attend to all previous tokens, which is constrained by memory bandwidth. While both stages take time in general chat, agent turns have the entire session as input and a short tool call as output, making prefill dominant. Even if the inference model adds thinking tokens, if the session grows to tens of thousands of tokens, the input still prevails.

How Prefix Caching Works and Break Conditions

Prefix caching enables cache reuse between requests. Turn 7 is a slightly modified version of Turn 6, so if the engine retains the work (KV cache) from Turn 6, it only needs to process the new part. Assuming the same amount is added at each turn:

Turn CountNo Caching (Cumulative Throughput)With Caching (New Throughput)
10 Turns55 units (1+2+...+10)10 units
100 Turns5,050 units100 units

Cached sessions are not completely free. Each new token must attend to the cached tokens, so there are still memory bandwidth costs. However, the pattern that without caching, the rework increases in proportion to the square of the session length remains unchanged.

Critical Constraint: Prefix caching matches only from the beginning of the prompt to just before the first different token. Everything after that must be reprocessed. Adding to the end of the prompt incurs no cost, but if even a single character is changed in the front part, the entire subsequent part must be reprocessed. If the change is near the top, the entire session must be discarded, and the cache must be rebuilt from the start. This one rule derives almost all optimization strategies.

  1. Harness Request Configuration
  2. Prefix Cache Matching Check
  3. Cache Hit: Only New Part Prefill
  4. Cache Miss: Entire Prefill Re-execution
  5. Decode Stage
  6. Response Return

Optimization on the Harness Side: Tool Definition Placement and System Prompt Management

On the Ollama side, the `ollama launch` command allows connecting harnesses like Claude Code, Codex, and Pi with open models like DeepSeek-V4.1-Flash and GLM-5.3. While the harness-API connection appears simple, the differences in translation layers and expectations lead to cache invalidation (time and cost loss), inexplicable agent behavior, and harness errors.

The latest harnesses have introduced tool search functionality. When needed, the agent searches for tools, loads definitions, and calls them. For example, a personal assistant agent searches for "calendar" to load and call the `save_calendar` definition. From a caching perspective, the key is where the definition is located.

Therefore, harness builders need to know whether model-specific chat templates support this, and it should be abstracted so that users do not need to know.

Top Changing Elements That Ruin Caching

If any of these change at each turn, the cache breaks from the first turn every time. The harness should maintain a static system prompt, and variable information should be placed at the bottom of the prompt (near user messages).

Optimization on the Inference Engine Side: GPU Routing and Cache Maintenance

Together AI must ensure that the cache is alive when requests arrive on the inference engine side. The key is to route consecutive requests of the same session to the same GPU holding the cache. When the engine receives a request:

1. Prefix Cache Lookup: Search for cache entries matching the prompt prefix 2. On Cache Hit: Identify the GPU holding the corresponding KV cache block 3. Dispatch the request to that GPU (random GPU on cache miss) 4. Prefill: Skip cached parts, compute only new tokens 5. Decode: Stream generated tokens

The engine must also carefully design the cache eviction policy. Simple policies like LRU can prematurely delete caches for long sessions, so a policy considering session length, recency, and expected remaining turns is necessary.

Collaboration Between Together AI and Ollama: Cache Protection Strategies from Both Ends

The two companies discovered that protecting the cache on both the harness (request configuration) and engine (request processing) sides is interlinked. If the harness touches the top of the prompt, the engine cache is invalidated, and if the engine routes requests incorrectly, the harness's cache-friendly configuration becomes useless. This has been documented in a single document to serve as a reference for both agent builders and infrastructure builders.

Key collaboration points:

Practical Application Checklist and Implications

For Harness/Agent Developers

For Inference Infrastructure Engineers

Considerations When Choosing Models

ModelFeaturesChat Template Constraints
DeepSeek-V4.1-FlashOpen model, supports long contextCheck for support of intermediate tool definitions
GLM-5.3Open model, optimized for agent workloadsSame

In agent workloads, "how fast is the model?" is essentially synonymous with "how quickly can it prefill long histories?". Prefix caching determines the answer to this question, and if both the harness and engine do not protect the cache in their respective roles, the square law costs cannot be avoided.

Verified figures

Our take

The collaboration between Together AI and Ollama to jointly document cache protection strategies across the harness-engine boundary is an important milestone demonstrating the maturity of the open model agent ecosystem. Particularly, the insight that a seemingly minor design choice regarding tool definition placement can drastically affect costs per session is high-value knowledge immediately applicable to practitioners.

Open questions

Source

Back to catalog