Why your AI coding agent keeps reprocessing the same context

monitor screengrab

AI coding agents are not chatbots. One developer instruction can trigger dozens of sequential model calls as the agent inspects files, runs tests, reads failures, and decides what to do next. That loop creates a systems problem most builders overlook: how much of the context the model already processed can actually be reused?

What the production data shows

A study of sampled GitHub Copilot coding-agent traces from June 2026 analyzed roughly 13 million sessions, 3.2 million users, 761 million LLM calls, and 95 trillion tokens. The key finding: KV-cache reuse averaged around 90% within a single agent turn but dropped to about 55% across turn boundaries.

About 87% of LLM calls in the workload were agent-initiated, not user-initiated. One developer message generates a long chain of model invocations, which makes cache reuse a first-class concern rather than an infrastructure footnote.

Two events that tank cache reuse

Context compaction happens when a long-running session accumulates too much history and the agent summarizes it down. Useful for token management, but the study found compaction was associated with cache cold-start behavior. Changing the context structure means previously computed state may no longer be usable.

Model switching is worse. Even when the textual context is identical, cache state does not transfer across models. The study reported that after a model switch, only about 8% of context remained cached in the analyzed workload. Switching models mid-session to save a few cents on a simpler subtask can cost more in reprocessing than it saves.

3D rendered ai text on dark digital background

Practical design rules

  • Keep the prefix stable. System instructions, tool definitions, and repository configuration should not be regenerated or reordered between model calls unless the content actually changed.
  • Treat compaction as a workload event, not a free optimization. Compact when retained context stops providing value, not on an arbitrary schedule. Preserve decisions, modified files, known failures, and remaining work. Drop raw logs and duplicate tool output.
  • Avoid model switches without a strong reason. Better capability or a provider failover are legitimate reasons. A marginal cost difference mid-session probably is not.
  • Separate persistent agent state from conversation history. Store task state explicitly so compaction does not have to reconstruct the entire project context from scratch.
  • Filter large tool outputs. Give the model what it needs for its next decision, not the full compiler log.

What to measure

If you are building agent infrastructure, cache hit rate, prefix length, compaction frequency, model switch count, and tool calls per turn should all be tracked together. Token reduction alone is not a useful signal. A strategy that produces a smaller context but forces more repository rediscovery may cost more overall than one that retains state and reuses it.

The full article includes C# code examples for a session-aware model router and an explicit agent state record. Worth reading if you are designing agent orchestration at any scale.

Stay on top of AI & Automation with BizStack Newsletter
BizStack  —  Entrepreneur’s Business Stack
Logo