Why Long Sessions Cause Abnormal Token Consumption: Causes and Optimization

    This article explains why long AI coding sessions can consume far more tokens than expected. It breaks down three overlapping cost drivers: auto-compaction breaking prefix cache, the higher unit cost and amplification of long contexts, and loop or retry calls. It also outlines how to identify abnormal consumption and a practical optimization checklist covering cache-friendly prompts, task segmentation, session resets, tighter loops, observability, and cost isolation.

    1. A Striking Contrast to Start With

    Seven and a half minutes of actual compute time, yet roughly five hours of quota consumed. That number is more telling than any explanation, and it points in one direction: the consumption does not come from “more compute,” but from a change in how usage is billed.

    Here is the conclusion up front: this is most likely not a “fix one line of code” kind of defect. It is the result of three normally unrelated bills opening and stacking at the same time in a long session. Understanding these three bills is more valuable than hunting for a single culprit.

    2. Mechanism Breakdown: Auto-Compaction Is Where the Chain Starts

    2.1 Trigger Conditions and Thresholds

    Context management in mainstream AI coding tools generally uses a “threshold-triggered” strategy: compaction starts when context usage reaches about 90% of the window; at around 95% a hard limit kicks in and compaction is forced regardless of user configuration. The window size is determined by the tool’s built-in model catalog, and different catalog versions may report different values. This explains why the same tool, with catalogs cached at different times, can trigger at inconsistent points.

    2.2 What Compaction Does

    Compaction essentially replaces earlier conversation turns with a summary, usually shown in the UI as a “compacting context” to “context compacted” status. The summary itself takes up space, so as the task continues the context approaches the threshold again, leading to repeated compaction.

    2.3 Why Users Cannot Turn It Off

    Among common configuration options, one parameter can only make compaction happen earlier; it cannot delay or disable it. Another parameter can raise the window limit, but that limit is still constrained by the model catalog. There may be an internal disable switch at the code level, but it typically serves only the tool’s own internal threads and is not reachable through user configuration. So as long as a session is long enough, compaction will happen.

    3. First Bill: Compaction Shatters the Prefix Cache

    This is the most counterintuitive part. In the billing structure of model calls, prefix cache hits usually enjoy a significant discount. A typical public rate card looks like this: normal input has a high unit price, cached input is an order of magnitude cheaper, and output has the highest unit price.

    The conditions for a prefix cache hit are strict: the beginning of the request must be byte-for-byte identical to the most recent request. The system prompt, tool definitions, and early conversation 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 those hundreds of thousands of tokens are all billed as normal input. The small number of tokens saved cannot make up for the lost discount.

    Illustration (pseudocode):

    1. if context_usage >= 0.90 * window:
    2. history = summarize(history) # prefix is rewritten
    3. cache_key = hash(prefix) # differs from last time -> cache miss
    4. billable = full_input_tokens # full amount billed as normal input

    4. Second Bill: Unit Cost and Amplification of Long Contexts

    Even without compaction, long sessions are inherently more expensive: every turn carries the full history, input tokens grow linearly with the number of turns; output unit price is usually higher than input; and once repeated compaction begins, the amplification effect stacks, so the bill rises non-linearly.

    5. Third Bill: Loop Calls and Retries

    AI coding tools often include a “think—call—observe” loop. If the task description is vague or constraints are missing, the model may trial-and-error repeatedly; automatic retries and timeout replays on the tool side can duplicate several more full-prefix requests without the user noticing. This is most dangerous when combined with the first bill: every retry pays again for a full input that misses the cache.

    6. How to Tell Whether Consumption Is Abnormal

    • Look at the time distribution: is consumption concentrated in the later part of a long session, around when compaction prompts appear;
    • Look at the request structure: is a single request’s input tokens far larger than the visible conversation length;
    • Look for retry traces: are there duplicate records of the same request in the logs;
    • Look at task complexity: was the actual change very small yet accompanied by many rounds of exploration.

    7. An Actionable Optimization Checklist

    1. Cache-friendly: keep the system prompt and tool definitions stable, and avoid frequently changing the beginning of the conversation mid-session;
    2. Segment the work: split large tasks into separate sessions, wrap up at the end of each segment, and avoid entering the compaction zone;
    3. Reset promptly: after a long task is done, actively start a new session instead of continuing to ask follow-ups in the same context;
    4. Tighten the loop: give clear constraints and acceptance criteria to reduce trial-and-error and retries;
    5. Observability: use monitoring alerts and log services to set threshold alerts on token usage;
    6. Cost isolation: on the enterprise side, use container platforms and resource quotas for allocation and limits.

    8. Conclusion

    The essence of abnormal consumption is the resonance of three bills—auto-compaction, prefix cache invalidation, and loop retries—within a long session. It is more of a structural cost at the mechanism level than a single-point defect. For developers, rather than waiting for a fix, it is better to actively control costs with segmentation, cache-friendly practices, and observability; for enterprises, incorporating usage monitoring and quota governance into platform capabilities is the sustainable approach.