Troubleshooting Surging Token Consumption After LLM Context Auto-Compression

    This article explains why token usage can spike after enabling or relying on automatic context compression in LLM-based coding tools, even when the actual workload stays the same. It breaks down four common root causes—an overly high compression threshold, prefix cache invalidation, context stacking across multiple agents, and oversized tool outputs—and shows how to confirm each one with logs. You will also find a practical observability checklist, a five-step diagnostic order, and layered optimization advice covering parameters, structure, caching, tools, and governance, plus a regression approach to verify that costs drop without hurting task success rates.

    1. Symptom: Why the Usage Curve Suddenly Steepens

    Many teams using AI coding tools report a sharp jump in model consumption after a version change or a shift in the time window. A scenario that used to account for roughly one fifth of total usage climbs to nearly half within a short period; a quota that previously lasted several hours is exhausted in about twenty minutes; yet the actual volume of coding work and the number of conversation turns have not grown accordingly. This pattern—same input, same output, double the bill—is usually not a single billing issue. It means the context management mechanism has entered an inefficient state under a specific load.

    A few basic concepts need to be aligned first. The context window is the total amount of input and output tokens a single request can carry. Auto-compression means that when the context approaches its limit, the tool summarizes or trims historical messages to free up space so the conversation can continue. Prefix caching means the inference side reuses repeated computation for identical prefixes; a cache hit can significantly reduce both latency and cost. These mechanisms are common designs across the industry, implementation details differ, but the symptoms after they fail are highly similar.

    2. A Rigorous Breakdown of Four Frequent Causes

    2.1 Compression Threshold Too High, Retention Ratio Too Large: Compression That Does Not Relieve Pressure

    One typical configuration sets the trigger threshold at around 240,000 tokens and still retains 140,000 to 160,000 tokens after compression. This means compression only reclaims about one third of the space, and the remaining volume still approaches the limit, so it triggers again after a few turns. If a single task involves more than ten main-chain compressions and each subtask compresses another ten-plus times, the accumulated summary generation alone consumes a large amount of input and output tokens, creating a negative-return loop of “spending first to save space.”

    The判断 method is straightforward: count the number of compressions per unit task, the token counts before and after each compression, and the input size of the first normal request after compression. If post-compression input stays above sixty percent of the threshold over time, the retention policy is too aggressive.

    2.2 Prefix Cache Invalidation: Compression Triggers a Full Recompute

    Multi-turn conversations save money because the prefix stays stable and the cache can be reused. Compression rewrites historical messages, the prefix changes accordingly, and the entire cache that could have been hit is invalidated; subsequent requests are billed as misses. If a “high cache hit ratio” reading appears after compression, it is often because the compression output has been fixed as a new stable prefix—it looks good in the short term, but every new conversation turn pays for this rebuilt long prefix.

    Illustrative log fields can be designed like this:

    1. request_id, stage, input_tokens, cached_tokens, cache_hit_ratio, compress_seq, kept_tokens
    2. r-001, normal, 248000, 12000, 0.05, 0, 0
    3. r-002, compress, 250000, 0, 0.00, 1, 150000
    4. r-003, normal, 151000, 138000,0.91, 1, 150000

    Watch for the cliff in cached_tokens before and after compression, and whether input_tokens rises in step as compress_seq increments.

    2.3 Context Stacking Across Multiple Agents Amplifies the Compression Count

    The main agent and multiple sub-agents each maintain their own context. If they share the same long history or frequently pass back a complete summary, the number of compressions multiplies. In the case described, the main chain compressed more than ten times and each of three sub-chains compressed more than ten times—essentially compressing one long context repeatedly. A subtask’s context should keep only the fragments strongly related to its own goal, rather than inheriting the entire history.

    2.4 Oversized Tool Outputs “Overfeed” the Compression Threshold

    If code search, log reading, and build output are fed back in full, a single tool result can consume tens of thousands of tokens. Without truncation and structured summarization, the threshold is pushed up quickly and compression triggers frequently. Set a length limit on tool output and return structured summaries in preference to raw full text.

    3. Build Observability First: Measure, Then Optimize

    Without metrics there is no way to locate the problem. Collect at least three categories of data. First, compression metrics: trigger count, tokens before and after compression, retention ratio, and compression time. Second, cache metrics: cached tokens, hit ratio, and changes in prefix length. Third, business metrics: task success rate, average number of turns, and cost per task. Only when all three are aligned to the same request_id and timeline can you tell whether the growth in consumption comes from “computing more” or “computing more expensively.”

    The recommended diagnostic order is:

    1. Confirm whether the consumption growth is concentrated in compression-heavy tasks;
    2. Check whether post-compression input is still above sixty percent of the threshold;
    3. Compare the cache hit breakpoint before and after compression;
    4. Measure the share of sub-agent compressions to identify duplicate compression;
    5. Sample tool output sizes to locate oversized feedback.

    4. Layered Optimization Plan

    Parameter layer: lower the trigger threshold to around seventy percent of the window, keep the retention ratio within forty percent of the pre-compression size, and set a minimum usable space so the context does not still approach the limit after compression.

    Structure layer: apply layered memory to history—keep recent conversation as-is and turn older content into structured summaries; give sub-agents an independent, lean context and forbid full inheritance.

    Cache layer: implement compression as much as possible by appending summary blocks to keep the prefix stable; place volatile content at the end to reduce cache invalidation caused by prefix jitter.

    Tool layer: set token limits for search and log-type returns and output structured fragments; aggregate build and test logs before returning them.

    Governance layer: set per-task limits on compression count and cost; when exceeded, degrade to “summary plus key files only” and trigger an alert, so quota is not silently burned.

    5. Validation and Regression

    After optimization, run a controlled experiment: fix the task set and compare total tokens, cache hit ratio, compression count, and task success rate before and after. A healthy signal is fewer compressions, post-compression input well below the threshold, a stable cache hit ratio, and no degradation in task success rate. If cost drops but success rate declines, the retention policy has trimmed too aggressively and key context needs to be restored.

    6. Conclusion

    Abnormal consumption is not frightening; what is frightening is the lack of a shared definition and of observability. Linking the three metric families—compression, cache, and business—explains and contains most cases of “quota gone in twenty minutes.” For teams using Baidu AI Cloud services, carrying the observability and quota policies described above on general-purpose logging services, monitoring and alerting, and container platforms can turn cost governance from reactive firefighting into proactive control.