LLM Context Compaction and Abnormal Token Usage: A Troubleshooting Guide

    This article explains why LLM token consumption can spike suddenly and how to troubleshoot it. It covers how automatic context compaction works, four technical root causes of abnormal usage (costly summarization, insufficient space release, cache invalidation, and multi-agent amplification), a step-by-step method to separate platform-side issues from usage-side issues, and best practices such as threshold tuning, prefix stability, concurrency control, and monitoring alerts.

    1. Symptom: Why the Usage Curve Suddenly Steepens

    Many developers have reported in community discussions that, under the same usage habits, the share of LLM inference quota consumed rises sharply within a very short period: calls that used to account for roughly 20% of total usage climb to around 50%, and quota that previously supported hours of interaction is now exhausted in 20–30 minutes, even though the number and complexity of actual coding tasks have not grown accordingly. This kind of “usage cliff” is rarely caused by a single factor; it needs to be investigated across three dimensions at once: context management, caching strategy, and concurrency orchestration.

    2. How Automatic Context Compaction Works

    Mainstream AI coding tools commonly adopt automatic compaction in long sessions: when token usage approaches the model’s context window limit, the system summarizes and rewrites the message history, replacing the original long history with shorter text to free up space and continue the conversation.

    The typical flow can be abstracted as:

    1. if used_tokens >= threshold:
    2. summary = summarize(history, keep_ratio=r)
    3. history = summary + recent_turns
    4. used_tokens = count_tokens(history)

    Three variables matter here: the trigger threshold, the retention ratio, and whether space is actually released after compaction. A misconfiguration in any one of them can degrade compaction from a “saving mechanism” into a “consumption amplifier.”

    3. Four Technical Root Causes of Abnormal Consumption

    3.1 Compaction Is Itself an Expensive Inference Call

    Generating a summary requires feeding the full long history into the model, so the input tokens are often the entire pre-compaction context. If the main agent compacts more than ten times a day and each of several sub-agents compacts nearly twenty times, the cumulative compaction input can reach millions of tokens—an overhead completely independent of the user’s real business requests.

    3.2 A High Retention Ratio Leaves Almost No Space Released

    Observational data shows the context is about 240K–250K tokens before compaction and still 140K–160K tokens after, releasing less than 40% of the space. If the automatic compaction threshold is set near 244,800 tokens, and the next normal request after three consecutive compactions takes 241,860, 241,604, and 242,041 input tokens respectively, the system falls into a “compact—still above threshold—compact again” loop, forming a self-amplifying consumption circuit.

    3.3 Hidden Costs from a Sharp Drop in Cache Hit Rate

    In normal multi-turn conversations the prefix stays stable, so the inference side can reuse the prefix cache, and the cached portion is usually free or billed at a low rate. Compaction rewrites the history prefix, invalidating the existing cache entirely; the request immediately after compaction then shows an extremely high cache hit rate, indicating the system is performing a large amount of repeated prefix computation. Compaction cost plus cache invalidation cost is one of the main sources of an abnormal bill.

    3.4 The Amplification Effect of Multi-Agent Concurrency

    The main agent and multiple sub-agents each maintain an independent context. If the threshold and retention ratio are configured per single session without accounting for concurrency, the number of compactions scales roughly linearly with the number of sub-agents, and they cannot share caches with one another.

    4. Quick Diagnosis: Separating “Platform Issues” from “Usage Issues”

    Collect evidence in the following order to avoid misjudgment:

    1. Export the usage details, tag them into three categories—compaction requests, business requests, and sub-agent requests—and watch for the point in time when the proportions shift.
    2. Record the token counts before and after each compaction, the retention ratio, and the first request’s input size after compaction, to verify whether a compaction loop exists.
    3. Check whether the fixed prefix (system prompt, repository notes, dependency list) is being rewritten frequently, a common trigger of cache invalidation.
    4. Rerun the same task with fewer concurrent sub-agents and compare the total token consumption.
    5. If all the above metrics look normal but consumption still spikes, then consider a platform-side policy change, keeping timestamps and screenshots for feedback to the service provider.

    5. Optimization Strategies and Best Practices

    • Tune the threshold and retention ratio: make the post-compaction token count clearly lower than the threshold (leave more than 30% headroom) to break the compaction loop at its root.
    • Stabilize the prefix: fix the system prompt, project conventions, and directory descriptions as an invariant prefix to improve cache reuse.
    • Control sub-agent concurrency: enable sub-agents only as the task requires, avoiding redundant context overhead from pointless parallelism.
    • Segment proactively: splitting a long task into several short sessions saves more quota than relying on passive compaction.
    • Build a dashboard: use monitoring alerts and log services to set threshold alerts on token consumption, cache hit rate, and compaction counts, so anomalies are detected within minutes.
    • Use elastic resources: for sudden batch tasks, container platforms can scale out elastically and object storage can archive historical sessions, lowering unit cost.

    6. Conclusion

    Context compaction is a double-edged sword: configured well, it significantly extends usable session length; configured poorly, the four factors of compaction input, cache invalidation, loop triggering, and multi-agent amplification stack up and drain the quota within a very short time. Developers should first prove the case with quantitative metrics before concluding that a platform-side change is responsible; meanwhile, threshold tuning, prefix stability, concurrency control, and monitoring alerts can compress uncontrollable consumption into a predictable range.