Why LLM Context Auto-Compaction Spikes Token Usage: A Troubleshooting Guide

    This article explains why token consumption can suddenly double after a large language model update, focusing on context auto-compaction. It breaks down the components of request billing, identifies three high-probability causes—compaction overhead, cache invalidation, and insufficient context reduction—and outlines the observability fields needed to confirm them. It then provides layered optimization strategies across the application, platform, and usage-habit levels to keep costs predictable.

    1. The Symptom: Why the Usage Curve Suddenly Climbs

    Many developers using AI coding tools have reported a sharp jump in account usage within days of a model version update. Scenarios that once accounted for roughly 20% of total consumption climbed to around 50% in a short period. Quotas that previously supported hours of conversation were exhausted in about 20 minutes. Yet the complexity of the actual coding tasks and the number of interaction turns did not increase accordingly. This pattern of “same input, double the consumption” is rarely caused by a single factor; it is usually the result of multiple mechanisms stacking together.

    Another typical report comes from heavy users: context auto-compaction was triggered more than 70 times in a single day. The main agent compacted a dozen times, while three sub-agents each compacted around 20 times. Context size before compaction was roughly 240,000–250,000 tokens, and 140,000–160,000 tokens remained afterward. Once compaction finished, the cache hit rate rose abnormally, whereas normal multi-turn conversations usually struggle to reuse existing cache after compaction. Together, these details point in one direction: a mismatch between how the compaction mechanism is implemented and how billing is calculated.

    2. Breaking Down the Problem: What Makes Up Consumption

    To judge whether the anomaly is reasonable, first clarify what a single request is billed for. The widely accepted breakdown includes roughly four parts: input tokens that miss the cache, input tokens that hit the cache, output tokens, and, for some services, separate charges for long context and reasoning. A change in the coefficient of any one part is amplified on the total bill.

    Auto-compaction is an “extra request initiated by the system.” It must read the full context, generate a summary, and write it back, so it naturally incurs input and output consumption. If the compaction strategy is too aggressive and fires dozens of times a day, this overhead quickly accumulates into a major cost item rather than a negligible fraction.

    3. Three High-Probability Causes

    3.1 The Fixed Overhead of the Compaction Action Itself

    Compaction is not a lossless “trim”; it is a full model call. The longer the context, the more input tokens; the longer the summary, the more output tokens. When the trigger threshold is set too low, or the decision looks only at total size and not growth rate, the system repeatedly performs high-cost operations in a short window.

    3.2 Repeated Billing from Cache Invalidation

    Cache is usually matched based on prefix content. Compaction rewrites the context prefix, invalidating the entire segment that could have hit the cache. The system then issues requests with the “new prefix,” and the hit rate appears to rise—but in reality the old cache has been discarded and rebuilt. Writing and invalidating the old cache, plus rebuilding the new one, all translate into actual consumption. This is the root cause of “the cache hit rate is suspiciously high after compaction, yet the bill is higher.”

    3.3 High-Frequency Triggering from Insufficient Space Release

    If the retention ratio after compaction is too high—for example, dropping only from 250,000 to 150,000 while the trigger threshold sits around 240,000—then only a small amount of new content is needed to hit the ceiling again. After three compactions, the input of the next normal request may still stay at a high level, forming a “compact—hit ceiling—compact again” loop. Each sub-agent compacting independently multiplies this loop several times over.

    4. Observability: Gather the Evidence First

    Troubleshooting starts with being able to see the data. It is recommended to instrument both the client and the gateway, logging at least the following fields:

    1. request_id, agent_role(main/sub), turn_index,
    2. input_tokens, cached_tokens, output_tokens,
    3. compact_trigger(reason), tokens_before, tokens_after,
    4. cache_hit_ratio_before, cache_hit_ratio_after,
    5. latency_ms, model_version

    With these fields, you can answer three key questions: Is the consumption growth concentrated on compaction requests? Did cache invalidation happen after compaction? Is the post-compaction level still above the safety line? Without this evidence, any conclusion is only a guess.

    5. Layered Optimization Strategies

    Application Layer

    • Raise the trigger threshold and add a “growth rate” check to avoid repeated ceiling hits;
    • Set the post-compaction target level below 60% of the threshold to leave enough buffer;
    • Set independent budgets and compaction limits for sub-agents to prevent single-point amplification;
    • Place stable, unchanging system prompts and tool definitions at the very front of the context to improve prefix reuse.

    Platform Layer

    • Provide configurable compaction strategies so the retention ratio can be tuned per scenario;
    • Expose metrics such as compaction count and cache hit rate in logging and alerting;
    • Offer cost estimates for long-context scenarios so users can decide in advance.

    Usage Habits

    • Split tasks into multiple short sessions to reduce context growth in a single session;
    • Clean up irrelevant files and historical output promptly to keep ineffective content out of the context;
    • Use a summary file instead of full history as lightweight memory across sessions.

    6. Conclusion

    A sudden rise in usage is usually not a single defect but the combined effect of compaction overhead, cache rebuilding, and level control. Developers should first fill in the observability data, then converge layer by layer across the application, platform, and usage habits. If you use the relevant general capabilities from Baidu AI Cloud, you can combine logging, monitoring and alerting, and container platforms to collect metrics and roll out strategies gradually, keeping cost fluctuations within a predictable range.