Troubleshooting Abnormal Token Consumption After LLM Context Compression

    This article investigates why token usage can spike after automatic context compression in LLM-based coding tools. It explains three root causes: compression itself consumes a full large-context input, compressed prefixes invalidate prompt caching and force cold restarts, and overlapping main/sub-agent contexts trigger repeated compression. It also outlines a reproducible test to separate compression issues from pricing changes, plus engineering fixes such as lowering the retention limit, stabilizing the prefix, minimizing sub-task context, merging compressions, and adding monitoring and budget guardrails.

    1. Symptom: Why Quota Consumption Suddenly Doubled

    After November 10, many developers using AI coding tools reported a clear jump in token consumption share for the same account and usage habits. Daily usage had previously accounted for about 20% of the quota, but in the last two days it climbed to around 50%. For some users, a quota that used to last several hours was exhausted within 20 minutes, even though the actual coding workload and number of conversation turns had not increased proportionally.

    A more telling case came from long sessions. One high-quota user triggered 72 automatic context compressions in a single day: 17 in the main agent and 21, 17, and 17 across three sub-agents. Before compression, the context was about 240K–250K tokens; after compression, it still retained 140K–160K tokens. In other words, each compression freed only about 100K tokens, far below expectations.

    Taken together, these observations point to a shared conclusion: the abnormal consumption is most likely not a single billing change, but a systemic amplification effect produced by the interaction between the context compression pipeline and the caching mechanism. The following sections break this down layer by layer.

    2. Root Cause 1: Compression Is Itself an Expensive Operation

    Automatic compression is not a “free cleanup.” A typical implementation sends the conversation history to the model for summarization, then writes the summary back into the context. This process consumes a full input and output token pass, and the input size is precisely the oversized context that is already near the threshold.

    A schematic pseudocode example:

    1. def auto_compress(ctx):
    2. # The input is the full current context, already near the threshold
    3. summary = model.summarize(ctx) # Cost: full ctx input + summary output
    4. new_ctx = summary + ctx.tail() # Summary + retained tail
    5. return new_ctx

    When the trigger threshold is around 244,800 tokens, each compression costs nearly 250K input tokens. With 72 compressions in a day, the input volume from compression alone reaches the tens of millions of tokens. This explains the first layer of “workload unchanged, consumption surging.”

    3. Root Cause 2: Cache Is Almost Entirely Invalidated After Compression

    In normal multi-turn conversations, prefix caching (Prompt Cache) relies on an “exact prefix match” to hit. Once a summary is inserted, message order is rearranged, or the system prompt is rewritten, the prefix hash changes, the existing cache is invalidated, and subsequent requests are billed again against the full context.

    The current compression strategy retains 140K–160K tokens after compression, which means:

    • Compression does not shrink the context enough, so cache rebuild cost remains high;
    • The first request immediately after compression must rebuild the cache with a 140K–160K cold-start input;
    • If sub-agents compress independently, the cache cannot be reused across sessions, and cold starts multiply.

    The hit logic can be simplified as:

    1. cache_hit = (current_prefix_hash == cached_prefix_hash)

    Compression rewrites the prefix, so cache_hit becomes false directly. The more frequent the compression, the more cold starts, and the more expensive the billing.

    4. Root Cause 3: Context Stacking and Repeated Compression Across Main and Sub-Agents

    Of the 72 compressions, the main agent accounted for 17 and the three sub-agents for 55 combined, showing that sub-tasks are the main consumers. Common problems include:

    1. Sub-agents inherit large blocks of the main session context instead of receiving only the necessary fragments;
    2. Sub-agents each maintain independent compression thresholds and trigger repeatedly;
    3. After a sub-task ends, its context is not reclaimed, and the main session keeps running with redundant history.

    Combined, these cause both compression count and cold-start count to rise, forming a positive feedback loop of consumption.

    5. How to Verify It Is a Compression Pipeline Issue Rather Than a Billing Issue

    Run a reproducible controlled experiment in the following order:

    1. Fix the task script and prompts, and record input/output tokens per turn;
    2. Disable automatic compression, run for 30 minutes, and record total consumption and cache hit rate;
    3. Enable automatic compression, run for the same 30 minutes, and compare the consumption curves;
    4. Check whether the input tokens of the first request after compression approach the retention limit;
    5. Check whether sub-agents share the cache and whether they compress repeatedly.

    If consumption returns to normal after disabling compression, the anomaly mainly comes from the compression and cache invalidation pipeline, not simply a unit price change.

    6. Engineering-Side Optimization Recommendations

    • Lower the retention limit: Reduce post-compression retention from 140K–160K to 40K–60K to keep cache rebuild cost under control;
    • Fix the summary prefix: Place the system prompt and fixed instructions at the very front and never rewrite them, ensuring a stable prefix;
    • Minimize sub-task context: Give sub-agents only the fragments required for the task, avoiding inheritance of full history;
    • Merge compressions: When compression triggers multiple times in a short window, merge them to reduce cold starts;
    • Instrument metrics: Build dashboards and alerts for compression count, tokens before and after compression, cache hit rate, and per-turn input tokens;
    • Budget guardrails: Set hourly token budgets and abnormal growth-rate alerts to avoid exhausting the quota in 20 minutes.

    7. Conclusion

    This kind of “consumption anomaly” is usually not a single bug, but an amplified result of compression strategy, caching mechanism, and multi-agent context management working together. As long as developers quantify the three pipelines—compression benefit, cache hit rate, and sub-task context—separately, they can quickly determine where the problem lies and use retention limits, prefix stability, and context minimization to bring costs back to a normal range.