Why Long Sessions Drain Token Quotas: Causes and Optimization

    This article explains why long-running AI coding sessions can consume tokens equivalent to hours of continuous usage in just a few minutes. It breaks down three overlapping costs: automatic context compaction breaking prefix cache hits, summary growth triggering repeated compaction, and mismatches between billing rules and cache lifetime. You will also find a low-to-high diagnostic checklist for tracing abnormal bills to a specific stage, plus configuration and workflow practices for users and general engineering recommendations for platform teams.

    1. The Symptom: A 7.5-Minute Task That Bills Like 5 Hours

    In long sessions, developers often run into a confusing pattern: the actual interaction lasts only a few minutes and the model spends very little time on inference, yet the account is charged a token amount equivalent to hours of continuous calls. Pull up the billing details and there is no abnormal concurrency and no runaway loop — the input token count is simply enormous.

    That number says more than any explanation: compute usage and elapsed time are badly mismatched, which means the consumption does not come from “computing more” but from “sending more.” So the investigation should not stop at the model side; it should move to the intersection of context management and billing structure.

    The conclusion up front: this is most likely not a defect you can fix by changing one line of code. It is the result of three normally unrelated costs that all open at once and stack on top of each other in a long session. Understanding these three costs is what lets you tell whether you are dealing with a configuration issue, a mechanism issue, or a structural issue that has to wait for platform-side improvements.

    2. The Premise: Auto-Compaction Is Inevitable in Long Sessions

    Mainstream AI coding tools and LLM chat services all perform automatic compaction when the context approaches the window limit: earlier turns are replaced by a summary, the UI usually shows that context is being organized, and then reports that it is done.

    What matters is where the window size and the thresholds come from. The context window size is not something the client can set arbitrarily — it comes from the server-side model catalog. Catalogs cached at different points in time may report different values, so the same person can see different compaction trigger points on different machines. A common rule is that compaction starts at some relatively high percentage of the window and becomes a hard limit near a full window; compaction is forced regardless of configuration.

    Two important facts follow from this:

    • Compaction cannot be turned off. The tunable parameters usually only make compaction happen earlier; they cannot delay or disable it. The window parameter can be raised, but the ceiling is still constrained by the model catalog.
    • There is an internal “disable auto-compaction” switch, but it is a capability the service process reserves for its own internal threads, and user-side configuration cannot reach it.

    The conclusion is clear: as long as a session is long enough, compaction will happen. It was designed to save money — send fewer tokens — but given the billing structure, it turns into the most expensive step in the whole chain.

    Schematically, the logic can be abstracted as:

    1. # Illustrative mechanism only, not the interface of any specific product
    2. def before_request(messages, cfg):
    3. window = cfg.model_catalog_window # comes from the server catalog, not freely set by the client
    4. hard_limit = int(window * 0.95) # hard limit, forces compaction
    5. soft_limit = min(cfg.auto_compact_limit, hard_limit) # can only be earlier, never later
    6. if estimate_tokens(messages) >= soft_limit:
    7. messages = compact(messages) # early turns -> summary, the prefix is rewritten
    8. return messages

    3. Cost One: Compaction Shatters the Prefix Cache

    This is the most counterintuitive part. LLM billing typically includes a cache discount: input that hits the prefix cache is priced significantly lower than ordinary input that misses, while output is the most expensive of the three. The three often differ by an order of magnitude.

    The conditions for a prefix cache hit are very 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 this cache key; any difference anywhere invalidates the key.

    Compaction does exactly the opposite — it rewrites the early conversation into a summary. The prefix changes, the cache key is void, and in the first request after compaction those hundreds of thousands of tokens are all billed as ordinary input. An order-of-magnitude price difference is lost right at this step.

    Worse, compaction is not a one-time event. The summary itself takes up space, the task keeps moving forward, and the threshold is approached again fairly quickly, so compaction runs again. The user reports of “a long chat history being repeatedly auto-organized” correspond exactly to this behavior: every compaction means paying the full prefix cost again. The tokens compaction saves fall far short of the cache discount that is thrown away.

    4. Cost Two: Summary Bloat and Repeated Compaction

    Summaries have a cost, and that cost accumulates:

    • A summary requires an extra model call whose input is the long context being compacted and whose output is the summary text — this is pure incremental consumption.
    • Once written into the history, the summary becomes the new starting point of the prefix, and every subsequent request has to carry it.
    • If the summary keeps too much detail, the post-compaction size drops only slightly, the interval before the next compaction gets shorter, and a “compact — bloat — compact again” loop forms.

    A rough model is enough to show the trend:

    1. Let the window be W, the compaction threshold 0.8W, and the summary length about r of the compacted content
    2. With g new tokens added per turn, the number of turns between two compactions is roughly:
    3. N ≈ (W - r*C) / g # C is the context size at compaction time
    4. The larger r and g are, the smaller N is, the more frequent compaction becomes, and the more full-prefix re-payments occur

    The engineering takeaway: controlling the information density of the summary matters just as much as controlling how fast the context grows.

    5. Cost Three: Mismatched Billing Rules and Cache Lifetime

    Even without compaction, long sessions can get expensive, for three reasons:

    1. Caches have a lifetime. Once idle time exceeds the cache survival window, the next request has to rebuild the cache, which is equivalent to full-price input.
    2. The prefix must be strictly stable. Injecting any dynamic content into the system prompt or tool definitions (timestamps, random identifiers, constantly changing lists) makes the prefix different on every turn, so the cache can never hit.
    3. Multiple parallel sessions each maintain their own prefix. If they share the same account quota, total consumption multiplies, yet each individual session “looks normal.”

    Stacked on top of compaction, these three points make the billing curve show clear step-like jumps rather than smooth growth.

    6. Diagnosis: Tracing an Abnormal Bill to a Specific Stage

    Recommended order of investigation, from lowest to highest cost:

    • Check timeline alignment. Line up the moments when quota consumption jumps with the context-organization notices in the UI; if they overlap closely, compaction is almost certainly involved.
    • Check the input/output ratio. A normal coding task usually has an input-to-output ratio of a few times; an abnormally high input share means the prefix is not being reused.
    • Check the per-turn increment. Record the input token deltas across several consecutive requests; a jump in the hundreds of thousands on one turn is a full re-payment.
    • Check prefix stability. Look for dynamic fields in the system prompt and tool definitions.
    • Check the session-splitting strategy. Whether multiple unrelated tasks are being pushed forward in the same session.

    7. Optimization Practices

    Configuration and habits for users:

    • Split sessions along task boundaries. One independent task, one new session, so the context does not grow without limit.
    • Put stable content first and dynamic content last. Keeping the system prompt and tool definitions byte-for-byte stable is the prerequisite for cache hits.
    • Compact somewhat early. Actively organizing context during a long task is more controllable than waiting for the hard limit to trigger, but be aware that early compaction also breaks the prefix.
    • Avoid frequently switching models or tool sets within a long session; any switch changes the prefix composition.
    • Build a quota dashboard with monitoring, alerting, and logging services, and set threshold alerts on per-turn input tokens.

    For platform and tool implementations (general engineering advice):

    • Make compaction points configurable and observable, and return metadata to the caller such as whether compaction occurred this time and the token counts before and after.
    • Proactively rebuild the cache after compaction to shrink the window of one full-price input.
    • Use layered summarization: first extract structured key points from early turns, avoiding a natural-language long summary that keeps bloating.
    • Distinguish ordinary input, cached input, and output in the billing details so users can audit their own usage.

    8. Conclusion

    This kind of “short elapsed time, large charge” phenomenon is not a single-point defect. It is the resonance of context compaction, cache invalidation, and billing rules inside a long session. For developers, the practical response is to keep sessions small, keep the prefix stable, and turn compaction into an observable, explicit action. For platforms, making compaction configurable, explainable, and measurable is the key to bringing long-session costs back into a predictable range.