Long LLM Sessions and Token Costs: Tracing and Fixing Abnormal Token Consumption

    This article traces why long LLM sessions can consume an entire five-hour quota window in minutes. It explains three compounding causes: auto compaction breaking the prefix cache, compaction thresholds drifting with the context window, and larger windows amplifying each loss. It also outlines how to verify the issue with token metrics and a practical governance checklist for developers and enterprise users.

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

    A scenario that comes up repeatedly: a high-tier session task finishes its actual execution in seven and a half minutes, yet consumes an entire five-hour quota window. The number is striking because it puts the enormous gap between the compute bill and the actual work on full display.

    The conclusion needs to be stated up front: this kind of anomaly is most likely not a defect that can be fixed by changing a single line of code. Instead, it is three separate bills that are normally unrelated, all opening at once under the specific condition of a long session. Understanding how these three bills stack is more valuable from an engineering standpoint than arguing about whether a particular tool has a bug.

    2. Bill One: Auto Compaction Shatters the Prefix Cache

    2.1 Prefix cache hits require exact conditions

    Mainstream large model inference services commonly use prefix caching to reduce the cost of repeated input. The billing structure typically shows a clear tiered difference, illustrated below (the numbers only convey the order of magnitude and are not any vendor’s actual pricing):

    1. Regular input : 100 units / million tokens
    2. Cache-hit input : 10 units / million tokens # roughly an order-of-magnitude discount
    3. Output : 500 units / million tokens

    The precondition for a hit is that the leading byte sequence of the request must match the most recent request byte for byte. This sequence is usually made up of the system prompt, tool and function definitions, and the earliest few turns of the conversation, together forming a cache key. Any change to a single character invalidates the entire prefix.

    2.2 Compaction is essentially rewriting the prefix

    What context auto compaction does is precisely replace earlier turns with a summary. In the interface this typically appears as a “compacting context” to “context compacted” notice. Once the summary is generated, the byte content of the prefix changes, and the cache key is invalidated.

    The consequence: the first request after compaction will bill those hundreds of thousands of tokens entirely as “regular input” rather than “cache input.” An order-of-magnitude price difference is erased in this single step. Worse, compaction is often not a one-time event — the summary itself takes up space, and as the task continues it approaches the threshold again, forming a “compact — pay full price again — compact again” cycle. The “verbose sessions being repeatedly auto-compacted” that users report is essentially paying the full prefix price every time a compaction happens.

    The few input tokens saved fall far short of the lost cache discount. This is the most expensive step in the whole chain, and the most counterintuitive.

    3. Bill Two: Threshold Drift Makes the Same Session Behave Differently Over Time

    3.1 Thresholds are derived from the context window

    The trigger point for auto compaction is usually not a hard-coded fixed value but is derived as a percentage of the context window. A common industry strategy is: begin soft compaction at around 90% usage, and trigger forced compaction at around 95%, which cannot be bypassed no matter how the upper layer is configured.

    This means the same session can behave completely differently under different versions of the model catalog:

    1. Catalog version A: window 272,000 → soft compaction starts around 244,800
    2. Catalog version B: window 500,000 → soft compaction starts around 450,000
    3. Hard limit: 95% of the window, forced compaction cannot be disabled

    3.2 Directional limits on configurable options

    Most implementations only allow “compacting earlier,” not “delaying or disabling.” For example, a certain class of parameters can only lower the soft compaction threshold, another class can only raise the context window, and raising it is subject to a catalog-declared upper limit. At the code level there may be a switch to “disable auto compaction,” but it is usually only for internal service threads and cannot be reached through user-side configuration.

    The conclusion: as long as the session is long enough, compaction will inevitably happen. It was designed to save money, but in practice it has become the most expensive link.

    4. Bill Three: The Large Window Itself Amplifies Each Single Loss

    The larger the window, the larger the prefix invalidated by a single compaction. When the window expands from just over two hundred thousand to five hundred thousand or higher, the “full re-payment” amount for one compaction scales linearly. The combined effect of the three bills: the task duration is unchanged, but the bill multiplies several times over.

    5. How to Verify: Turning Guesswork into Observable Evidence

    It is recommended to gather evidence in the following order, avoiding staying at the level of intuition:

    1. Record the separate usage of the three token types — input, cache-hit input, and output — for a single session, and observe whether the cache hit ratio drops sharply after the compaction notice appears.
    2. Align the model catalog version with the effective context window value, and confirm the actual starting points of soft and forced compaction.
    3. Mark the timestamp of each “context compacted” event within the same session, and align it on a timeline with billing spikes.
    4. Use a minimal reproduction: construct a long session that has just crossed the soft compaction threshold, and compare the cache hit difference between the two requests before and after compaction.

    Illustrative monitoring metric:

    1. Metric: cache_hit_ratio = cache-hit input / total input
    2. Expected: a normal long session should maintain a high value over time
    3. Anomaly: after "context compacted" appears, this ratio drops off a cliff

    6. Governance: An Actionable Checklist for Developers and Enterprise Users

    • Control single-session length: splitting tasks into segments and starting a new session promptly, breaking long tasks into multiple short sessions, is the highest cost-benefit measure.
    • Stabilize the prefix: keep the system prompt and tool definitions fixed at the very front of the request, avoiding frequent changes that invalidate the cache key.
    • Actively manage context: manually organize and archive before approaching the soft compaction threshold, instead of passively waiting for auto compaction.
    • Budgeting and alerts: build a usage dashboard based on the three metrics — regular input, cache input, and output — and set alerts for drops in the cache hit ratio.
    • Architectural decoupling: use object storage to hold intermediate artifacts of long tasks, use a message queue to chain stage tasks, use a container platform to host stateless execution units, and use logging and monitoring services to accumulate observable data, avoiding piling all state into a single session context.
    • Platform-side capabilities: if using a cloud model service, prioritize a general-purpose solution that supports prefix caching, observable usage details, and elastic quota management, and specify the billing basis clearly in the contract.

    7. Conclusion

    “Seven and a half minutes for a five-hour quota” is not magic; it is the inevitable result of auto compaction, threshold drift, and large-window amplification stacking together. Treating it as an observable, reproducible, and governable engineering problem, and hedging with segmented sessions, a stable prefix, active context management, and usage alerts, is the correct path to pulling the bill back into a controllable range.