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 Count | No Caching (Cumulative Throughput) | With Caching (New Throughput) |
|---|---|---|
| 10 Turns | 55 units (1+2+...+10) | 10 units |
| 100 Turns | 5,050 units | 100 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.
- Harness Request Configuration
- Prefix Cache Matching Check
- Cache Hit: Only New Part Prefill
- Cache Miss: Entire Prefill Re-execution
- Decode Stage
- 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.
- Obvious Approach: Add definitions to the tool list. However, the tool list is located at the front (top) of the conversation history, so the prefix changes, and the subsequent tens of thousands to hundreds of thousands of tokens are reprocessed.
- Better Approach: Place definitions at the end of the prompt (behind the conversation history). Since this is where new content is added anyway, there is no cache cost.
- Constraint: Not all models support intermediate tool definitions. Some chat templates do not expect tool definitions in the middle of the conversation, leading to errors in calls.
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
- Current Timestamp
- Git Status
- Working Directory Path
- Dynamic System Prompt Injection
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:
- Ollama: Provides harness integration via `ollama launch`, documents tool search and lazy loading patterns
- Together AI: Prefix cache routing, cache-friendly endpoint design, optimization for long context processing
- Common: Share model-specific chat template constraints, classify cache invalidation patterns
Practical Application Checklist and Implications
For Harness/Agent Developers
- [ ] Prohibit variable elements like timestamps, git status, and paths in the system prompt
- [ ] Place tool definitions at the bottom of the prompt (behind the conversation history) whenever possible
- [ ] Pre-test whether model-specific chat templates support intermediate tool definitions
- [ ] Utilize standard integration paths like `ollama launch` to minimize translation layers
For Inference Infrastructure Engineers
- [ ] Ensure session affinity: Route identical sessions to the same GPU
- [ ] Prefix cache indexing: Fast lookup based on prompt prefix hash
- [ ] Cache eviction policy: Weight-based eviction reflecting session length and remaining turn predictions
- [ ] Build a monitoring dashboard for cache hit rates, prefill latencies, and GPU memory usage
Considerations When Choosing Models
| Model | Features | Chat Template Constraints |
|---|---|---|
| DeepSeek-V4.1-Flash | Open model, supports long context | Check for support of intermediate tool definitions |
| GLM-5.3 | Open model, optimized for agent workloads | Same |
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.