Understanding Token Spikes in Long AI Coding Sessions: Context Compaction vs. Prefix Caching

    This article explains why a short AI coding task can exhaust a full five-hour usage window. It breaks the issue down into three overlapping causes: automatic context compaction invalidating the cached prefix, inconsistent context window labeling across model catalogs, and repeated context transmission amplified by tool-call chains. It also offers a simple self-check to distinguish mechanism-driven consumption from usage patterns, plus practical governance steps such as splitting sessions, stabilizing the prefix, and building usage observability.

    1. The Symptom: A 7.5-Minute Task, a 5-Hour Window

    A coding task flagged for the highest compute tier ran for only seven and a half minutes, yet consumed an entire five-hour usage window. That contrast points to the real issue: consumption is driven not by how long the model “thought,” but by how much context each request carries and at what price that context is billed.

    The conclusion first: this kind of anomaly is most likely not a defect you can fix by changing a single line of code. It is the result of three normally unrelated bills opening and stacking at the same time inside a long session. Understanding these three bills is more valuable than asking whether a specific tool has a bug.

    2. Bill One: Auto-Compaction Shatters the Cached Prefix

    2.1 Long sessions inevitably trigger compaction

    Mainstream AI coding tools generally adopt a “compact as the window fills up” strategy: automatic compaction starts at roughly 90% context occupancy, and forced compaction kicks in at around 95%. Compaction replaces earlier conversation turns with a summary, usually surfaced in the UI as “compacting context” or “context compacted.”

    The key point is that this mechanism typically only allows “compact earlier,” not “delay or disable.” Even where internal switches exist, they are for the tool’s own internal threads and are not reachable through user-side configuration. So as long as a session is long enough, compaction will happen.

    2.2 Prefix cache hit conditions are extremely strict

    In the billing structure of model calls, cached input carries a significant discount relative to normal input, often an order of magnitude apart. But a cache hit requires that the beginning of the request be byte-for-byte identical to the most recent request, including the system prompt, tool definitions, and early conversation, which together form the cache key.

    Compaction does exactly the opposite: it rewrites the early conversation. Once the prefix changes, the cache key is invalidated, and in the first request after compaction, hundreds of thousands of tokens are all billed as normal input. The few tokens saved cannot make up for the lost discount.

    2.3 Compaction happens repeatedly

    The summary itself takes up space, so as the task progresses the context quickly approaches the threshold again, triggering a second and third compaction. The “verbose sessions being repeatedly auto-compacted” that users report is exactly this: every compaction means paying the full prefix again.

    1. Normal long session: req1[HIT] req2[HIT] req3[HIT] ... low unit price
    2. After compaction: reqN[ALL MISS] -> unit price jumps an order of magnitude

    3. Bill Two: Hidden Amplification from Inconsistent Window Definitions

    Model catalogs from different sources may label the available window differently. The same model can be marked with a smaller window in one version of a catalog and a larger window in another. A smaller window label triggers compaction earlier; a larger label means each request carries more context. The former increases the number of compactions, while the latter raises the per-request price, and both push total consumption up. In addition, manually increasing the window parameter only delays compaction; it does not reduce the repeated transmission of total tokens.

    4. Bill Three: Repeated Transmission and Tool-Call Amplification in Long Sessions

    Beyond compaction, there are two common amplification sources. First, every turn carries the full history and tool definitions, so the cumulative transmission grows linearly or even faster as turns increase. Second, in tool-call chains, intermediate results such as file reads, retrievals, and tests are continuously fed back into the context, further raising the baseline.

    5. Is It a Mechanism Problem or a Usage Problem?

    A simple self-check: divide total consumption by the number of effective output turns. If the average input per request stays close to the window limit over time and is accompanied by repeated compaction notices, the cause is mainly stacked mechanisms. If the average input is small but the number of turns is extremely high, it leans more toward usage patterns. Most “abnormal bills” are a combination of both.

    6. Actionable Governance Practices

    1. Split sessions: break a large task into smaller tasks that can be accepted independently, and keep a single session within half of the window.
    2. Stabilize the prefix: fix the system prompt and tool set, and avoid frequent mid-session changes that repeatedly invalidate the cache.
    3. Converge promptly: write staged conclusions to external files or documents, and reference them instead of keeping long history in context.
    4. Control feedback: trim retrieval and file-read results, keeping only the necessary fragments.
    5. Build observability: use the cloud platform’s logging service and monitoring alerts to build dashboards and threshold alerts for request volume, input tokens, and cache hit rate.
    6. Attribute cost: tag by project on the container platform or task scheduler side, and aggregate usage to task granularity for easier review.

    7. Conclusion

    This “short task, high consumption” phenomenon is essentially a structural conflict between context compaction and the prefix cache discount, triggered simultaneously in long sessions. For users, the most effective approach is not to wait for a fix, but to pull the consumption curve back into a predictable range through session splitting, prefix stability, and usage observability.