Troubleshooting Abnormal Token Consumption in AI Coding Tools

    This article explains why token consumption in AI coding tools can spike without any change in user behavior. It examines three root causes: automatic context compaction thresholds and retention policies, prefix cache invalidation caused by compaction, and the amplification effect of multi-agent architectures. It also provides practical monitoring, strategy, architecture, and cost-control recommendations for diagnosing and mitigating hidden token usage.

    1. Symptom: Why the Consumption Curve Suddenly Steepens

    Recently, many developers using AI coding tools have reported a common issue: token consumption has jumped significantly despite no change in their usage habits. Scenarios that previously accounted for about 20% of total usage have climbed to roughly 50% in a short period. Quotas that once supported hours of conversation are now exhausted within 20 minutes.

    The defining characteristics are “unchanged input, unchanged output, sharply increased consumption.” This means the issue cannot be simply attributed to user behavior and should instead be analyzed from the platform’s context management mechanisms.

    2. Root Cause 1: Auto-Compaction Threshold and Post-Compaction Retention

    Most mainstream AI coding tools adopt a strategy of “context window + automatic compaction.” When the session token count approaches the model’s context limit, the system triggers compaction, summarizing historical conversations to free up space.

    Take a certain type of tool as an example: its auto-compaction trigger threshold is approximately 244,800 tokens. Before compaction, the context is about 240K–250K tokens; after compaction, 140K–160K tokens are still retained. This means:

    • A single compaction frees up only about 100K tokens of space;
    • If the main agent and multiple sub-agents work in parallel, compaction is triggered repeatedly;
    • Some users report that in one day, the main agent compacted 17 times and three sub-agents compacted 21/17/17 times respectively, for a cumulative total of 72 compactions.

    Each compaction itself requires an additional model call (input is the content to be compacted, output is the summary). The token consumption from this part is “additionally stacked” rather than generated by active user conversation. The more frequent the compaction, the higher the hidden consumption.

    3. Root Cause 2: Compaction Collapses the Cache Hit Rate

    In normal multi-turn conversations, prefix tokens remain unchanged, and the inference side can use prefix caching to significantly reduce repeated computation costs. However, compaction rewrites the context prefix, invalidating all existing caches.

    After compaction, the cache hit rate is extremely high (meaning the cache is being rebuilt), but building the cache itself requires a full prefill, which is an expensive full-input billing event. In other words:

    1. Normal request: cache hit → only new tokens are billed
    2. Post-compaction request: cache invalidated → full prefill → all 140K–160K tokens are billed

    If compaction happens 72 times a day, the additional input consumption from cache rebuilding alone could reach the millions of tokens.

    4. Root Cause 3: Multi-Agent Architecture Amplification

    In a main agent + sub-agent architecture, each sub-agent has an independent context. If task distribution is unreasonable, multiple sub-agents will approach the compaction threshold simultaneously, creating a “compaction storm.” When the three agents compact in parallel, the main agent must also wait and integrate the results, further lengthening the context and forming a positive feedback loop.

    5. Troubleshooting and Optimization Recommendations

    1. Monitoring: Instrument logs to record each compaction’s trigger time, token counts before and after compaction, and cache hit rate. Build a consumption dashboard with alerts.
    2. Strategy: Appropriately raise the compaction threshold or reduce the number of retained tokens to lower compaction frequency. Set independent and stricter context limits for sub-agents.
    3. Architecture: Split long tasks into short sessions to avoid unlimited growth of a single session. Adopt a “use and discard” mode for sub-agents to reduce context inheritance.
    4. Cost: Use container platforms to isolate different tasks, and combine monitoring alerts to set a daily token consumption cap to prevent abnormal usage spikes.

    6. Summary

    Abnormal token consumption is usually not a single bug, but the result of coupling among compaction strategy, caching mechanisms, and agent architecture. Understanding the key fact that “compaction means full prefill” is the first step in diagnosing the problem.