Token Consumption Anomalies in Long Sessions: Causes and Governance

    This article explains why a short coding task can consume an entire multi-hour usage window in AI coding tools. It breaks down three overlapping cost drivers—automatic context compaction, strict prefix caching, and drifting context window definitions—then outlines a four-step diagnostic path and practical governance measures such as session splitting, explicit context budgets, stable prefixes, observability, and quota guardrails.

    1. The Symptom: A Short Task Consumes an Entire Quota Window

    A coding task that took seven and a half minutes of actual work consumed a full five-hour usage window in an AI coding tool. That contrast is itself the most valuable diagnostic clue: consumption is decoupled from the amount of work a human perceives, which indicates the dominant variable in billing is not task complexity but how session state is managed.

    The conclusion should come first: this kind of anomaly is most likely not a defect that can be fixed by changing a single line of code. It is the result of three separate accounts, normally unrelated, being opened and stacked at the same time inside a long session. Any single-point attribution—model price increases, a client defect, or account throttling—will lead to misdiagnosis and ineffective mitigation.

    2. Mechanism Breakdown: Why Automatic Compaction Becomes the Most Expensive Step

    2.1 How Compaction Is Triggered

    Clients of mainstream AI coding tools generally include automatic context compaction: summarization starts when token usage reaches a certain ratio of the window (commonly 90%), and forced compaction occurs at a higher ratio (commonly 95%). In the interface, this usually appears as two messages: Compacting context and Context compacted.

    The key constraint is that these mechanisms typically only allow triggering earlier; they do not allow postponing or disabling compaction. In other words, as long as the session is long enough, compaction will happen, and users cannot avoid it through configuration.

    The configuration relationship can be understood schematically as follows (this only expresses general semantics, not a specific product interface):

    1. effective_trigger = min(configured_ratio, catalog_ratio) * context_window
    2. hard_limit = 0.95 * context_window
    3. # configured_ratio can only be lowered, not raised; hard_limit cannot be bypassed

    2.2 First Account: The Prefix Is Broken and the Cache Discount Drops to Zero

    The billing structure of model calls usually distinguishes “regular input” from “cached input,” with the latter priced significantly lower (an order of magnitude difference is common in the industry). The conditions for a prefix cache hit are very strict: the beginning of the request must be byte-for-byte identical to a recent request, and the portion forming the key typically includes the system prompt, tool definitions, and early conversation.

    Compaction does exactly the opposite—it rewrites the early conversation into a summary. Once the prefix changes, the cache key becomes invalid, and in the first request after compaction, those hundreds of thousands of tokens are all billed as regular input. Trading the discount on the entire prefix to save a small number of tokens is a net loss.

    Worse, compaction happens repeatedly: the summary itself occupies context, and as the task continues, the threshold is approached again quickly, forming a cycle of “compact—pay in full again—compact again.” Feedback in user communities about “verbose histories being repeatedly compacted automatically” is essentially the external manifestation of this cycle.

    2.3 Second Account: Window Definition Drift Makes Thresholds Unpredictable

    For the same model, the context window labeled in the built-in catalog may differ across versions (for example, one version labels 272,000 tokens, while an older catalog cached locally by the user labels 500,000). When the window value changes, the compaction trigger point shifts accordingly, and the upper limit allowed by the catalog is far higher than the default window.

    This has two consequences: first, sessions of the same length behave inconsistently across client versions, making reproduction and comparison difficult; second, users assume that “enlarging the window makes things safe,” when in fact they have only pushed the trigger point back without changing the fact that compaction will inevitably happen.

    2.4 Third Account: Linear Amplification in Long Sessions

    The longer the session, the larger the history carried in each round; the more frequent the compaction, the more times full payment occurs. Multiplied together, the consumption curve shifts from roughly linear to stepwise jumps. This explains why a “seven-minute task” can eat up a five-hour window—the real cost comes from session history, not from the current task.

    3. Diagnostic Path: Four Steps to Locate the Source of Consumption

    1. Align the timeline: align the quota consumption curve with the timestamps of compaction messages in the session. If every jump immediately follows a compaction, the first account is basically confirmed.
    2. Check the window definition: inspect the window value and trigger ratio in the client’s built-in model catalog to confirm whether there is drift between versions.
    3. Measure in segments: run the same task in a short session and a long session, compare consumption per task, and quantify the marginal cost of carrying history.
    4. Rule out account factors: confirm that throttling and quota policies have not changed at the same time, to avoid mistaking a policy adjustment for a client defect.

    4. Engineering Practices: Turning Consumption into a Controllable Variable

    • Session splitting: proactively start a new session when task phases switch, using “cache-friendly” as one of the splitting criteria, instead of waiting to be compacted.
    • Explicit context budget: maintain a concise external state file in long tasks (decision records, interface contracts, to-dos) so that context stays “discardable and rebuildable.”
    • Stable prefix: keep the system prompt and tool definitions byte-for-byte stable, avoiding meaningless changes that invalidate the cache entirely.
    • Observability: use logging services and monitoring alerts to build dashboards and threshold alerts for token usage, cache hit rate, and compaction counts.
    • Quota guardrails: when carrying batch tasks on the platform side through container platforms and message queues, set a per-session budget cap and automatic circuit breaking to prevent a single long session from breaking through the overall quota.
    • Architectural decoupling: split large tasks into a pipeline of multiple short sessions, passing intermediate artifacts through object storage or message queues to avoid unlimited context growth.

    5. Conclusion

    The root cause of the consumption anomaly is not a single-point defect, but the stacking of three factors: inevitable compaction × strict prefix caching × drifting window definitions. Governance should correspond to three lines: reduce the preconditions for compaction (split sessions), lower the cost of compaction (protect the prefix and cache hits), and make the cost visible and controllable (observability and quota guardrails). In engineering practice on platforms such as Baidu AI Cloud, this combination of “budget—observability—guardrails” usually stabilizes cost performance better than chasing a fix in a single version.